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

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