XMLElement.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "Rect.h"
  26. #include "Ptr.h"
  27. #include "Variant.h"
  28. namespace pugi
  29. {
  30. struct xml_node_struct;
  31. }
  32. class XMLFile;
  33. /// Element in an XML file.
  34. class XMLElement
  35. {
  36. public:
  37. /// Construct null element.
  38. XMLElement();
  39. /// Construct with document and node pointers.
  40. XMLElement(XMLFile* file, pugi::xml_node_struct* node);
  41. /// Copy-construct from another element.
  42. XMLElement(const XMLElement& rhs);
  43. /// Destruct.
  44. ~XMLElement();
  45. /// Create a child element.
  46. XMLElement CreateChild(const String& name);
  47. /// Create a child element.
  48. XMLElement CreateChild(const char* name);
  49. /// Remove a child element. Return true if successful.
  50. bool RemoveChild(const XMLElement& element);
  51. /// Remove a child element by name. Return true if successful.
  52. bool RemoveChild(const String& name);
  53. /// Remove a child element by name. Return true if successful.
  54. bool RemoveChild(const char* name);
  55. /// Remove child elements of certain name, or all child elements if name is empty. Return true if successful.
  56. bool RemoveChildren(const String& name = String());
  57. /// Remove child elements of certain name, or all child elements if name is empty. Return true if successful.
  58. bool RemoveChildren(const char* name);
  59. /// %Set an attribute.
  60. bool SetAttribute(const String& name, const String& value);
  61. /// %Set an attribute.
  62. bool SetAttribute(const char* name, const char* value);
  63. /// %Set a bool attribute.
  64. bool SetBool(const String& name, bool value);
  65. /// %Set a BoundingBox attribute.
  66. bool SetBoundingBox(const BoundingBox& value);
  67. /// %Set a buffer attribute.
  68. bool SetBuffer(const String& name, const void* data, unsigned size);
  69. /// %Set a buffer attribute.
  70. bool SetBuffer(const String& name, const PODVector<unsigned char>& value);
  71. /// %Set a color attribute.
  72. bool SetColor(const String& name, const Color& value);
  73. /// %Set a float attribute.
  74. bool SetFloat(const String& name, float value);
  75. /// %Set an integer attribute.
  76. bool SetInt(const String& name, int value);
  77. /// %Set an IntRect attribute.
  78. bool SetIntRect(const String& name, const IntRect& value);
  79. /// %Set an IntVector2 attribute.
  80. bool SetIntVector2(const String& name, const IntVector2& value);
  81. /// %Set a Rect attribute.
  82. bool SetRect(const String& name, const Rect& value);
  83. /// %Set a quaternion attribute.
  84. bool SetQuaternion(const String& name, const Quaternion& value);
  85. /// %Set a string attribute.
  86. bool SetString(const String& name, const String& value);
  87. /// %Set a variant attribute.
  88. bool SetVariant(const Variant& value);
  89. /// %Set a variant attribute excluding the type.
  90. bool SetVariantValue(const Variant& value);
  91. /// %Set a resource reference attribute.
  92. bool SetResourceRef(const ResourceRef& value);
  93. /// %Set a resource referene list attribute.
  94. bool SetResourceRefList(const ResourceRefList& value);
  95. /// %Set a variant vector attribute. Creates child elements as necessary.
  96. bool SetVariantVector(const VariantVector& value);
  97. /// %Set a variant map attribute. Creates child elements as necessary.
  98. bool SetVariantMap(const VariantMap& value);
  99. /// %Set a Vector2 attribute.
  100. bool SetVector2(const String& name, const Vector2& value);
  101. /// %Set a Vector3 attribute.
  102. bool SetVector3(const String& name, const Vector3& value);
  103. /// %Set a Vector4 attribute.
  104. bool SetVector4(const String& name, const Vector4& value);
  105. /// Return whether does not refer to an element.
  106. bool IsNull() const { return node_ == 0; }
  107. /// Return whether refers to an element.
  108. bool NotNull() const { return node_ != 0; }
  109. /// Return true if refers to an element.
  110. operator bool () const { return node_ != 0; }
  111. /// Return element name.
  112. String GetName() const;
  113. /// Return whether has a child element.
  114. bool HasChild(const String& name) const;
  115. /// Return whether has a child element.
  116. bool HasChild(const char* name) const;
  117. /// Return child element, or null if missing.
  118. XMLElement GetChild(const String& name = String()) const;
  119. /// Return child element, or null if missing.
  120. XMLElement GetChild(const char* name) const;
  121. /// Return next sibling element.
  122. XMLElement GetNext(const String& name = String()) const;
  123. /// Return next sibling element.
  124. XMLElement GetNext(const char* name) const;
  125. /// Return parent element.
  126. XMLElement GetParent() const;
  127. /// Return number of attributes.
  128. unsigned GetNumAttributes() const;
  129. /// Return whether has an attribute.
  130. bool HasAttribute(const String& name) const;
  131. /// Return whether has an attribute.
  132. bool HasAttribute(const char* name) const;
  133. /// Return attribute, or empty if missing.
  134. String GetAttribute(const String& name) const;
  135. /// Return attribute, or empty if missing.
  136. const char* GetAttribute(const char* name) const;
  137. /// Return attribute in lowercase, or empty if missing.
  138. String GetAttributeLower(const String& name) const;
  139. /// Return attribute in lowercase, or empty if missing.
  140. String GetAttributeLower(const char* name) const;
  141. /// Return attribute in lowercase, or empty if missing.
  142. String GetAttributeUpper(const String& name) const;
  143. /// Return attribute in lowercase, or empty if missing.
  144. String GetAttributeUpper(const char* name) const;
  145. /// Return names of all attributes.
  146. Vector<String> GetAttributeNames() const;
  147. /// Return bool attribute, or false if missing.
  148. bool GetBool(const String& name) const;
  149. /// Return buffer attribute, or empty if missing.
  150. PODVector<unsigned char> GetBuffer(const String& name) const;
  151. /// Copy buffer attribute into a supplied buffer. Return true if buffer was large enough.
  152. bool GetBuffer(const String& name, void* dest, unsigned size) const;
  153. /// Return bounding box attribute, or empty if missing.
  154. BoundingBox GetBoundingBox() const;
  155. /// Return a color attribute, or default if missing.
  156. Color GetColor(const String& name) const;
  157. /// Return a float attribute, or zero if missing.
  158. float GetFloat(const String& name) const;
  159. /// Return an integer attribute, or zero if missing.
  160. int GetInt(const String& name) const;
  161. /// Return an IntRect attribute, or default if missing.
  162. IntRect GetIntRect(const String& name) const;
  163. /// Return an IntVector2 attribute, or default if missing.
  164. IntVector2 GetIntVector2(const String& name) const;
  165. /// Return a Rect attribute, or default if missing.
  166. Rect GetRect(const String& name) const;
  167. /// Return a quaternion attribute, or default if missing.
  168. Quaternion GetQuaternion(const String& name) const;
  169. /// Return a variant attribute, or empty if missing.
  170. Variant GetVariant() const;
  171. /// Return a variant attribute with static type.
  172. Variant GetVariantValue(VariantType type) const;
  173. /// Return a resource reference attribute, or empty if missing.
  174. ResourceRef GetResourceRef() const;
  175. /// Return a resource reference list attribute, or empty if missing.
  176. ResourceRefList GetResourceRefList() const;
  177. /// Return a variant vector attribute, or empty if missing.
  178. VariantVector GetVariantVector() const;
  179. /// Return a variant map attribute, or empty if missing.
  180. VariantMap GetVariantMap() const;
  181. /// Return a Vector2 attribute, or default if missing.
  182. Vector2 GetVector2(const String& name) const;
  183. /// Return a Vector3 attribute, or default if missing.
  184. Vector3 GetVector3(const String& name) const;
  185. /// Return a Vector4 attribute, or default if missing.
  186. Vector4 GetVector4(const String& name) const;
  187. /// Return any Vector attribute as Vector4. Missing coordinates will be zero.
  188. Vector4 GetVector(const String& name) const;
  189. /// Return XML file.
  190. XMLFile* GetFile() const;
  191. /// Return pugixml node.s
  192. pugi::xml_node_struct* GetNode() const { return node_; }
  193. private:
  194. /// XML file.
  195. WeakPtr<XMLFile> file_;
  196. /// Pugixml node.
  197. pugi::xml_node_struct* node_;
  198. };