XMLConfiguration.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // XMLConfiguration.h
  3. //
  4. // Library: Util
  5. // Package: Configuration
  6. // Module: XMLConfiguration
  7. //
  8. // Definition of the XMLConfiguration class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_XMLConfiguration_INCLUDED
  16. #define Util_XMLConfiguration_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #ifndef POCO_UTIL_NO_XMLCONFIGURATION
  19. #include "Poco/Util/MapConfiguration.h"
  20. #include "Poco/DOM/Document.h"
  21. #include "Poco/DOM/AutoPtr.h"
  22. #include "Poco/DOM/DOMWriter.h"
  23. #include "Poco/SAX/InputSource.h"
  24. #include <istream>
  25. namespace Poco {
  26. namespace Util {
  27. class Util_API XMLConfiguration: public AbstractConfiguration
  28. /// This configuration class extracts configuration properties
  29. /// from an XML document. An XPath-like syntax for property
  30. /// names is supported to allow full access to the XML document.
  31. /// XML namespaces are not supported. The name of the root element
  32. /// of the XML document is not significant and ignored.
  33. /// Periods in tag names are not supported.
  34. ///
  35. /// Given the following XML document as an example:
  36. ///
  37. /// <config>
  38. /// <prop1>value1</prop1>
  39. /// <prop2>value2</prop2>
  40. /// <prop3>
  41. /// <prop4 attr="value3"/>
  42. /// <prop4 attr="value4"/>
  43. /// </prop3>
  44. /// <prop5 id="first">value5</prop5>
  45. /// <prop5 id="second">value6</prop5>
  46. /// </config>
  47. ///
  48. /// The following property names would be valid and would
  49. /// yield the shown values:
  50. ///
  51. /// prop1 -> value1
  52. /// prop2 -> value2
  53. /// prop3.prop4 -> (empty string)
  54. /// prop3.prop4[@attr] -> value3
  55. /// prop3.prop4[1][@attr] -> value4
  56. /// prop5[0] -> value5
  57. /// prop5[1] -> value6
  58. /// prop5[@id=first] -> value5
  59. /// prop5[@id='second'] -> value6
  60. ///
  61. /// Enumerating attributes is not supported.
  62. /// Calling keys("prop3.prop4") will return an empty range.
  63. ///
  64. /// As a special feature, the delimiter character used to delimit
  65. /// property names can be changed to something other than period ('.') by
  66. /// passing the desired character to the constructor. This allows
  67. /// working with XML documents having element names with periods
  68. /// in them.
  69. {
  70. public:
  71. XMLConfiguration();
  72. /// Creates an empty XMLConfiguration with a "config" root element.
  73. XMLConfiguration(char delim);
  74. /// Creates an empty XMLConfiguration with a "config" root element,
  75. /// using the given delimiter char instead of the default '.'.
  76. XMLConfiguration(Poco::XML::InputSource* pInputSource);
  77. /// Creates an XMLConfiguration and loads the XML document from
  78. /// the given InputSource.
  79. XMLConfiguration(Poco::XML::InputSource* pInputSource, char delim);
  80. /// Creates an XMLConfiguration and loads the XML document from
  81. /// the given InputSource. Uses the given delimiter char instead
  82. /// of the default '.'.
  83. XMLConfiguration(std::istream& istr);
  84. /// Creates an XMLConfiguration and loads the XML document from
  85. /// the given stream.
  86. XMLConfiguration(std::istream& istr, char delim);
  87. /// Creates an XMLConfiguration and loads the XML document from
  88. /// the given stream. Uses the given delimiter char instead
  89. /// of the default '.'.
  90. XMLConfiguration(const std::string& path);
  91. /// Creates an XMLConfiguration and loads the XML document from
  92. /// the given path.
  93. XMLConfiguration(const std::string& path, char delim);
  94. /// Creates an XMLConfiguration and loads the XML document from
  95. /// the given path. Uses the given delimiter char instead
  96. /// of the default '.'.
  97. XMLConfiguration(const Poco::XML::Document* pDocument);
  98. /// Creates the XMLConfiguration using the given XML document.
  99. XMLConfiguration(const Poco::XML::Document* pDocument, char delim);
  100. /// Creates the XMLConfiguration using the given XML document.
  101. /// Uses the given delimiter char instead of the default '.'.
  102. XMLConfiguration(const Poco::XML::Node* pNode);
  103. /// Creates the XMLConfiguration using the given XML node.
  104. XMLConfiguration(const Poco::XML::Node* pNode, char delim);
  105. /// Creates the XMLConfiguration using the given XML node.
  106. /// Uses the given delimiter char instead of the default '.'.
  107. void load(Poco::XML::InputSource* pInputSource);
  108. /// Loads the XML document containing the configuration data
  109. /// from the given InputSource.
  110. void load(Poco::XML::InputSource* pInputSource, unsigned long namePoolSize);
  111. /// Loads the XML document containing the configuration data
  112. /// from the given InputSource. Uses the give namePoolSize (which
  113. /// should be a suitable prime like 251, 509, 1021, 4093) for the
  114. /// internal DOM Document's name pool.
  115. void load(std::istream& istr);
  116. /// Loads the XML document containing the configuration data
  117. /// from the given stream.
  118. void load(const std::string& path);
  119. /// Loads the XML document containing the configuration data
  120. /// from the given file.
  121. void load(const Poco::XML::Document* pDocument);
  122. /// Loads the XML document containing the configuration data
  123. /// from the given XML document.
  124. void load(const Poco::XML::Node* pNode);
  125. /// Loads the XML document containing the configuration data
  126. /// from the given XML node.
  127. void loadEmpty(const std::string& rootElementName);
  128. /// Loads an empty XML document containing only the
  129. /// root element with the given name.
  130. void save(const std::string& path) const;
  131. /// Writes the XML document containing the configuration data
  132. /// to the file given by path.
  133. void save(std::ostream& str) const;
  134. /// Writes the XML document containing the configuration data
  135. /// to the given stream.
  136. void save(Poco::XML::DOMWriter& writer, const std::string& path) const;
  137. /// Writes the XML document containing the configuration data
  138. /// to the file given by path, using the given DOMWriter.
  139. ///
  140. /// This can be used to use a DOMWriter with custom options.
  141. void save(Poco::XML::DOMWriter& writer, std::ostream& str) const;
  142. /// Writes the XML document containing the configuration data
  143. /// to the given stream.
  144. ///
  145. /// This can be used to use a DOMWriter with custom options.
  146. protected:
  147. bool getRaw(const std::string& key, std::string& value) const;
  148. void setRaw(const std::string& key, const std::string& value);
  149. void enumerate(const std::string& key, Keys& range) const;
  150. void removeRaw(const std::string& key);
  151. ~XMLConfiguration();
  152. private:
  153. const Poco::XML::Node* findNode(const std::string& key) const;
  154. Poco::XML::Node* findNode(const std::string& key);
  155. Poco::XML::Node* findNode(std::string::const_iterator& it, const std::string::const_iterator& end, Poco::XML::Node* pNode, bool create = false) const;
  156. static Poco::XML::Node* findElement(const std::string& name, Poco::XML::Node* pNode, bool create);
  157. static Poco::XML::Node* findElement(int index, Poco::XML::Node* pNode, bool create);
  158. static Poco::XML::Node* findElement(const std::string& attr, const std::string& value, Poco::XML::Node* pNode);
  159. static Poco::XML::Node* findAttribute(const std::string& name, Poco::XML::Node* pNode, bool create);
  160. Poco::XML::AutoPtr<Poco::XML::Node> _pRoot;
  161. Poco::XML::AutoPtr<Poco::XML::Document> _pDocument;
  162. char _delim;
  163. };
  164. } } // namespace Poco::Util
  165. #endif // POCO_UTIL_NO_XMLCONFIGURATION
  166. #endif // Util_XMLConfiguration_INCLUDED