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.

767 lines
33KB

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