XMLElement.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "BoundingBox.h"
  24. #include "Rect.h"
  25. #include "Ptr.h"
  26. #include "Variant.h"
  27. namespace pugi
  28. {
  29. struct xml_node_struct;
  30. class xpath_node;
  31. class xpath_node_set;
  32. class xpath_query;
  33. class xpath_variable_set;
  34. }
  35. namespace Urho3D
  36. {
  37. class XMLFile;
  38. class XPathQuery;
  39. class XPathResultSet;
  40. /// Element in an XML file.
  41. class XMLElement
  42. {
  43. public:
  44. /// Construct null element.
  45. XMLElement();
  46. /// Construct with document and node pointers.
  47. XMLElement(XMLFile* file, pugi::xml_node_struct* node);
  48. /// Construct from xpath query result set.
  49. XMLElement(XMLFile* file, const XPathResultSet* resultSet, const pugi::xpath_node* xpathNode, unsigned xpathResultIndex);
  50. /// Copy-construct from another element.
  51. XMLElement(const XMLElement& rhs);
  52. /// Destruct.
  53. ~XMLElement();
  54. /// Assignment operator.
  55. XMLElement& operator = (const XMLElement& rhs);
  56. /// Create a child element.
  57. XMLElement CreateChild(const String& name);
  58. /// Create a child element.
  59. XMLElement CreateChild(const char* name);
  60. /// Remove a child element. Return true if successful.
  61. bool RemoveChild(const XMLElement& element);
  62. /// Remove a child element by name. Return true if successful.
  63. bool RemoveChild(const String& name);
  64. /// Remove a child element by name. Return true if successful.
  65. bool RemoveChild(const char* name);
  66. /// Remove child elements of certain name, or all child elements if name is empty. Return true if successful.
  67. bool RemoveChildren(const String& name = String::EMPTY);
  68. /// Remove child elements of certain name, or all child elements if name is empty. Return true if successful.
  69. bool RemoveChildren(const char* name);
  70. /// Remove an attribute by name. Return true if successful.
  71. bool RemoveAttribute(const String& name = String::EMPTY);
  72. /// Remove an attribute by name. Return true if successful.
  73. bool RemoveAttribute(const char* name);
  74. /// Select an element/attribute using XPath query.
  75. XMLElement SelectSingle(const String& query, pugi::xpath_variable_set* variables = 0) const;
  76. /// Select an element/attribute using XPath query.
  77. XMLElement SelectSinglePrepared(const XPathQuery& query) const;
  78. /// Select elements/attributes using XPath query.
  79. XPathResultSet Select(const String& query, pugi::xpath_variable_set* variables = 0) const;
  80. /// Select elements/attributes using XPath query.
  81. XPathResultSet SelectPrepared(const XPathQuery& query) const;
  82. /// Set an attribute.
  83. bool SetAttribute(const String& name, const String& value);
  84. /// Set an attribute.
  85. bool SetAttribute(const char* name, const char* value);
  86. /// Set an attribute. Only valid if it is an attribute only XPath query result.
  87. bool SetAttribute(const String& value);
  88. /// Set an attribute. Only valid if it is an attribute only XPath query result.
  89. bool SetAttribute(const char* value);
  90. /// Set a bool attribute.
  91. bool SetBool(const String& name, bool value);
  92. /// Set a BoundingBox attribute.
  93. bool SetBoundingBox(const BoundingBox& value);
  94. /// Set a buffer attribute.
  95. bool SetBuffer(const String& name, const void* data, unsigned size);
  96. /// Set a buffer attribute.
  97. bool SetBuffer(const String& name, const PODVector<unsigned char>& value);
  98. /// Set a color attribute.
  99. bool SetColor(const String& name, const Color& value);
  100. /// Set a float attribute.
  101. bool SetFloat(const String& name, float value);
  102. /// Set an unsigned integer attribute.
  103. bool SetUInt(const String& name, unsigned value);
  104. /// Set an integer attribute.
  105. bool SetInt(const String& name, int value);
  106. /// Set an IntRect attribute.
  107. bool SetIntRect(const String& name, const IntRect& value);
  108. /// Set an IntVector2 attribute.
  109. bool SetIntVector2(const String& name, const IntVector2& value);
  110. /// Set a Rect attribute.
  111. bool SetRect(const String& name, const Rect& value);
  112. /// Set a quaternion attribute.
  113. bool SetQuaternion(const String& name, const Quaternion& value);
  114. /// Set a string attribute.
  115. bool SetString(const String& name, const String& value);
  116. /// Set a variant attribute.
  117. bool SetVariant(const Variant& value);
  118. /// Set a variant attribute excluding the type.
  119. bool SetVariantValue(const Variant& value);
  120. /// Set a resource reference attribute.
  121. bool SetResourceRef(const ResourceRef& value);
  122. /// Set a resource referene list attribute.
  123. bool SetResourceRefList(const ResourceRefList& value);
  124. /// Set a variant vector attribute. Creates child elements as necessary.
  125. bool SetVariantVector(const VariantVector& value);
  126. /// Set a variant map attribute. Creates child elements as necessary.
  127. bool SetVariantMap(const VariantMap& value);
  128. /// Set a Vector2 attribute.
  129. bool SetVector2(const String& name, const Vector2& value);
  130. /// Set a Vector3 attribute.
  131. bool SetVector3(const String& name, const Vector3& value);
  132. /// Set a Vector4 attribute.
  133. bool SetVector4(const String& name, const Vector4& value);
  134. /// Return whether does not refer to an element or an XPath node.
  135. bool IsNull() const;
  136. /// Return whether refers to an element or an XPath node.
  137. bool NotNull() const;
  138. /// Return true if refers to an element or an XPath node.
  139. operator bool () const;
  140. /// Return element name (or attribute name if it is an attribute only XPath query result).
  141. String GetName() const;
  142. /// Return whether has a child element.
  143. bool HasChild(const String& name) const;
  144. /// Return whether has a child element.
  145. bool HasChild(const char* name) const;
  146. /// Return child element, or null if missing.
  147. XMLElement GetChild(const String& name = String::EMPTY) const;
  148. /// Return child element, or null if missing.
  149. XMLElement GetChild(const char* name) const;
  150. /// Return next sibling element.
  151. XMLElement GetNext(const String& name = String::EMPTY) const;
  152. /// Return next sibling element.
  153. XMLElement GetNext(const char* name) const;
  154. /// Return parent element.
  155. XMLElement GetParent() const;
  156. /// Return number of attributes.
  157. unsigned GetNumAttributes() const;
  158. /// Return whether has an attribute.
  159. bool HasAttribute(const String& name) const;
  160. /// Return whether has an attribute.
  161. bool HasAttribute(const char* name) const;
  162. /// Return attribute, or empty if missing.
  163. String GetAttribute(const String& name = String::EMPTY) const;
  164. /// Return attribute, or empty if missing.
  165. String GetAttribute(const char* name) const;
  166. /// Return attribute as C string, or null if missing.
  167. const char* GetAttributeCString(const char* name) const;
  168. /// Return attribute in lowercase, or empty if missing.
  169. String GetAttributeLower(const String& name) const;
  170. /// Return attribute in lowercase, or empty if missing.
  171. String GetAttributeLower(const char* name) const;
  172. /// Return attribute in lowercase, or empty if missing.
  173. String GetAttributeUpper(const String& name) const;
  174. /// Return attribute in lowercase, or empty if missing.
  175. String GetAttributeUpper(const char* name) const;
  176. /// Return names of all attributes.
  177. Vector<String> GetAttributeNames() const;
  178. /// Return bool attribute, or false if missing.
  179. bool GetBool(const String& name) const;
  180. /// Return buffer attribute, or empty if missing.
  181. PODVector<unsigned char> GetBuffer(const String& name) const;
  182. /// Copy buffer attribute into a supplied buffer. Return true if buffer was large enough.
  183. bool GetBuffer(const String& name, void* dest, unsigned size) const;
  184. /// Return bounding box attribute, or empty if missing.
  185. BoundingBox GetBoundingBox() const;
  186. /// Return a color attribute, or default if missing.
  187. Color GetColor(const String& name) const;
  188. /// Return a float attribute, or zero if missing.
  189. float GetFloat(const String& name) const;
  190. /// Return an unsigned integer attribute, or zero if missing.
  191. unsigned GetUInt(const String& name) const;
  192. /// Return an integer attribute, or zero if missing.
  193. int GetInt(const String& name) const;
  194. /// Return an IntRect attribute, or default if missing.
  195. IntRect GetIntRect(const String& name) const;
  196. /// Return an IntVector2 attribute, or default if missing.
  197. IntVector2 GetIntVector2(const String& name) const;
  198. /// Return a Rect attribute, or default if missing.
  199. Rect GetRect(const String& name) const;
  200. /// Return a quaternion attribute, or default if missing.
  201. Quaternion GetQuaternion(const String& name) const;
  202. /// Return a variant attribute, or empty if missing.
  203. Variant GetVariant() const;
  204. /// Return a variant attribute with static type.
  205. Variant GetVariantValue(VariantType type) const;
  206. /// Return a resource reference attribute, or empty if missing.
  207. ResourceRef GetResourceRef() const;
  208. /// Return a resource reference list attribute, or empty if missing.
  209. ResourceRefList GetResourceRefList() const;
  210. /// Return a variant vector attribute, or empty if missing.
  211. VariantVector GetVariantVector() const;
  212. /// Return a variant map attribute, or empty if missing.
  213. VariantMap GetVariantMap() const;
  214. /// Return a Vector2 attribute, or default if missing.
  215. Vector2 GetVector2(const String& name) const;
  216. /// Return a Vector3 attribute, or default if missing.
  217. Vector3 GetVector3(const String& name) const;
  218. /// Return a Vector4 attribute, or default if missing.
  219. Vector4 GetVector4(const String& name) const;
  220. /// Return any Vector attribute as Vector4. Missing coordinates will be zero.
  221. Vector4 GetVector(const String& name) const;
  222. /// Return XML file.
  223. XMLFile* GetFile() const;
  224. /// Return pugixml xml_node_struct.
  225. pugi::xml_node_struct* GetNode() const { return node_; }
  226. /// Return XPath query result set.
  227. const XPathResultSet* GetXPathResultSet() const { return xpathResultSet_; }
  228. /// Return pugixml xpath_node.
  229. const pugi::xpath_node* GetXPathNode() const { return xpathNode_; }
  230. /// Return current result index.
  231. unsigned GetXPathResultIndex() const { return xpathResultIndex_; }
  232. /// Return next XPath query result. Only valid when this instance of XMLElement is itself one of the query result in the result set.
  233. XMLElement NextResult() const;
  234. /// Empty XMLElement.
  235. static const XMLElement EMPTY;
  236. private:
  237. /// XML file.
  238. WeakPtr<XMLFile> file_;
  239. /// Pugixml node.
  240. pugi::xml_node_struct* node_;
  241. /// XPath query result set.
  242. const XPathResultSet* xpathResultSet_;
  243. /// Pugixml xpath_node.
  244. const pugi::xpath_node* xpathNode_;
  245. /// Current XPath query result index (used internally to advance to subsequent query result).
  246. mutable unsigned xpathResultIndex_;
  247. };
  248. /// XPath query result set.
  249. class XPathResultSet
  250. {
  251. public:
  252. /// Construct empty result set.
  253. XPathResultSet();
  254. /// Construct with result set from XPath query.
  255. XPathResultSet(XMLFile* file, pugi::xpath_node_set* resultSet);
  256. // Copy-construct.
  257. XPathResultSet(const XPathResultSet& rhs);
  258. /// Destruct.
  259. ~XPathResultSet();
  260. /// Assignment operator.
  261. XPathResultSet& operator = (const XPathResultSet& rhs);
  262. /// Return the n-th result in the set. Call XMLElement::GetNextResult() to get the subsequent result in the set.
  263. /// Note: The XPathResultSet return value must be stored in a lhs variable to ensure the underlying xpath_node_set* is still valid while performing XPathResultSet::FirstResult(), XPathResultSet::operator [], and XMLElement::NextResult().
  264. XMLElement operator[](unsigned index) const;
  265. /// Return the first result in the set. Call XMLElement::GetNextResult() to get the subsequent result in the set.
  266. /// Note: The XPathResultSet return value must be stored in a lhs variable to ensure the underlying xpath_node_set* is still valid while performing XPathResultSet::FirstResult(), XPathResultSet::operator [], and XMLElement::NextResult().
  267. XMLElement FirstResult();
  268. /// Return size of result set.
  269. unsigned Size() const;
  270. /// Return whether result set is empty.
  271. bool Empty() const;
  272. /// Return pugixml xpath_node_set.
  273. pugi::xpath_node_set* GetXPathNodeSet() const { return resultSet_; }
  274. private:
  275. /// XML file.
  276. WeakPtr<XMLFile> file_;
  277. /// Pugixml xpath_node_set.
  278. pugi::xpath_node_set* resultSet_;
  279. };
  280. /// XPath query.
  281. class XPathQuery
  282. {
  283. public:
  284. /// Construct empty.
  285. XPathQuery();
  286. /// Construct XPath query object with query string and variable string. The variable string format is "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet".
  287. XPathQuery(const String& queryString, const String& variableString = String::EMPTY);
  288. /// Destruct.
  289. ~XPathQuery();
  290. /// Bind query object with variable set.
  291. void Bind();
  292. /// Add/Set a bool variable. Return true if successful.
  293. bool SetVariable(const String& name, bool value);
  294. /// Add/Set a float variable. Return true if successful.
  295. bool SetVariable(const String& name, float value);
  296. /// Add/Set a string variable. Return true if successful.
  297. bool SetVariable(const String& name, const String& value);
  298. /// Add/Set a string variable. Return true if successful.
  299. bool SetVariable(const char* name, const char* value);
  300. /// Add/Set an XPath query result set variable. Return true if successful.
  301. bool SetVariable(const String& name, const XPathResultSet& value);
  302. /// Set XPath query string and variable string. The variable string format is "name1:type1,name2:type2,..." where type is one of "Bool", "Float", "String", "ResultSet".
  303. bool SetQuery(const String& queryString, const String& variableString = String::EMPTY, bool bind = true);
  304. /// Clear by removing all variables and XPath query object.
  305. void Clear();
  306. /// Evaluate XPath query and expecting a boolean return value.
  307. bool EvaluateToBool(XMLElement element) const;
  308. /// Evaluate XPath query and expecting a float return value.
  309. float EvaluateToFloat(XMLElement element) const;
  310. /// Evaluate XPath query and expecting a string return value.
  311. String EvaluateToString(XMLElement element) const;
  312. /// Evaluate XPath query and expecting an XPath query result set as return value.
  313. /// Note: The XPathResultSet return value must be stored in a lhs variable to ensure the underlying xpath_node_set* is still valid while performing XPathResultSet::FirstResult(), XPathResultSet::operator [], and XMLElement::NextResult().
  314. XPathResultSet Evaluate(XMLElement element) const;
  315. /// Return query string.
  316. String GetQuery() const { return queryString_; }
  317. /// Return pugixml xpath_query.
  318. pugi::xpath_query* GetXPathQuery() const { return query_; }
  319. /// Return pugixml xpath_variable_set.
  320. pugi::xpath_variable_set* GetXPathVariableSet() const { return variables_; }
  321. private:
  322. /// XPath query string.
  323. String queryString_;
  324. /// Pugixml xpath_query.
  325. pugi::xpath_query* query_;
  326. /// Pugixml xpath_variable_set.
  327. pugi::xpath_variable_set* variables_;
  328. };
  329. }