The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

761 lines
33KB

  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. XmlElement (String::CharPointerType tagNameBegin, String::CharPointerType tagNameEnd);
  120. /** Creates a (deep) copy of another element. */
  121. XmlElement (const XmlElement&);
  122. /** Creates a (deep) copy of another element. */
  123. XmlElement& operator= (const XmlElement&);
  124. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  125. XmlElement (XmlElement&&) noexcept;
  126. XmlElement& operator= (XmlElement&&) noexcept;
  127. #endif
  128. /** Deleting an XmlElement will also delete all of its child elements. */
  129. ~XmlElement() noexcept;
  130. //==============================================================================
  131. /** Compares two XmlElements to see if they contain the same text and attiributes.
  132. The elements are only considered equivalent if they contain the same attiributes
  133. with the same values, and have the same sub-nodes.
  134. @param other the other element to compare to
  135. @param ignoreOrderOfAttributes if true, this means that two elements with the
  136. same attributes in a different order will be
  137. considered the same; if false, the attributes must
  138. be in the same order as well
  139. */
  140. bool isEquivalentTo (const XmlElement* other,
  141. bool ignoreOrderOfAttributes) const noexcept;
  142. //==============================================================================
  143. /** Returns an XML text document that represents this element.
  144. The string returned can be parsed to recreate the same XmlElement that
  145. was used to create it.
  146. @param dtdToUse the DTD to add to the document
  147. @param allOnOneLine if true, this means that the document will not contain any
  148. linefeeds, so it'll be smaller but not very easy to read.
  149. @param includeXmlHeader whether to add the "<?xml version..etc" line at the start of the
  150. document
  151. @param encodingType the character encoding format string to put into the xml
  152. header
  153. @param lineWrapLength the line length that will be used before items get placed on
  154. a new line. This isn't an absolute maximum length, it just
  155. determines how lists of attributes get broken up
  156. @see writeToStream, writeToFile
  157. */
  158. String createDocument (StringRef dtdToUse,
  159. bool allOnOneLine = false,
  160. bool includeXmlHeader = true,
  161. StringRef encodingType = "UTF-8",
  162. int lineWrapLength = 60) const;
  163. /** Writes the document to a stream as UTF-8.
  164. @param output the stream to write to
  165. @param dtdToUse the DTD to add to the document
  166. @param allOnOneLine if true, this means that the document will not contain any
  167. linefeeds, so it'll be smaller but not very easy to read.
  168. @param includeXmlHeader whether to add the "<?xml version..etc" line at the start of the
  169. document
  170. @param encodingType the character encoding format string to put into the xml
  171. header
  172. @param lineWrapLength the line length that will be used before items get placed on
  173. a new line. This isn't an absolute maximum length, it just
  174. determines how lists of attributes get broken up
  175. @see writeToFile, createDocument
  176. */
  177. void writeToStream (OutputStream& output,
  178. StringRef dtdToUse,
  179. bool allOnOneLine = false,
  180. bool includeXmlHeader = true,
  181. StringRef encodingType = "UTF-8",
  182. int lineWrapLength = 60) const;
  183. /** Writes the element to a file as an XML document.
  184. To improve safety in case something goes wrong while writing the file, this
  185. will actually write the document to a new temporary file in the same
  186. directory as the destination file, and if this succeeds, it will rename this
  187. new file as the destination file (overwriting any existing file that was there).
  188. @param destinationFile the file to write to. If this already exists, it will be
  189. overwritten.
  190. @param dtdToUse the DTD to add to the document
  191. @param encodingType the character encoding format string to put into the xml
  192. header
  193. @param lineWrapLength the line length that will be used before items get placed on
  194. a new line. This isn't an absolute maximum length, it just
  195. determines how lists of attributes get broken up
  196. @returns true if the file is written successfully; false if something goes wrong
  197. in the process
  198. @see createDocument
  199. */
  200. bool writeToFile (const File& destinationFile,
  201. StringRef dtdToUse,
  202. StringRef encodingType = "UTF-8",
  203. int lineWrapLength = 60) const;
  204. //==============================================================================
  205. /** Returns this element's tag type name.
  206. E.g. for an element such as \<MOOSE legs="4" antlers="2">, this would return "MOOSE".
  207. @see hasTagName
  208. */
  209. const String& getTagName() const noexcept { return tagName; }
  210. /** Returns the namespace portion of the tag-name, or an empty string if none is specified. */
  211. String getNamespace() const;
  212. /** Returns the part of the tag-name that follows any namespace declaration. */
  213. String getTagNameWithoutNamespace() const;
  214. /** Tests whether this element has a particular tag name.
  215. @param possibleTagName the tag name you're comparing it with
  216. @see getTagName
  217. */
  218. bool hasTagName (StringRef possibleTagName) const noexcept;
  219. /** Tests whether this element has a particular tag name, ignoring any XML namespace prefix.
  220. So a test for e.g. "xyz" will return true for "xyz" and also "foo:xyz", "bar::xyz", etc.
  221. @see getTagName
  222. */
  223. bool hasTagNameIgnoringNamespace (StringRef possibleTagName) const;
  224. //==============================================================================
  225. /** Returns the number of XML attributes this element contains.
  226. E.g. for an element such as \<MOOSE legs="4" antlers="2">, this would
  227. return 2.
  228. */
  229. int getNumAttributes() const noexcept;
  230. /** Returns the name of one of the elements attributes.
  231. E.g. for an element such as \<MOOSE legs="4" antlers="2">, then
  232. getAttributeName(1) would return "antlers".
  233. @see getAttributeValue, getStringAttribute
  234. */
  235. const String& getAttributeName (int attributeIndex) const noexcept;
  236. /** Returns the value of one of the elements attributes.
  237. E.g. for an element such as \<MOOSE legs="4" antlers="2">, then
  238. getAttributeName(1) would return "2".
  239. @see getAttributeName, getStringAttribute
  240. */
  241. const String& getAttributeValue (int attributeIndex) const noexcept;
  242. //==============================================================================
  243. // Attribute-handling methods..
  244. /** Checks whether the element contains an attribute with a certain name. */
  245. bool hasAttribute (StringRef attributeName) const noexcept;
  246. /** Returns the value of a named attribute.
  247. @param attributeName the name of the attribute to look up
  248. */
  249. const String& getStringAttribute (StringRef attributeName) const noexcept;
  250. /** Returns the value of a named attribute.
  251. @param attributeName the name of the attribute to look up
  252. @param defaultReturnValue a value to return if the element doesn't have an attribute
  253. with this name
  254. */
  255. String getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const;
  256. /** Compares the value of a named attribute with a value passed-in.
  257. @param attributeName the name of the attribute to look up
  258. @param stringToCompareAgainst the value to compare it with
  259. @param ignoreCase whether the comparison should be case-insensitive
  260. @returns true if the value of the attribute is the same as the string passed-in;
  261. false if it's different (or if no such attribute exists)
  262. */
  263. bool compareAttribute (StringRef attributeName,
  264. StringRef stringToCompareAgainst,
  265. bool ignoreCase = false) const noexcept;
  266. /** Returns the value of a named attribute as an integer.
  267. This will try to find the attribute and convert it to an integer (using
  268. the String::getIntValue() method).
  269. @param attributeName the name of the attribute to look up
  270. @param defaultReturnValue a value to return if the element doesn't have an attribute
  271. with this name
  272. @see setAttribute
  273. */
  274. int getIntAttribute (StringRef attributeName, int defaultReturnValue = 0) const;
  275. /** Returns the value of a named attribute as floating-point.
  276. This will try to find the attribute and convert it to an integer (using
  277. the String::getDoubleValue() method).
  278. @param attributeName the name of the attribute to look up
  279. @param defaultReturnValue a value to return if the element doesn't have an attribute
  280. with this name
  281. @see setAttribute
  282. */
  283. double getDoubleAttribute (StringRef attributeName, double defaultReturnValue = 0.0) const;
  284. /** Returns the value of a named attribute as a boolean.
  285. This will try to find the attribute and interpret it as a boolean. To do this,
  286. it'll return true if the value is "1", "true", "y", etc, or false for other
  287. values.
  288. @param attributeName the name of the attribute to look up
  289. @param defaultReturnValue a value to return if the element doesn't have an attribute
  290. with this name
  291. */
  292. bool getBoolAttribute (StringRef attributeName, bool defaultReturnValue = false) const;
  293. /** Adds a named attribute to the element.
  294. If the element already contains an attribute with this name, it's value will
  295. be updated to the new value. If there's no such attribute yet, a new one will
  296. be added.
  297. Note that there are other setAttribute() methods that take integers,
  298. doubles, etc. to make it easy to store numbers.
  299. @param attributeName the name of the attribute to set
  300. @param newValue the value to set it to
  301. @see removeAttribute
  302. */
  303. void setAttribute (const Identifier& attributeName, const String& newValue);
  304. /** Adds a named attribute to the element, setting it to an integer value.
  305. If the element already contains an attribute with this name, it's value will
  306. be updated to the new value. If there's no such attribute yet, a new one will
  307. be added.
  308. Note that there are other setAttribute() methods that take integers,
  309. doubles, etc. to make it easy to store numbers.
  310. @param attributeName the name of the attribute to set
  311. @param newValue the value to set it to
  312. */
  313. void setAttribute (const Identifier& attributeName, int newValue);
  314. /** Adds a named attribute to the element, setting it to a floating-point value.
  315. If the element already contains an attribute with this name, it's value will
  316. be updated to the new value. If there's no such attribute yet, a new one will
  317. be added.
  318. Note that there are other setAttribute() methods that take integers,
  319. doubles, etc. to make it easy to store numbers.
  320. @param attributeName the name of the attribute to set
  321. @param newValue the value to set it to
  322. */
  323. void setAttribute (const Identifier& attributeName, double newValue);
  324. /** Removes a named attribute from the element.
  325. @param attributeName the name of the attribute to remove
  326. @see removeAllAttributes
  327. */
  328. void removeAttribute (const Identifier& attributeName) noexcept;
  329. /** Removes all attributes from this element. */
  330. void removeAllAttributes() noexcept;
  331. //==============================================================================
  332. // Child element methods..
  333. /** Returns the first of this element's sub-elements.
  334. see getNextElement() for an example of how to iterate the sub-elements.
  335. @see forEachXmlChildElement
  336. */
  337. XmlElement* getFirstChildElement() const noexcept { return firstChildElement; }
  338. /** Returns the next of this element's siblings.
  339. This can be used for iterating an element's sub-elements, e.g.
  340. @code
  341. XmlElement* child = myXmlDocument->getFirstChildElement();
  342. while (child != nullptr)
  343. {
  344. ...do stuff with this child..
  345. child = child->getNextElement();
  346. }
  347. @endcode
  348. Note that when iterating the child elements, some of them might be
  349. text elements as well as XML tags - use isTextElement() to work this
  350. out.
  351. Also, it's much easier and neater to use this method indirectly via the
  352. forEachXmlChildElement macro.
  353. @returns the sibling element that follows this one, or zero if this is the last
  354. element in its parent
  355. @see getNextElement, isTextElement, forEachXmlChildElement
  356. */
  357. inline XmlElement* getNextElement() const noexcept { return nextListItem; }
  358. /** Returns the next of this element's siblings which has the specified tag
  359. name.
  360. This is like getNextElement(), but will scan through the list until it
  361. finds an element with the given tag name.
  362. @see getNextElement, forEachXmlChildElementWithTagName
  363. */
  364. XmlElement* getNextElementWithTagName (StringRef requiredTagName) const;
  365. /** Returns the number of sub-elements in this element.
  366. @see getChildElement
  367. */
  368. int getNumChildElements() const noexcept;
  369. /** Returns the sub-element at a certain index.
  370. It's not very efficient to iterate the sub-elements by index - see
  371. getNextElement() for an example of how best to iterate.
  372. @returns the n'th child of this element, or nullptr if the index is out-of-range
  373. @see getNextElement, isTextElement, getChildByName
  374. */
  375. XmlElement* getChildElement (int index) const noexcept;
  376. /** Returns the first sub-element with a given tag-name.
  377. @param tagNameToLookFor the tag name of the element you want to find
  378. @returns the first element with this tag name, or nullptr if none is found
  379. @see getNextElement, isTextElement, getChildElement, getChildByAttribute
  380. */
  381. XmlElement* getChildByName (StringRef tagNameToLookFor) const noexcept;
  382. /** Returns the first sub-element which has an attribute that matches the given value.
  383. @param attributeName the name of the attribute to check
  384. @param attributeValue the target value of the attribute
  385. @returns the first element with this attribute value, or nullptr if none is found
  386. @see getChildByName
  387. */
  388. XmlElement* getChildByAttribute (StringRef attributeName,
  389. StringRef attributeValue) const noexcept;
  390. //==============================================================================
  391. /** Appends an element to this element's list of children.
  392. Child elements are deleted automatically when their parent is deleted, so
  393. make sure the object that you pass in will not be deleted by anything else,
  394. and make sure it's not already the child of another element.
  395. Note that due to the XmlElement using a singly-linked-list, prependChildElement()
  396. is an O(1) operation, but addChildElement() is an O(N) operation - so if
  397. you're adding large number of elements, you may prefer to do so in reverse order!
  398. @see getFirstChildElement, getNextElement, getNumChildElements,
  399. getChildElement, removeChildElement
  400. */
  401. void addChildElement (XmlElement* newChildElement) noexcept;
  402. /** Inserts an element into this element's list of children.
  403. Child elements are deleted automatically when their parent is deleted, so
  404. make sure the object that you pass in will not be deleted by anything else,
  405. and make sure it's not already the child of another element.
  406. @param newChildElement the element to add
  407. @param indexToInsertAt the index at which to insert the new element - if this is
  408. below zero, it will be added to the end of the list
  409. @see addChildElement, insertChildElement
  410. */
  411. void insertChildElement (XmlElement* newChildElement,
  412. int indexToInsertAt) noexcept;
  413. /** Inserts an element at the beginning of this element's list of children.
  414. Child elements are deleted automatically when their parent is deleted, so
  415. make sure the object that you pass in will not be deleted by anything else,
  416. and make sure it's not already the child of another element.
  417. Note that due to the XmlElement using a singly-linked-list, prependChildElement()
  418. is an O(1) operation, but addChildElement() is an O(N) operation - so if
  419. you're adding large number of elements, you may prefer to do so in reverse order!
  420. @see addChildElement, insertChildElement
  421. */
  422. void prependChildElement (XmlElement* newChildElement) noexcept;
  423. /** Creates a new element with the given name and returns it, after adding it
  424. as a child element.
  425. This is a handy method that means that instead of writing this:
  426. @code
  427. XmlElement* newElement = new XmlElement ("foobar");
  428. myParentElement->addChildElement (newElement);
  429. @endcode
  430. ..you could just write this:
  431. @code
  432. XmlElement* newElement = myParentElement->createNewChildElement ("foobar");
  433. @endcode
  434. */
  435. XmlElement* createNewChildElement (StringRef tagName);
  436. /** Replaces one of this element's children with another node.
  437. If the current element passed-in isn't actually a child of this element,
  438. this will return false and the new one won't be added. Otherwise, the
  439. existing element will be deleted, replaced with the new one, and it
  440. will return true.
  441. */
  442. bool replaceChildElement (XmlElement* currentChildElement,
  443. XmlElement* newChildNode) noexcept;
  444. /** Removes a child element.
  445. @param childToRemove the child to look for and remove
  446. @param shouldDeleteTheChild if true, the child will be deleted, if false it'll
  447. just remove it
  448. */
  449. void removeChildElement (XmlElement* childToRemove,
  450. bool shouldDeleteTheChild) noexcept;
  451. /** Deletes all the child elements in the element.
  452. @see removeChildElement, deleteAllChildElementsWithTagName
  453. */
  454. void deleteAllChildElements() noexcept;
  455. /** Deletes all the child elements with a given tag name.
  456. @see removeChildElement
  457. */
  458. void deleteAllChildElementsWithTagName (StringRef tagName) noexcept;
  459. /** Returns true if the given element is a child of this one. */
  460. bool containsChildElement (const XmlElement* possibleChild) const noexcept;
  461. /** Recursively searches all sub-elements to find one that contains the specified
  462. child element.
  463. */
  464. XmlElement* findParentElementOf (const XmlElement* elementToLookFor) noexcept;
  465. //==============================================================================
  466. /** Sorts the child elements using a comparator.
  467. This will use a comparator object to sort the elements into order. The object
  468. passed must have a method of the form:
  469. @code
  470. int compareElements (const XmlElement* first, const XmlElement* second);
  471. @endcode
  472. ..and this method must return:
  473. - a value of < 0 if the first comes before the second
  474. - a value of 0 if the two objects are equivalent
  475. - a value of > 0 if the second comes before the first
  476. To improve performance, the compareElements() method can be declared as static or const.
  477. @param comparator the comparator to use for comparing elements.
  478. @param retainOrderOfEquivalentItems if this is true, then items which the comparator
  479. says are equivalent will be kept in the order in which they
  480. currently appear in the array. This is slower to perform, but
  481. may be important in some cases. If it's false, a faster algorithm
  482. is used, but equivalent elements may be rearranged.
  483. */
  484. template <class ElementComparator>
  485. void sortChildElements (ElementComparator& comparator,
  486. bool retainOrderOfEquivalentItems = false)
  487. {
  488. const int num = getNumChildElements();
  489. if (num > 1)
  490. {
  491. HeapBlock <XmlElement*> elems ((size_t) num);
  492. getChildElementsAsArray (elems);
  493. sortArray (comparator, (XmlElement**) elems, 0, num - 1, retainOrderOfEquivalentItems);
  494. reorderChildElements (elems, num);
  495. }
  496. }
  497. //==============================================================================
  498. /** Returns true if this element is a section of text.
  499. Elements can either be an XML tag element or a secton of text, so this
  500. is used to find out what kind of element this one is.
  501. @see getAllText, addTextElement, deleteAllTextElements
  502. */
  503. bool isTextElement() const noexcept;
  504. /** Returns the text for a text element.
  505. Note that if you have an element like this:
  506. @code<xyz>hello</xyz>@endcode
  507. then calling getText on the "xyz" element won't return "hello", because that is
  508. actually stored in a special text sub-element inside the xyz element. To get the
  509. "hello" string, you could either call getText on the (unnamed) sub-element, or
  510. use getAllSubText() to do this automatically.
  511. Note that leading and trailing whitespace will be included in the string - to remove
  512. if, just call String::trim() on the result.
  513. @see isTextElement, getAllSubText, getChildElementAllSubText
  514. */
  515. const String& getText() const noexcept;
  516. /** Sets the text in a text element.
  517. Note that this is only a valid call if this element is a text element. If it's
  518. not, then no action will be performed. If you're trying to add text inside a normal
  519. element, you probably want to use addTextElement() instead.
  520. */
  521. void setText (const String& newText);
  522. /** Returns all the text from this element's child nodes.
  523. This iterates all the child elements and when it finds text elements,
  524. it concatenates their text into a big string which it returns.
  525. E.g. @code<xyz>hello <x>there</x> world</xyz>@endcode
  526. if you called getAllSubText on the "xyz" element, it'd return "hello there world".
  527. Note that leading and trailing whitespace will be included in the string - to remove
  528. if, just call String::trim() on the result.
  529. @see isTextElement, getChildElementAllSubText, getText, addTextElement
  530. */
  531. String getAllSubText() const;
  532. /** Returns all the sub-text of a named child element.
  533. If there is a child element with the given tag name, this will return
  534. all of its sub-text (by calling getAllSubText() on it). If there is
  535. no such child element, this will return the default string passed-in.
  536. @see getAllSubText
  537. */
  538. String getChildElementAllSubText (StringRef childTagName,
  539. const String& defaultReturnValue) const;
  540. /** Appends a section of text to this element.
  541. @see isTextElement, getText, getAllSubText
  542. */
  543. void addTextElement (const String& text);
  544. /** Removes all the text elements from this element.
  545. @see isTextElement, getText, getAllSubText, addTextElement
  546. */
  547. void deleteAllTextElements() noexcept;
  548. /** Creates a text element that can be added to a parent element. */
  549. static XmlElement* createTextElement (const String& text);
  550. //==============================================================================
  551. private:
  552. struct XmlAttributeNode
  553. {
  554. XmlAttributeNode (const XmlAttributeNode&) noexcept;
  555. XmlAttributeNode (const Identifier&, const String&) noexcept;
  556. XmlAttributeNode (String::CharPointerType, String::CharPointerType);
  557. LinkedListPointer<XmlAttributeNode> nextListItem;
  558. Identifier name;
  559. String value;
  560. private:
  561. XmlAttributeNode& operator= (const XmlAttributeNode&) JUCE_DELETED_FUNCTION;
  562. };
  563. friend class XmlDocument;
  564. friend class LinkedListPointer<XmlAttributeNode>;
  565. friend class LinkedListPointer<XmlElement>;
  566. friend class LinkedListPointer<XmlElement>::Appender;
  567. friend class NamedValueSet;
  568. LinkedListPointer<XmlElement> nextListItem;
  569. LinkedListPointer<XmlElement> firstChildElement;
  570. LinkedListPointer<XmlAttributeNode> attributes;
  571. String tagName;
  572. XmlElement (int) noexcept;
  573. void copyChildrenAndAttributesFrom (const XmlElement&);
  574. void writeElementAsText (OutputStream&, int indentationLevel, int lineWrapLength) const;
  575. void getChildElementsAsArray (XmlElement**) const noexcept;
  576. void reorderChildElements (XmlElement**, int) noexcept;
  577. XmlAttributeNode* getAttribute (StringRef) const noexcept;
  578. // Sigh.. L"" or _T("") string literals are problematic in general, and really inappropriate
  579. // for XML tags. Use a UTF-8 encoded literal instead, or if you're really determined to use
  580. // UTF-16, cast it to a String and use the other constructor.
  581. XmlElement (const wchar_t*) JUCE_DELETED_FUNCTION;
  582. JUCE_LEAK_DETECTOR (XmlElement)
  583. };
  584. #endif // JUCE_XMLELEMENT_H_INCLUDED