XMLElement.h 18 KB

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