The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

762 lines
32KB

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