XMLElement.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. }
  31. namespace Urho3D
  32. {
  33. class XMLFile;
  34. /// Element in an XML file.
  35. class XMLElement
  36. {
  37. public:
  38. /// Construct null element.
  39. XMLElement();
  40. /// Construct with document and node pointers.
  41. XMLElement(XMLFile* file, pugi::xml_node_struct* node);
  42. /// Copy-construct from another element.
  43. XMLElement(const XMLElement& rhs);
  44. /// Destruct.
  45. ~XMLElement();
  46. /// Create a child element.
  47. XMLElement CreateChild(const String& name);
  48. /// Create a child element.
  49. XMLElement CreateChild(const char* name);
  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. /// Set an attribute.
  61. bool SetAttribute(const String& name, const String& value);
  62. /// Set an attribute.
  63. bool SetAttribute(const char* name, const char* value);
  64. /// Set a bool attribute.
  65. bool SetBool(const String& name, bool value);
  66. /// Set a BoundingBox attribute.
  67. bool SetBoundingBox(const BoundingBox& value);
  68. /// Set a buffer attribute.
  69. bool SetBuffer(const String& name, const void* data, unsigned size);
  70. /// Set a buffer attribute.
  71. bool SetBuffer(const String& name, const PODVector<unsigned char>& value);
  72. /// Set a color attribute.
  73. bool SetColor(const String& name, const Color& value);
  74. /// Set a float attribute.
  75. bool SetFloat(const String& name, float value);
  76. /// Set an unsigned integer attribute.
  77. bool SetUInt(const String& name, unsigned value);
  78. /// Set an integer attribute.
  79. bool SetInt(const String& name, int value);
  80. /// Set an IntRect attribute.
  81. bool SetIntRect(const String& name, const IntRect& value);
  82. /// Set an IntVector2 attribute.
  83. bool SetIntVector2(const String& name, const IntVector2& value);
  84. /// Set a Rect attribute.
  85. bool SetRect(const String& name, const Rect& value);
  86. /// Set a quaternion attribute.
  87. bool SetQuaternion(const String& name, const Quaternion& value);
  88. /// Set a string attribute.
  89. bool SetString(const String& name, const String& value);
  90. /// Set a variant attribute.
  91. bool SetVariant(const Variant& value);
  92. /// Set a variant attribute excluding the type.
  93. bool SetVariantValue(const Variant& value);
  94. /// Set a resource reference attribute.
  95. bool SetResourceRef(const ResourceRef& value);
  96. /// Set a resource referene list attribute.
  97. bool SetResourceRefList(const ResourceRefList& value);
  98. /// Set a variant vector attribute. Creates child elements as necessary.
  99. bool SetVariantVector(const VariantVector& value);
  100. /// Set a variant map attribute. Creates child elements as necessary.
  101. bool SetVariantMap(const VariantMap& value);
  102. /// Set a Vector2 attribute.
  103. bool SetVector2(const String& name, const Vector2& value);
  104. /// Set a Vector3 attribute.
  105. bool SetVector3(const String& name, const Vector3& value);
  106. /// Set a Vector4 attribute.
  107. bool SetVector4(const String& name, const Vector4& value);
  108. /// Return whether does not refer to an element.
  109. bool IsNull() const { return node_ == 0; }
  110. /// Return whether refers to an element.
  111. bool NotNull() const { return node_ != 0; }
  112. /// Return true if refers to an element.
  113. operator bool () const { return node_ != 0; }
  114. /// Return element name.
  115. String GetName() const;
  116. /// Return whether has a child element.
  117. bool HasChild(const String& name) const;
  118. /// Return whether has a child element.
  119. bool HasChild(const char* name) const;
  120. /// Return child element, or null if missing.
  121. XMLElement GetChild(const String& name = String::EMPTY) const;
  122. /// Return child element, or null if missing.
  123. XMLElement GetChild(const char* name) const;
  124. /// Return next sibling element.
  125. XMLElement GetNext(const String& name = String::EMPTY) const;
  126. /// Return next sibling element.
  127. XMLElement GetNext(const char* name) const;
  128. /// Return parent element.
  129. XMLElement GetParent() const;
  130. /// Return number of attributes.
  131. unsigned GetNumAttributes() const;
  132. /// Return whether has an attribute.
  133. bool HasAttribute(const String& name) const;
  134. /// Return whether has an attribute.
  135. bool HasAttribute(const char* name) const;
  136. /// Return attribute, or empty if missing.
  137. String GetAttribute(const String& name) const;
  138. /// Return attribute, or empty if missing.
  139. const char* GetAttribute(const char* name) const;
  140. /// Return attribute in lowercase, or empty if missing.
  141. String GetAttributeLower(const String& name) const;
  142. /// Return attribute in lowercase, or empty if missing.
  143. String GetAttributeLower(const char* name) const;
  144. /// Return attribute in lowercase, or empty if missing.
  145. String GetAttributeUpper(const String& name) const;
  146. /// Return attribute in lowercase, or empty if missing.
  147. String GetAttributeUpper(const char* name) const;
  148. /// Return names of all attributes.
  149. Vector<String> GetAttributeNames() const;
  150. /// Return bool attribute, or false if missing.
  151. bool GetBool(const String& name) const;
  152. /// Return buffer attribute, or empty if missing.
  153. PODVector<unsigned char> GetBuffer(const String& name) const;
  154. /// Copy buffer attribute into a supplied buffer. Return true if buffer was large enough.
  155. bool GetBuffer(const String& name, void* dest, unsigned size) const;
  156. /// Return bounding box attribute, or empty if missing.
  157. BoundingBox GetBoundingBox() const;
  158. /// Return a color attribute, or default if missing.
  159. Color GetColor(const String& name) const;
  160. /// Return a float attribute, or zero if missing.
  161. float GetFloat(const String& name) const;
  162. /// Return an unsigned integer attribute, or zero if missing.
  163. unsigned GetUInt(const String& name) const;
  164. /// Return an integer attribute, or zero if missing.
  165. int GetInt(const String& name) const;
  166. /// Return an IntRect attribute, or default if missing.
  167. IntRect GetIntRect(const String& name) const;
  168. /// Return an IntVector2 attribute, or default if missing.
  169. IntVector2 GetIntVector2(const String& name) const;
  170. /// Return a Rect attribute, or default if missing.
  171. Rect GetRect(const String& name) const;
  172. /// Return a quaternion attribute, or default if missing.
  173. Quaternion GetQuaternion(const String& name) const;
  174. /// Return a variant attribute, or empty if missing.
  175. Variant GetVariant() const;
  176. /// Return a variant attribute with static type.
  177. Variant GetVariantValue(VariantType type) const;
  178. /// Return a resource reference attribute, or empty if missing.
  179. ResourceRef GetResourceRef() const;
  180. /// Return a resource reference list attribute, or empty if missing.
  181. ResourceRefList GetResourceRefList() const;
  182. /// Return a variant vector attribute, or empty if missing.
  183. VariantVector GetVariantVector() const;
  184. /// Return a variant map attribute, or empty if missing.
  185. VariantMap GetVariantMap() const;
  186. /// Return a Vector2 attribute, or default if missing.
  187. Vector2 GetVector2(const String& name) const;
  188. /// Return a Vector3 attribute, or default if missing.
  189. Vector3 GetVector3(const String& name) const;
  190. /// Return a Vector4 attribute, or default if missing.
  191. Vector4 GetVector4(const String& name) const;
  192. /// Return any Vector attribute as Vector4. Missing coordinates will be zero.
  193. Vector4 GetVector(const String& name) const;
  194. /// Return XML file.
  195. XMLFile* GetFile() const;
  196. /// Return pugixml node.s
  197. pugi::xml_node_struct* GetNode() const { return node_; }
  198. /// Empty XMLElement.
  199. static const XMLElement EMPTY;
  200. private:
  201. /// XML file.
  202. WeakPtr<XMLFile> file_;
  203. /// Pugixml node.
  204. pugi::xml_node_struct* node_;
  205. };
  206. }