juce_XmlElement.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_XMLELEMENT_H_INCLUDED
  22. #define JUCE_XMLELEMENT_H_INCLUDED
  23. //==============================================================================
  24. /** A handy macro to make it easy to iterate all the child elements in an XmlElement.
  25. The parentXmlElement should be a reference to the parent XML, and the childElementVariableName
  26. will be the name of a pointer to each child element.
  27. E.g. @code
  28. XmlElement* myParentXml = createSomeKindOfXmlDocument();
  29. forEachXmlChildElement (*myParentXml, child)
  30. {
  31. if (child->hasTagName ("FOO"))
  32. doSomethingWithXmlElement (child);
  33. }
  34. @endcode
  35. @see forEachXmlChildElementWithTagName
  36. */
  37. #define forEachXmlChildElement(parentXmlElement, childElementVariableName) \
  38. \
  39. for (juce::XmlElement* childElementVariableName = (parentXmlElement).getFirstChildElement(); \
  40. childElementVariableName != nullptr; \
  41. childElementVariableName = childElementVariableName->getNextElement())
  42. /** A macro that makes it easy to iterate all the child elements of an XmlElement
  43. which have a specified tag.
  44. This does the same job as the forEachXmlChildElement macro, but only for those
  45. elements that have a particular tag name.
  46. The parentXmlElement should be a reference to the parent XML, and the childElementVariableName
  47. will be the name of a pointer to each child element. The requiredTagName is the
  48. tag name to match.
  49. E.g. @code
  50. XmlElement* myParentXml = createSomeKindOfXmlDocument();
  51. forEachXmlChildElementWithTagName (*myParentXml, child, "MYTAG")
  52. {
  53. // the child object is now guaranteed to be a <MYTAG> element..
  54. doSomethingWithMYTAGElement (child);
  55. }
  56. @endcode
  57. @see forEachXmlChildElement
  58. */
  59. #define forEachXmlChildElementWithTagName(parentXmlElement, childElementVariableName, requiredTagName) \
  60. \
  61. for (juce::XmlElement* childElementVariableName = (parentXmlElement).getChildByName (requiredTagName); \
  62. childElementVariableName != nullptr; \
  63. childElementVariableName = childElementVariableName->getNextElementWithTagName (requiredTagName))
  64. //==============================================================================
  65. /** Used to build a tree of elements representing an XML document.
  66. An XML document can be parsed into a tree of XmlElements, each of which
  67. represents an XML tag structure, and which may itself contain other
  68. nested elements.
  69. An XmlElement can also be converted back into a text document, and has
  70. lots of useful methods for manipulating its attributes and sub-elements,
  71. so XmlElements can actually be used as a handy general-purpose data
  72. structure.
  73. Here's an example of parsing some elements: @code
  74. // check we're looking at the right kind of document..
  75. if (myElement->hasTagName ("ANIMALS"))
  76. {
  77. // now we'll iterate its sub-elements looking for 'giraffe' elements..
  78. forEachXmlChildElement (*myElement, e)
  79. {
  80. if (e->hasTagName ("GIRAFFE"))
  81. {
  82. // found a giraffe, so use some of its attributes..
  83. String giraffeName = e->getStringAttribute ("name");
  84. int giraffeAge = e->getIntAttribute ("age");
  85. bool isFriendly = e->getBoolAttribute ("friendly");
  86. }
  87. }
  88. }
  89. @endcode
  90. And here's an example of how to create an XML document from scratch: @code
  91. // create an outer node called "ANIMALS"
  92. XmlElement animalsList ("ANIMALS");
  93. for (int i = 0; i < numAnimals; ++i)
  94. {
  95. // create an inner element..
  96. XmlElement* giraffe = new XmlElement ("GIRAFFE");
  97. giraffe->setAttribute ("name", "nigel");
  98. giraffe->setAttribute ("age", 10);
  99. giraffe->setAttribute ("friendly", true);
  100. // ..and add our new element to the parent node
  101. animalsList.addChildElement (giraffe);
  102. }
  103. // now we can turn the whole thing into a text document..
  104. String myXmlDoc = animalsList.createDocument (String::empty);
  105. @endcode
  106. @see XmlDocument
  107. */
  108. class JUCE_API XmlElement
  109. {
  110. public:
  111. //==============================================================================
  112. /** Creates an XmlElement with this tag name. */
  113. explicit XmlElement (const String& tagName);
  114. /** Creates an XmlElement with this tag name. */
  115. explicit XmlElement (const char* tagName);
  116. /** Creates an XmlElement with this tag name. */
  117. explicit XmlElement (const Identifier& tagName);
  118. /** Creates an XmlElement with this tag name. */
  119. explicit XmlElement (StringRef tagName);
  120. /** Creates an XmlElement with this tag name. */
  121. XmlElement (String::CharPointerType tagNameBegin, String::CharPointerType tagNameEnd);
  122. /** Creates a (deep) copy of another element. */
  123. XmlElement (const XmlElement&);
  124. /** Creates a (deep) copy of another element. */
  125. XmlElement& operator= (const XmlElement&);
  126. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  127. XmlElement (XmlElement&&) noexcept;
  128. XmlElement& operator= (XmlElement&&) noexcept;
  129. #endif
  130. /** Deleting an XmlElement will also delete all of its child elements. */
  131. ~XmlElement() noexcept;
  132. //==============================================================================
  133. /** Compares two XmlElements to see if they contain the same text and attiributes.
  134. The elements are only considered equivalent if they contain the same attiributes
  135. with the same values, and have the same sub-nodes.
  136. @param other the other element to compare to
  137. @param ignoreOrderOfAttributes if true, this means that two elements with the
  138. same attributes in a different order will be
  139. considered the same; if false, the attributes must
  140. be in the same order as well
  141. */
  142. bool isEquivalentTo (const XmlElement* other,
  143. bool ignoreOrderOfAttributes) const noexcept;
  144. //==============================================================================
  145. /** Returns an XML text document that represents this element.
  146. The string returned can be parsed to recreate the same XmlElement that
  147. was used to create it.
  148. @param dtdToUse the DTD to add to the document
  149. @param allOnOneLine if true, this means that the document will not contain any
  150. linefeeds, so it'll be smaller but not very easy to read.
  151. @param includeXmlHeader whether to add the "<?xml version..etc" line at the start of the
  152. document
  153. @param encodingType the character encoding format string to put into the xml
  154. header
  155. @param lineWrapLength the line length that will be used before items get placed on
  156. a new line. This isn't an absolute maximum length, it just
  157. determines how lists of attributes get broken up
  158. @see writeToStream, writeToFile
  159. */
  160. String createDocument (StringRef dtdToUse,
  161. bool allOnOneLine = false,
  162. bool includeXmlHeader = true,
  163. StringRef encodingType = "UTF-8",
  164. int lineWrapLength = 60) const;
  165. /** Writes the document to a stream as UTF-8.
  166. @param output the stream to write to
  167. @param dtdToUse the DTD to add to the document
  168. @param allOnOneLine if true, this means that the document will not contain any
  169. linefeeds, so it'll be smaller but not very easy to read.
  170. @param includeXmlHeader whether to add the "<?xml version..etc" line at the start of the
  171. document
  172. @param encodingType the character encoding format string to put into the xml
  173. header
  174. @param lineWrapLength the line length that will be used before items get placed on
  175. a new line. This isn't an absolute maximum length, it just
  176. determines how lists of attributes get broken up
  177. @see writeToFile, createDocument
  178. */
  179. void writeToStream (OutputStream& output,
  180. StringRef dtdToUse,
  181. bool allOnOneLine = false,
  182. bool includeXmlHeader = true,
  183. StringRef encodingType = "UTF-8",
  184. int lineWrapLength = 60) const;
  185. /** Writes the element to a file as an XML document.
  186. To improve safety in case something goes wrong while writing the file, this
  187. will actually write the document to a new temporary file in the same
  188. directory as the destination file, and if this succeeds, it will rename this
  189. new file as the destination file (overwriting any existing file that was there).
  190. @param destinationFile the file to write to. If this already exists, it will be
  191. overwritten.
  192. @param dtdToUse the DTD to add to the document
  193. @param encodingType the character encoding format string to put into the xml
  194. header
  195. @param lineWrapLength the line length that will be used before items get placed on
  196. a new line. This isn't an absolute maximum length, it just
  197. determines how lists of attributes get broken up
  198. @returns true if the file is written successfully; false if something goes wrong
  199. in the process
  200. @see createDocument
  201. */
  202. bool writeToFile (const File& destinationFile,
  203. StringRef dtdToUse,
  204. StringRef encodingType = "UTF-8",
  205. int lineWrapLength = 60) const;
  206. //==============================================================================
  207. /** Returns this element's tag type name.
  208. E.g. for an element such as \<MOOSE legs="4" antlers="2">, this would return "MOOSE".
  209. @see hasTagName
  210. */
  211. const String& getTagName() const noexcept { return tagName; }
  212. /** Returns the namespace portion of the tag-name, or an empty string if none is specified. */
  213. String getNamespace() const;
  214. /** Returns the part of the tag-name that follows any namespace declaration. */
  215. String getTagNameWithoutNamespace() const;
  216. /** Tests whether this element has a particular tag name.
  217. @param possibleTagName the tag name you're comparing it with
  218. @see getTagName
  219. */
  220. bool hasTagName (StringRef possibleTagName) const noexcept;
  221. /** Tests whether this element has a particular tag name, ignoring any XML namespace prefix.
  222. So a test for e.g. "xyz" will return true for "xyz" and also "foo:xyz", "bar::xyz", etc.
  223. @see getTagName
  224. */
  225. bool hasTagNameIgnoringNamespace (StringRef possibleTagName) const;
  226. //==============================================================================
  227. /** Returns the number of XML attributes this element contains.
  228. E.g. for an element such as \<MOOSE legs="4" antlers="2">, this would
  229. return 2.
  230. */
  231. int getNumAttributes() const noexcept;
  232. /** Returns the name of one of the elements attributes.
  233. E.g. for an element such as \<MOOSE legs="4" antlers="2">, then
  234. getAttributeName(1) would return "antlers".
  235. @see getAttributeValue, getStringAttribute
  236. */
  237. const String& getAttributeName (int attributeIndex) const noexcept;
  238. /** Returns the value of one of the elements attributes.
  239. E.g. for an element such as \<MOOSE legs="4" antlers="2">, then
  240. getAttributeName(1) would return "2".
  241. @see getAttributeName, getStringAttribute
  242. */
  243. const String& getAttributeValue (int attributeIndex) const noexcept;
  244. //==============================================================================
  245. // Attribute-handling methods..
  246. /** Checks whether the element contains an attribute with a certain name. */
  247. bool hasAttribute (StringRef attributeName) const noexcept;
  248. /** Returns the value of a named attribute.
  249. @param attributeName the name of the attribute to look up
  250. */
  251. const String& getStringAttribute (StringRef attributeName) const noexcept;
  252. /** Returns the value of a named attribute.
  253. @param attributeName the name of the attribute to look up
  254. @param defaultReturnValue a value to return if the element doesn't have an attribute
  255. with this name
  256. */
  257. String getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const;
  258. /** Compares the value of a named attribute with a value passed-in.
  259. @param attributeName the name of the attribute to look up
  260. @param stringToCompareAgainst the value to compare it with
  261. @param ignoreCase whether the comparison should be case-insensitive
  262. @returns true if the value of the attribute is the same as the string passed-in;
  263. false if it's different (or if no such attribute exists)
  264. */
  265. bool compareAttribute (StringRef attributeName,
  266. StringRef stringToCompareAgainst,
  267. bool ignoreCase = false) const noexcept;
  268. /** Returns the value of a named attribute as an integer.
  269. This will try to find the attribute and convert it to an integer (using
  270. the String::getIntValue() method).
  271. @param attributeName the name of the attribute to look up
  272. @param defaultReturnValue a value to return if the element doesn't have an attribute
  273. with this name
  274. @see setAttribute
  275. */
  276. int getIntAttribute (StringRef attributeName, int defaultReturnValue = 0) const;
  277. /** Returns the value of a named attribute as floating-point.
  278. This will try to find the attribute and convert it to an integer (using
  279. the String::getDoubleValue() method).
  280. @param attributeName the name of the attribute to look up
  281. @param defaultReturnValue a value to return if the element doesn't have an attribute
  282. with this name
  283. @see setAttribute
  284. */
  285. double getDoubleAttribute (StringRef attributeName, double defaultReturnValue = 0.0) const;
  286. /** Returns the value of a named attribute as a boolean.
  287. This will try to find the attribute and interpret it as a boolean. To do this,
  288. it'll return true if the value is "1", "true", "y", etc, or false for other
  289. values.
  290. @param attributeName the name of the attribute to look up
  291. @param defaultReturnValue a value to return if the element doesn't have an attribute
  292. with this name
  293. */
  294. bool getBoolAttribute (StringRef attributeName, bool defaultReturnValue = false) const;
  295. /** Adds a named attribute to the element.
  296. If the element already contains an attribute with this name, it's value will
  297. be updated to the new value. If there's no such attribute yet, a new one will
  298. be added.
  299. Note that there are other setAttribute() methods that take integers,
  300. doubles, etc. to make it easy to store numbers.
  301. @param attributeName the name of the attribute to set
  302. @param newValue the value to set it to
  303. @see removeAttribute
  304. */
  305. void setAttribute (const Identifier& attributeName, const String& newValue);
  306. /** Adds a named attribute to the element, setting it to an integer value.
  307. If the element already contains an attribute with this name, it's value will
  308. be updated to the new value. If there's no such attribute yet, a new one will
  309. be added.
  310. Note that there are other setAttribute() methods that take integers,
  311. doubles, etc. to make it easy to store numbers.
  312. @param attributeName the name of the attribute to set
  313. @param newValue the value to set it to
  314. */
  315. void setAttribute (const Identifier& attributeName, int newValue);
  316. /** Adds a named attribute to the element, setting it to a floating-point value.
  317. If the element already contains an attribute with this name, it's value will
  318. be updated to the new value. If there's no such attribute yet, a new one will
  319. be added.
  320. Note that there are other setAttribute() methods that take integers,
  321. doubles, etc. to make it easy to store numbers.
  322. @param attributeName the name of the attribute to set
  323. @param newValue the value to set it to
  324. */
  325. void setAttribute (const Identifier& attributeName, double newValue);
  326. /** Removes a named attribute from the element.
  327. @param attributeName the name of the attribute to remove
  328. @see removeAllAttributes
  329. */
  330. void removeAttribute (const Identifier& attributeName) noexcept;
  331. /** Removes all attributes from this element. */
  332. void removeAllAttributes() noexcept;
  333. //==============================================================================
  334. // Child element methods..
  335. /** Returns the first of this element's sub-elements.
  336. see getNextElement() for an example of how to iterate the sub-elements.
  337. @see forEachXmlChildElement
  338. */
  339. XmlElement* getFirstChildElement() const noexcept { return firstChildElement; }
  340. /** Returns the next of this element's siblings.
  341. This can be used for iterating an element's sub-elements, e.g.
  342. @code
  343. XmlElement* child = myXmlDocument->getFirstChildElement();
  344. while (child != nullptr)
  345. {
  346. ...do stuff with this child..
  347. child = child->getNextElement();
  348. }
  349. @endcode
  350. Note that when iterating the child elements, some of them might be
  351. text elements as well as XML tags - use isTextElement() to work this
  352. out.
  353. Also, it's much easier and neater to use this method indirectly via the
  354. forEachXmlChildElement macro.
  355. @returns the sibling element that follows this one, or zero if this is the last
  356. element in its parent
  357. @see getNextElement, isTextElement, forEachXmlChildElement
  358. */
  359. inline XmlElement* getNextElement() const noexcept { return nextListItem; }
  360. /** Returns the next of this element's siblings which has the specified tag
  361. name.
  362. This is like getNextElement(), but will scan through the list until it
  363. finds an element with the given tag name.
  364. @see getNextElement, forEachXmlChildElementWithTagName
  365. */
  366. XmlElement* getNextElementWithTagName (StringRef requiredTagName) const;
  367. /** Returns the number of sub-elements in this element.
  368. @see getChildElement
  369. */
  370. int getNumChildElements() const noexcept;
  371. /** Returns the sub-element at a certain index.
  372. It's not very efficient to iterate the sub-elements by index - see
  373. getNextElement() for an example of how best to iterate.
  374. @returns the n'th child of this element, or nullptr if the index is out-of-range
  375. @see getNextElement, isTextElement, getChildByName
  376. */
  377. XmlElement* getChildElement (int index) const noexcept;
  378. /** Returns the first sub-element with a given tag-name.
  379. @param tagNameToLookFor the tag name of the element you want to find
  380. @returns the first element with this tag name, or nullptr if none is found
  381. @see getNextElement, isTextElement, getChildElement, getChildByAttribute
  382. */
  383. XmlElement* getChildByName (StringRef tagNameToLookFor) const noexcept;
  384. /** Returns the first sub-element which has an attribute that matches the given value.
  385. @param attributeName the name of the attribute to check
  386. @param attributeValue the target value of the attribute
  387. @returns the first element with this attribute value, or nullptr if none is found
  388. @see getChildByName
  389. */
  390. XmlElement* getChildByAttribute (StringRef attributeName,
  391. StringRef attributeValue) const noexcept;
  392. //==============================================================================
  393. /** Appends an element to this element's list of children.
  394. Child elements are deleted automatically when their parent is deleted, so
  395. make sure the object that you pass in will not be deleted by anything else,
  396. and make sure it's not already the child of another element.
  397. Note that due to the XmlElement using a singly-linked-list, prependChildElement()
  398. is an O(1) operation, but addChildElement() is an O(N) operation - so if
  399. you're adding large number of elements, you may prefer to do so in reverse order!
  400. @see getFirstChildElement, getNextElement, getNumChildElements,
  401. getChildElement, removeChildElement
  402. */
  403. void addChildElement (XmlElement* newChildElement) noexcept;
  404. /** Inserts an element into this element's list of children.
  405. Child elements are deleted automatically when their parent is deleted, so
  406. make sure the object that you pass in will not be deleted by anything else,
  407. and make sure it's not already the child of another element.
  408. @param newChildElement the element to add
  409. @param indexToInsertAt the index at which to insert the new element - if this is
  410. below zero, it will be added to the end of the list
  411. @see addChildElement, insertChildElement
  412. */
  413. void insertChildElement (XmlElement* newChildElement,
  414. int indexToInsertAt) noexcept;
  415. /** Inserts an element at the beginning of this element's list of children.
  416. Child elements are deleted automatically when their parent is deleted, so
  417. make sure the object that you pass in will not be deleted by anything else,
  418. and make sure it's not already the child of another element.
  419. Note that due to the XmlElement using a singly-linked-list, prependChildElement()
  420. is an O(1) operation, but addChildElement() is an O(N) operation - so if
  421. you're adding large number of elements, you may prefer to do so in reverse order!
  422. @see addChildElement, insertChildElement
  423. */
  424. void prependChildElement (XmlElement* newChildElement) noexcept;
  425. /** Creates a new element with the given name and returns it, after adding it
  426. as a child element.
  427. This is a handy method that means that instead of writing this:
  428. @code
  429. XmlElement* newElement = new XmlElement ("foobar");
  430. myParentElement->addChildElement (newElement);
  431. @endcode
  432. ..you could just write this:
  433. @code
  434. XmlElement* newElement = myParentElement->createNewChildElement ("foobar");
  435. @endcode
  436. */
  437. XmlElement* createNewChildElement (StringRef tagName);
  438. /** Replaces one of this element's children with another node.
  439. If the current element passed-in isn't actually a child of this element,
  440. this will return false and the new one won't be added. Otherwise, the
  441. existing element will be deleted, replaced with the new one, and it
  442. will return true.
  443. */
  444. bool replaceChildElement (XmlElement* currentChildElement,
  445. XmlElement* newChildNode) noexcept;
  446. /** Removes a child element.
  447. @param childToRemove the child to look for and remove
  448. @param shouldDeleteTheChild if true, the child will be deleted, if false it'll
  449. just remove it
  450. */
  451. void removeChildElement (XmlElement* childToRemove,
  452. bool shouldDeleteTheChild) noexcept;
  453. /** Deletes all the child elements in the element.
  454. @see removeChildElement, deleteAllChildElementsWithTagName
  455. */
  456. void deleteAllChildElements() noexcept;
  457. /** Deletes all the child elements with a given tag name.
  458. @see removeChildElement
  459. */
  460. void deleteAllChildElementsWithTagName (StringRef tagName) noexcept;
  461. /** Returns true if the given element is a child of this one. */
  462. bool containsChildElement (const XmlElement* possibleChild) const noexcept;
  463. /** Recursively searches all sub-elements to find one that contains the specified
  464. child element.
  465. */
  466. XmlElement* findParentElementOf (const XmlElement* elementToLookFor) noexcept;
  467. //==============================================================================
  468. /** Sorts the child elements using a comparator.
  469. This will use a comparator object to sort the elements into order. The object
  470. passed must have a method of the form:
  471. @code
  472. int compareElements (const XmlElement* first, const XmlElement* second);
  473. @endcode
  474. ..and this method must return:
  475. - a value of < 0 if the first comes before the second
  476. - a value of 0 if the two objects are equivalent
  477. - a value of > 0 if the second comes before the first
  478. To improve performance, the compareElements() method can be declared as static or const.
  479. @param comparator the comparator to use for comparing elements.
  480. @param retainOrderOfEquivalentItems if this is true, then items which the comparator
  481. says are equivalent will be kept in the order in which they
  482. currently appear in the array. This is slower to perform, but
  483. may be important in some cases. If it's false, a faster algorithm
  484. is used, but equivalent elements may be rearranged.
  485. */
  486. template <class ElementComparator>
  487. void sortChildElements (ElementComparator& comparator,
  488. bool retainOrderOfEquivalentItems = false)
  489. {
  490. const int num = getNumChildElements();
  491. if (num > 1)
  492. {
  493. HeapBlock <XmlElement*> elems ((size_t) num);
  494. getChildElementsAsArray (elems);
  495. sortArray (comparator, (XmlElement**) elems, 0, num - 1, retainOrderOfEquivalentItems);
  496. reorderChildElements (elems, num);
  497. }
  498. }
  499. //==============================================================================
  500. /** Returns true if this element is a section of text.
  501. Elements can either be an XML tag element or a secton of text, so this
  502. is used to find out what kind of element this one is.
  503. @see getAllText, addTextElement, deleteAllTextElements
  504. */
  505. bool isTextElement() const noexcept;
  506. /** Returns the text for a text element.
  507. Note that if you have an element like this:
  508. @code<xyz>hello</xyz>@endcode
  509. then calling getText on the "xyz" element won't return "hello", because that is
  510. actually stored in a special text sub-element inside the xyz element. To get the
  511. "hello" string, you could either call getText on the (unnamed) sub-element, or
  512. use getAllSubText() to do this automatically.
  513. Note that leading and trailing whitespace will be included in the string - to remove
  514. if, just call String::trim() on the result.
  515. @see isTextElement, getAllSubText, getChildElementAllSubText
  516. */
  517. const String& getText() const noexcept;
  518. /** Sets the text in a text element.
  519. Note that this is only a valid call if this element is a text element. If it's
  520. not, then no action will be performed. If you're trying to add text inside a normal
  521. element, you probably want to use addTextElement() instead.
  522. */
  523. void setText (const String& newText);
  524. /** Returns all the text from this element's child nodes.
  525. This iterates all the child elements and when it finds text elements,
  526. it concatenates their text into a big string which it returns.
  527. E.g. @code<xyz>hello <x>there</x> world</xyz>@endcode
  528. if you called getAllSubText on the "xyz" element, it'd return "hello there world".
  529. Note that leading and trailing whitespace will be included in the string - to remove
  530. if, just call String::trim() on the result.
  531. @see isTextElement, getChildElementAllSubText, getText, addTextElement
  532. */
  533. String getAllSubText() const;
  534. /** Returns all the sub-text of a named child element.
  535. If there is a child element with the given tag name, this will return
  536. all of its sub-text (by calling getAllSubText() on it). If there is
  537. no such child element, this will return the default string passed-in.
  538. @see getAllSubText
  539. */
  540. String getChildElementAllSubText (StringRef childTagName,
  541. const String& defaultReturnValue) const;
  542. /** Appends a section of text to this element.
  543. @see isTextElement, getText, getAllSubText
  544. */
  545. void addTextElement (const String& text);
  546. /** Removes all the text elements from this element.
  547. @see isTextElement, getText, getAllSubText, addTextElement
  548. */
  549. void deleteAllTextElements() noexcept;
  550. /** Creates a text element that can be added to a parent element. */
  551. static XmlElement* createTextElement (const String& text);
  552. //==============================================================================
  553. private:
  554. struct XmlAttributeNode
  555. {
  556. XmlAttributeNode (const XmlAttributeNode&) noexcept;
  557. XmlAttributeNode (const Identifier&, const String&) noexcept;
  558. XmlAttributeNode (String::CharPointerType, String::CharPointerType);
  559. LinkedListPointer<XmlAttributeNode> nextListItem;
  560. Identifier name;
  561. String value;
  562. private:
  563. XmlAttributeNode& operator= (const XmlAttributeNode&) JUCE_DELETED_FUNCTION;
  564. };
  565. friend class XmlDocument;
  566. friend class LinkedListPointer<XmlAttributeNode>;
  567. friend class LinkedListPointer<XmlElement>;
  568. friend class LinkedListPointer<XmlElement>::Appender;
  569. friend class NamedValueSet;
  570. LinkedListPointer<XmlElement> nextListItem;
  571. LinkedListPointer<XmlElement> firstChildElement;
  572. LinkedListPointer<XmlAttributeNode> attributes;
  573. String tagName;
  574. XmlElement (int) noexcept;
  575. void copyChildrenAndAttributesFrom (const XmlElement&);
  576. void writeElementAsText (OutputStream&, int indentationLevel, int lineWrapLength) const;
  577. void getChildElementsAsArray (XmlElement**) const noexcept;
  578. void reorderChildElements (XmlElement**, int) noexcept;
  579. XmlAttributeNode* getAttribute (StringRef) const noexcept;
  580. // Sigh.. L"" or _T("") string literals are problematic in general, and really inappropriate
  581. // for XML tags. Use a UTF-8 encoded literal instead, or if you're really determined to use
  582. // UTF-16, cast it to a String and use the other constructor.
  583. XmlElement (const wchar_t*) JUCE_DELETED_FUNCTION;
  584. JUCE_LEAK_DETECTOR (XmlElement)
  585. };
  586. #endif // JUCE_XMLELEMENT_H_INCLUDED