XMLElement.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. //
  2. // Copyright (c) 2008-2020 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 "../Container/Ptr.h"
  24. #include "../Core/Variant.h"
  25. #include "../Math/BoundingBox.h"
  26. #include "../Math/Rect.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 URHO3D_API 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. /// Return the first child element with name or create if does not exist.
  61. XMLElement GetOrCreateChild(const String& name);
  62. /// Return the first child element with name or create if does not exist.
  63. XMLElement GetOrCreateChild(const char* name);
  64. /// Append element. If asCopy is set to true then original element is copied and appended, otherwise specified element is appended.
  65. bool AppendChild(XMLElement element, bool asCopy = false);
  66. /// Remove element from its parent.
  67. bool Remove();
  68. /// Remove a child element. Return true if successful.
  69. bool RemoveChild(const XMLElement& element);
  70. /// Remove a child element by name. Return true if successful.
  71. bool RemoveChild(const String& name);
  72. /// Remove a child element by name. Return true if successful.
  73. bool RemoveChild(const char* name);
  74. /// Remove child elements of certain name, or all child elements if name is empty. Return true if successful.
  75. bool RemoveChildren(const String& name = String::EMPTY);
  76. /// Remove child elements of certain name, or all child elements if name is empty. Return true if successful.
  77. bool RemoveChildren(const char* name);
  78. /// Remove an attribute by name. Return true if successful.
  79. bool RemoveAttribute(const String& name = String::EMPTY);
  80. /// Remove an attribute by name. Return true if successful.
  81. bool RemoveAttribute(const char* name);
  82. /// Select an element/attribute using XPath query.
  83. XMLElement SelectSingle(const String& query, pugi::xpath_variable_set* variables = nullptr) const;
  84. /// Select an element/attribute using XPath query.
  85. XMLElement SelectSinglePrepared(const XPathQuery& query) const;
  86. /// Select elements/attributes using XPath query.
  87. XPathResultSet Select(const String& query, pugi::xpath_variable_set* variables = nullptr) const;
  88. /// Select elements/attributes using XPath query.
  89. XPathResultSet SelectPrepared(const XPathQuery& query) const;
  90. /// Set the value for an inner node in the following format <node>value</node>.
  91. /// @property
  92. bool SetValue(const String& value);
  93. /// Set the value for an inner node in the following format <node>value</node>. Must be used on the <node> element.
  94. bool SetValue(const char* value);
  95. /// Set an attribute.
  96. bool SetAttribute(const String& name, const String& value);
  97. /// Set an attribute.
  98. bool SetAttribute(const char* name, const char* value);
  99. /// Set an attribute. Only valid if it is an attribute only XPath query result.
  100. bool SetAttribute(const String& value);
  101. /// Set an attribute. Only valid if it is an attribute only XPath query result.
  102. bool SetAttribute(const char* value);
  103. /// Set a bool attribute.
  104. bool SetBool(const String& name, bool value);
  105. /// Set a BoundingBox attribute.
  106. bool SetBoundingBox(const BoundingBox& value);
  107. /// Set a buffer attribute.
  108. bool SetBuffer(const String& name, const void* data, unsigned size);
  109. /// Set a buffer attribute.
  110. bool SetBuffer(const String& name, const PODVector<unsigned char>& value);
  111. /// Set a color attribute.
  112. bool SetColor(const String& name, const Color& value);
  113. /// Set a float attribute.
  114. bool SetFloat(const String& name, float value);
  115. /// Set a double attribute.
  116. bool SetDouble(const String& name, double value);
  117. /// Set an unsigned integer attribute.
  118. bool SetUInt(const String& name, unsigned value);
  119. /// Set an integer attribute.
  120. bool SetInt(const String& name, int value);
  121. /// Set an unsigned long long integer attribute.
  122. bool SetUInt64(const String& name, unsigned long long value);
  123. /// Set a long long integer attribute.
  124. bool SetInt64(const String& name, long long value);
  125. /// Set an IntRect attribute.
  126. bool SetIntRect(const String& name, const IntRect& value);
  127. /// Set an IntVector2 attribute.
  128. bool SetIntVector2(const String& name, const IntVector2& value);
  129. /// Set an IntVector3 attribute.
  130. bool SetIntVector3(const String& name, const IntVector3& value);
  131. /// Set a Rect attribute.
  132. bool SetRect(const String& name, const Rect& value);
  133. /// Set a quaternion attribute.
  134. bool SetQuaternion(const String& name, const Quaternion& value);
  135. /// Set a string attribute.
  136. bool SetString(const String& name, const String& value);
  137. /// Set a variant attribute.
  138. bool SetVariant(const Variant& value);
  139. /// Set a variant attribute excluding the type.
  140. bool SetVariantValue(const Variant& value);
  141. /// Set a resource reference attribute.
  142. bool SetResourceRef(const ResourceRef& value);
  143. /// Set a resource reference list attribute.
  144. bool SetResourceRefList(const ResourceRefList& value);
  145. /// Set a variant vector attribute. Creates child elements as necessary.
  146. bool SetVariantVector(const VariantVector& value);
  147. /// Set a string vector attribute. Creates child elements as necessary.
  148. bool SetStringVector(const StringVector& value);
  149. /// Set a variant map attribute. Creates child elements as necessary.
  150. bool SetVariantMap(const VariantMap& value);
  151. /// Set a Vector2 attribute.
  152. bool SetVector2(const String& name, const Vector2& value);
  153. /// Set a Vector3 attribute.
  154. bool SetVector3(const String& name, const Vector3& value);
  155. /// Set a Vector4 attribute.
  156. bool SetVector4(const String& name, const Vector4& value);
  157. /// Set a float, Vector or Matrix attribute stored in a variant.
  158. bool SetVectorVariant(const String& name, const Variant& value);
  159. /// Set a Matrix3 attribute.
  160. bool SetMatrix3(const String& name, const Matrix3& value);
  161. /// Set a Matrix3x4 attribute.
  162. bool SetMatrix3x4(const String& name, const Matrix3x4& value);
  163. /// Set a Matrix4 attribute.
  164. bool SetMatrix4(const String& name, const Matrix4& value);
  165. /// Return whether does not refer to an element or an XPath node.
  166. /// @property{get_isNull}
  167. bool IsNull() const;
  168. /// Return whether refers to an element or an XPath node.
  169. /// @property
  170. bool NotNull() const;
  171. /// Return true if refers to an element or an XPath node.
  172. explicit operator bool() const;
  173. /// Return element name (or attribute name if it is an attribute only XPath query result).
  174. /// @property
  175. String GetName() const;
  176. /// Return whether has a child element.
  177. bool HasChild(const String& name) const;
  178. /// Return whether has a child element.
  179. bool HasChild(const char* name) const;
  180. /// Return child element, or null if missing.
  181. XMLElement GetChild(const String& name = String::EMPTY) const;
  182. /// Return child element, or null if missing.
  183. XMLElement GetChild(const char* name) const;
  184. /// Return next sibling element.
  185. XMLElement GetNext(const String& name = String::EMPTY) const;
  186. /// Return next sibling element.
  187. XMLElement GetNext(const char* name) const;
  188. /// Return parent element.
  189. /// @property
  190. XMLElement GetParent() const;
  191. /// Return number of attributes.
  192. /// @property
  193. unsigned GetNumAttributes() const;
  194. /// Return whether has an attribute.
  195. bool HasAttribute(const String& name) const;
  196. /// Return whether has an attribute.
  197. bool HasAttribute(const char* name) const;
  198. /// Return inner value, or empty if missing for nodes like <node>value</node>.
  199. /// @property
  200. String GetValue() const;
  201. /// Return attribute, or empty if missing.
  202. String GetAttribute(const String& name = String::EMPTY) const;
  203. /// Return attribute, or empty if missing.
  204. String GetAttribute(const char* name) const;
  205. /// Return attribute as C string, or null if missing.
  206. const char* GetAttributeCString(const char* name) const;
  207. /// Return attribute in lowercase, or empty if missing.
  208. String GetAttributeLower(const String& name) const;
  209. /// Return attribute in lowercase, or empty if missing.
  210. String GetAttributeLower(const char* name) const;
  211. /// Return attribute in lowercase, or empty if missing.
  212. String GetAttributeUpper(const String& name) const;
  213. /// Return attribute in lowercase, or empty if missing.
  214. String GetAttributeUpper(const char* name) const;
  215. /// Return names of all attributes.
  216. Vector<String> GetAttributeNames() const;
  217. /// Return bool attribute, or false if missing.
  218. bool GetBool(const String& name) const;
  219. /// Return buffer attribute, or empty if missing.
  220. PODVector<unsigned char> GetBuffer(const String& name) const;
  221. /// Copy buffer attribute into a supplied buffer. Return true if buffer was large enough.
  222. bool GetBuffer(const String& name, void* dest, unsigned size) const;
  223. /// Return bounding box attribute, or empty if missing.
  224. BoundingBox GetBoundingBox() const;
  225. /// Return a color attribute, or default if missing.
  226. Color GetColor(const String& name) const;
  227. /// Return a float attribute, or zero if missing.
  228. float GetFloat(const String& name) const;
  229. /// Return a double attribute, or zero if missing.
  230. double GetDouble(const String& name) const;
  231. /// Return an unsigned integer attribute, or zero if missing.
  232. unsigned GetUInt(const String& name) const;
  233. /// Return an integer attribute, or zero if missing.
  234. int GetInt(const String& name) const;
  235. /// Return an unsigned long long integer attribute, or zero if missing.
  236. unsigned long long GetUInt64(const String& name) const;
  237. /// Return a long long integer attribute, or zero if missing.
  238. long long GetInt64(const String& name) const;
  239. /// Return an IntRect attribute, or default if missing.
  240. IntRect GetIntRect(const String& name) const;
  241. /// Return an IntVector2 attribute, or default if missing.
  242. IntVector2 GetIntVector2(const String& name) const;
  243. /// Return an IntVector3 attribute, or default if missing.
  244. IntVector3 GetIntVector3(const String& name) const;
  245. /// Return a Rect attribute, or default if missing.
  246. Rect GetRect(const String& name) const;
  247. /// Return a quaternion attribute, or default if missing.
  248. Quaternion GetQuaternion(const String& name) const;
  249. /// Return a variant attribute, or empty if missing.
  250. Variant GetVariant() const;
  251. /// Return a variant attribute with static type.
  252. Variant GetVariantValue(VariantType type) const;
  253. /// Return a resource reference attribute, or empty if missing.
  254. ResourceRef GetResourceRef() const;
  255. /// Return a resource reference list attribute, or empty if missing.
  256. ResourceRefList GetResourceRefList() const;
  257. /// Return a variant vector attribute, or empty if missing.
  258. VariantVector GetVariantVector() const;
  259. /// Return a string vector attribute, or empty if missing.
  260. StringVector GetStringVector() const;
  261. /// Return a variant map attribute, or empty if missing.
  262. VariantMap GetVariantMap() const;
  263. /// Return a Vector2 attribute, or zero vector if missing.
  264. Vector2 GetVector2(const String& name) const;
  265. /// Return a Vector3 attribute, or zero vector if missing.
  266. Vector3 GetVector3(const String& name) const;
  267. /// Return a Vector4 attribute, or zero vector if missing.
  268. Vector4 GetVector4(const String& name) const;
  269. /// Return any Vector attribute as Vector4. Missing coordinates will be zero.
  270. Vector4 GetVector(const String& name) const;
  271. /// Return a float, Vector or Matrix attribute as Variant.
  272. Variant GetVectorVariant(const String& name) const;
  273. /// Return a Matrix3 attribute, or zero matrix if missing.
  274. Matrix3 GetMatrix3(const String& name) const;
  275. /// Return a Matrix3x4 attribute, or zero matrix if missing.
  276. Matrix3x4 GetMatrix3x4(const String& name) const;
  277. /// Return a Matrix4 attribute, or zero matrix if missing.
  278. Matrix4 GetMatrix4(const String& name) const;
  279. /// Return XML file.
  280. /// @property
  281. XMLFile* GetFile() const;
  282. /// Return pugixml xml_node_struct.
  283. pugi::xml_node_struct* GetNode() const { return node_; }
  284. /// Return XPath query result set.
  285. const XPathResultSet* GetXPathResultSet() const { return xpathResultSet_; }
  286. /// Return pugixml xpath_node.
  287. const pugi::xpath_node* GetXPathNode() const { return xpathNode_; }
  288. /// Return current result index.
  289. unsigned GetXPathResultIndex() const { return xpathResultIndex_; }
  290. /// Return next XPath query result. Only valid when this instance of XMLElement is itself one of the query result in the result set.
  291. /// @property
  292. XMLElement NextResult() const;
  293. /// Empty XMLElement.
  294. static const XMLElement EMPTY;
  295. private:
  296. /// XML file.
  297. WeakPtr<XMLFile> file_;
  298. /// Pugixml node.
  299. pugi::xml_node_struct* node_;
  300. /// XPath query result set.
  301. const XPathResultSet* xpathResultSet_;
  302. /// Pugixml xpath_node.
  303. const pugi::xpath_node* xpathNode_;
  304. /// Current XPath query result index (used internally to advance to subsequent query result).
  305. mutable unsigned xpathResultIndex_;
  306. };
  307. /// XPath query result set.
  308. class URHO3D_API XPathResultSet
  309. {
  310. public:
  311. /// Construct empty result set.
  312. XPathResultSet();
  313. /// Construct with result set from XPath query.
  314. XPathResultSet(XMLFile* file, pugi::xpath_node_set* resultSet);
  315. /// Copy-construct.
  316. XPathResultSet(const XPathResultSet& rhs);
  317. /// Destruct.
  318. ~XPathResultSet();
  319. /// Assignment operator.
  320. XPathResultSet& operator =(const XPathResultSet& rhs);
  321. /// Return the n-th result in the set. Call XMLElement::GetNextResult() to get the subsequent result in the set.
  322. /// 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().
  323. XMLElement operator [](unsigned index) const;
  324. /// Return the first result in the set. Call XMLElement::GetNextResult() to get the subsequent result in the set.
  325. /// 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().
  326. /// @property
  327. XMLElement FirstResult();
  328. /// Return size of result set.
  329. /// @property
  330. unsigned Size() const;
  331. /// Return whether result set is empty.
  332. /// @property
  333. bool Empty() const;
  334. /// Return pugixml xpath_node_set.
  335. pugi::xpath_node_set* GetXPathNodeSet() const { return resultSet_; }
  336. private:
  337. /// XML file.
  338. WeakPtr<XMLFile> file_;
  339. /// Pugixml xpath_node_set.
  340. pugi::xpath_node_set* resultSet_;
  341. };
  342. /// XPath query.
  343. class URHO3D_API XPathQuery
  344. {
  345. public:
  346. /// Construct empty.
  347. XPathQuery();
  348. /// 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".
  349. explicit XPathQuery(const String& queryString, const String& variableString = String::EMPTY);
  350. /// Destruct.
  351. ~XPathQuery();
  352. /// Bind query object with variable set.
  353. void Bind();
  354. /// Add/Set a bool variable. Return true if successful.
  355. bool SetVariable(const String& name, bool value);
  356. /// Add/Set a float variable. Return true if successful.
  357. bool SetVariable(const String& name, float value);
  358. /// Add/Set a string variable. Return true if successful.
  359. bool SetVariable(const String& name, const String& value);
  360. /// Add/Set a string variable. Return true if successful.
  361. bool SetVariable(const char* name, const char* value);
  362. /// Add/Set an XPath query result set variable. Return true if successful.
  363. bool SetVariable(const String& name, const XPathResultSet& value);
  364. /// 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".
  365. bool SetQuery(const String& queryString, const String& variableString = String::EMPTY, bool bind = true);
  366. /// Clear by removing all variables and XPath query object.
  367. void Clear();
  368. /// Evaluate XPath query and expecting a boolean return value.
  369. bool EvaluateToBool(const XMLElement& element) const;
  370. /// Evaluate XPath query and expecting a float return value.
  371. float EvaluateToFloat(const XMLElement& element) const;
  372. /// Evaluate XPath query and expecting a string return value.
  373. String EvaluateToString(const XMLElement& element) const;
  374. /// Evaluate XPath query and expecting an XPath query result set as return value.
  375. /// 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().
  376. XPathResultSet Evaluate(const XMLElement& element) const;
  377. /// Return query string.
  378. /// @property
  379. String GetQuery() const { return queryString_; }
  380. /// Return pugixml xpath_query.
  381. pugi::xpath_query* GetXPathQuery() const { return query_.Get(); }
  382. /// Return pugixml xpath_variable_set.
  383. pugi::xpath_variable_set* GetXPathVariableSet() const { return variables_.Get(); }
  384. private:
  385. /// XPath query string.
  386. String queryString_;
  387. /// Pugixml xpath_query.
  388. UniquePtr<pugi::xpath_query> query_;
  389. /// Pugixml xpath_variable_set.
  390. UniquePtr<pugi::xpath_variable_set> variables_;
  391. };
  392. }