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.

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