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.

XmlElement.h 31KB

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