Audio plugin host https://kx.studio/carla
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.

776 lines
33KB

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