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

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