XML.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. #include <functional>
  3. #include "Array.h"
  4. #include "Maybe.h"
  5. #include "RCPointer.h"
  6. #include "ReferenceCounter.h"
  7. namespace Framework
  8. {
  9. class Text;
  10. namespace XML
  11. {
  12. class Editor;
  13. //! An XML element of the form \code <name attribut1
  14. //! attribut2="value">text or children</name> \endcode
  15. class Element : public virtual ReferenceCounter
  16. {
  17. private:
  18. RCArray<Element>* children;
  19. RCArray<Text>* attributes;
  20. RCArray<Text>* attributeValues;
  21. Text* name;
  22. Text* text;
  23. Element* parent;
  24. public:
  25. //! Creates an empty XML element
  26. DLLEXPORT Element();
  27. //! Creates an XML element
  28. //! \param string Either the name of the element or an XML text
  29. //! to be parsed
  30. DLLEXPORT Element(Text string);
  31. //! Creates an XML element
  32. //! \param string Either the name of the element or an XML text
  33. //! to be parsed \param zParent A pointer to the parent element
  34. //! (without increased reference counter)
  35. DLLEXPORT Element(Text string, Element* zParent);
  36. DLLEXPORT ~Element();
  37. //! Changes an attribute or adds a new one
  38. //! \param attribut The name of the attribute
  39. //! \param value The value of the attribute
  40. DLLEXPORT void setAttribute(Text attribut, Text value);
  41. //! Removes an attribute
  42. //! \param attribut The name of the attribute
  43. DLLEXPORT void removeAttribute(Text attribut);
  44. //! Adds a child
  45. //! \param child The new child element
  46. DLLEXPORT void addChild(Element* child);
  47. //! Adds a child at the front
  48. //! \param child The new child element
  49. DLLEXPORT void addChildAtFront(Element* child);
  50. //! Removes a child
  51. //! \param zChild The child to remove
  52. DLLEXPORT void removeChild(Element* child);
  53. //! Removes the i-th child
  54. //! \param i The index of the child (starting at 0)
  55. DLLEXPORT void removeChild(int i);
  56. //! Removes all children
  57. DLLEXPORT void removeAllChilds();
  58. //! Removes a list of children
  59. //! \param childs All children to be removed
  60. DLLEXPORT void removeChilds(RCArray<Element>* childs);
  61. //! Removes this element from the parent element
  62. DLLEXPORT void remove();
  63. //! Sets the text in the element if it has no children
  64. //! \param text The text
  65. DLLEXPORT void setText(Text text);
  66. //! Returns the text in the element
  67. DLLEXPORT Text getText() const;
  68. //! Returns the number of children
  69. DLLEXPORT int getChildCount() const;
  70. /// <summary>
  71. /// returns the index of the zChild in the list of children or -1 if
  72. /// zChild is not a child of this element
  73. /// </summary>
  74. /// <param name="zChild">the child element to search for</param>
  75. /// <returns>the index of zChild in the list of children or -1 if
  76. /// zChild is not a child of this element</returns>
  77. DLLEXPORT int getChildIndex(Element* zChild) const;
  78. //! Returns the i-th child
  79. DLLEXPORT Element* getChild(int i) const;
  80. //! Returns the i-th child (without increased reference counter)
  81. DLLEXPORT Element* zChild(int i) const;
  82. //! Returns the parent element
  83. DLLEXPORT Element* getParent() const;
  84. //! Returns the parent element (without increased reference counter)
  85. DLLEXPORT Element* zParent() const;
  86. //! Returns an iterator to iterate through all children
  87. DLLEXPORT ArrayIterator<Element*> getChilds() const;
  88. //! Returns an editor for this element
  89. DLLEXPORT Editor select();
  90. //! Returns a selector containing all children
  91. DLLEXPORT Editor selectChildren() const;
  92. //! Returns a list of children that have a specific name
  93. //! \param name The name of the children
  94. DLLEXPORT Editor selectChildsByName(Text name) const;
  95. //! Returns a list of children that have a specific attribute
  96. //! \param attribute The name of the attribute
  97. DLLEXPORT Editor selectChildsByAttribute(Text attribute) const;
  98. //! Returns a list of children that have a specific attribute
  99. //! with a specific value \param attribute The name of the
  100. //! attribute \param value The value of the attribute
  101. DLLEXPORT Editor selectChildsByAttribute(
  102. Text attribute, Text value) const;
  103. //! Returns 1 if an attribute name exists, 0 otherwise
  104. DLLEXPORT bool hasAttribute(Text name) const;
  105. //! Returns the number of attributes
  106. DLLEXPORT int getAttributeCount() const;
  107. //! Returns the name of the i-th attribute
  108. DLLEXPORT Text getAttributeName(int i) const;
  109. //! Returns the value of the i-th attribute
  110. DLLEXPORT Text getAttributeValue(int i) const;
  111. //! Returns the value of an attribute
  112. //! \param attribut The name of the attribute
  113. DLLEXPORT const Text& getAttributeValue(const Text& attribut) const;
  114. //! Returns an iterator to iterate through all attribute names
  115. DLLEXPORT ArrayIterator<Text*> getAttributeNames() const;
  116. //! Returns an iterator to iterate through all attribute values
  117. DLLEXPORT ArrayIterator<Text*> getAttributeValues() const;
  118. //! Sets the name of the element
  119. DLLEXPORT void setName(Text name);
  120. //! Returns the name of the element
  121. DLLEXPORT Text getName() const;
  122. //! Generates an XML text containing this element and all children
  123. DLLEXPORT Text toString() const;
  124. //! Creates a copy without references to this object
  125. DLLEXPORT Element* dublicate() const;
  126. friend Editor;
  127. };
  128. //! An XML Editor that can edit multiple elements at once
  129. class Editor : public virtual ReferenceCounter
  130. {
  131. private:
  132. RCArray<Element>* elements;
  133. public:
  134. //! Creates a new XML Editor with a list of objects to be edited
  135. DLLEXPORT Editor(RCArray<Element>* elements);
  136. DLLEXPORT Editor(const Editor& e);
  137. DLLEXPORT ~Editor();
  138. /// <summary>
  139. /// returns the first element in the list
  140. /// </summary>
  141. /// <returns> the first element of a list or an empty object if no
  142. /// elements are present</returns>
  143. DLLEXPORT Maybe<RCPointer<Element>> getFirstElement() const;
  144. //! Changes an attribute or adds a new one (on all elements
  145. //! in the list) \param attribut The name of the attribute \param
  146. //! value The value of the attribute
  147. DLLEXPORT void setAttribute(Text attribut, Text value);
  148. //! Removes an attribute (on all elements in the list)
  149. //! \param attribut The name of the attribute
  150. DLLEXPORT void removeAttribute(Text attribut);
  151. //! Adds a child (on all elements in the list)
  152. //! \param child The new child element
  153. DLLEXPORT void addChild(Element* child);
  154. //! Removes a child (on all elements in the list)
  155. //! \param zChild The child to remove
  156. DLLEXPORT void removeChild(Element* child);
  157. //! Removes the i-th child (on all elements in the list)
  158. //! \param i The index of the child (starting at 0)
  159. DLLEXPORT void removeChild(int i);
  160. //! Removes all children (on all elements in the list)
  161. DLLEXPORT void removeAllChilds();
  162. //! Removes a list of children (on all elements in the list)
  163. //! \param childs All children to be removed
  164. DLLEXPORT void removeChilds(RCArray<Element>* childs);
  165. //! Removes this element from the parent element (on all elements
  166. //! in the list)
  167. DLLEXPORT void remove();
  168. //! Sets the text in the element if it has no children (on all
  169. //! elements in the list) \param text The text
  170. DLLEXPORT void setText(Text text);
  171. //! Returns an iterator through all elements
  172. DLLEXPORT ArrayIterator<Element*> begin();
  173. //! Returns the end of the iterator
  174. DLLEXPORT ArrayIterator<Element*> end();
  175. private:
  176. DLLEXPORT void selectAllElements(RCArray<Element>* zResult);
  177. public:
  178. //! Returns a selector containing all elements in this selector
  179. //! and recursively all children of the elements
  180. DLLEXPORT Editor selectAllElements();
  181. //! Returns a selector containing all children
  182. DLLEXPORT Editor selectChildren() const;
  183. //! Returns a selector containing all parents
  184. DLLEXPORT Editor selectParents() const;
  185. //! Returns a list of elements that have a specific name
  186. //! \param name The name of the children
  187. DLLEXPORT Editor whereNameEquals(Text name) const;
  188. //! Returns a list of elements that have a specific child
  189. //! \param name The name of the child
  190. DLLEXPORT Editor whereChildWithNameExists(Text name) const;
  191. //! Returns a list of elements that have a specific child
  192. //! \param attribute The name of the attribute
  193. DLLEXPORT Editor whereChildWithAttributeExists(
  194. Text attribute) const;
  195. //! Returns a list of elements that have a specific child
  196. //! \param attribute The name of the attribute \param value The
  197. //! value of the attribute
  198. DLLEXPORT Editor whereChildWithAttributeExists(
  199. Text attribute, Text value) const;
  200. //! Returns a list of elements that have a specific attribute
  201. //! \param attribute The name of the attribute
  202. DLLEXPORT Editor whereAttributeExists(Text attribute) const;
  203. //! Returns a list of elements that have a specific attribute
  204. //! with a specific value \param attribute The name of the
  205. //! attribute \param value The value of the attribute
  206. DLLEXPORT Editor whereAttributeEquals(
  207. Text attribute, Text value) const;
  208. //! Returns an Editor containing only the elements that are
  209. //! not in e \param e An Editor with elements that should not
  210. //! be included
  211. DLLEXPORT Editor without(Editor e) const;
  212. //! Calls a function for each element (takes an Element object
  213. //! without increased reference counter as argument) \param f The
  214. //! function
  215. DLLEXPORT void forEach(std::function<void(Element*)> f) const;
  216. //! Returns 1 if at least one element was found
  217. DLLEXPORT bool exists() const;
  218. //! Returns the number of selected elements
  219. DLLEXPORT int getSize() const;
  220. //! assignment operator
  221. DLLEXPORT Editor& operator=(const Editor& e);
  222. };
  223. } // namespace XML
  224. } // namespace Framework