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.

883 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. XmlElement::XmlAttributeNode::XmlAttributeNode (const XmlAttributeNode& other) noexcept
  22. : name (other.name),
  23. value (other.value)
  24. {
  25. }
  26. XmlElement::XmlAttributeNode::XmlAttributeNode (const Identifier& n, const String& v) noexcept
  27. : name (n), value (v)
  28. {
  29. #if JUCE_DEBUG
  30. // this checks whether the attribute name string contains any illegal characters..
  31. for (String::CharPointerType t (name.getCharPointer()); ! t.isEmpty(); ++t)
  32. jassert (t.isLetterOrDigit() || *t == '_' || *t == '-' || *t == ':');
  33. #endif
  34. }
  35. XmlElement::XmlAttributeNode::XmlAttributeNode (String::CharPointerType nameStart, String::CharPointerType nameEnd)
  36. : name (nameStart, nameEnd)
  37. {
  38. }
  39. //==============================================================================
  40. static void sanityCheckTagName (const String& tag)
  41. {
  42. (void) tag;
  43. // the tag name mustn't be empty, or it'll look like a text element!
  44. jassert (tag.containsNonWhitespaceChars())
  45. // The tag can't contain spaces or other characters that would create invalid XML!
  46. jassert (! tag.containsAnyOf (" <>/&(){}"));
  47. }
  48. XmlElement::XmlElement (const String& tag)
  49. : tagName (StringPool::getGlobalPool().getPooledString (tag))
  50. {
  51. sanityCheckTagName (tagName);
  52. }
  53. XmlElement::XmlElement (const char* tag)
  54. : tagName (StringPool::getGlobalPool().getPooledString (tag))
  55. {
  56. sanityCheckTagName (tagName);
  57. }
  58. XmlElement::XmlElement (const Identifier& tag)
  59. : tagName (tag.toString())
  60. {
  61. sanityCheckTagName (tagName);
  62. }
  63. XmlElement::XmlElement (String::CharPointerType tagNameStart, String::CharPointerType tagNameEnd)
  64. : tagName (StringPool::getGlobalPool().getPooledString (tagNameStart, tagNameEnd))
  65. {
  66. sanityCheckTagName (tagName);
  67. }
  68. XmlElement::XmlElement (int /*dummy*/) noexcept
  69. {
  70. }
  71. XmlElement::XmlElement (const XmlElement& other)
  72. : tagName (other.tagName)
  73. {
  74. copyChildrenAndAttributesFrom (other);
  75. }
  76. XmlElement& XmlElement::operator= (const XmlElement& other)
  77. {
  78. if (this != &other)
  79. {
  80. removeAllAttributes();
  81. deleteAllChildElements();
  82. tagName = other.tagName;
  83. copyChildrenAndAttributesFrom (other);
  84. }
  85. return *this;
  86. }
  87. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  88. XmlElement::XmlElement (XmlElement&& other) noexcept
  89. : nextListItem (static_cast<LinkedListPointer<XmlElement>&&> (other.nextListItem)),
  90. firstChildElement (static_cast<LinkedListPointer<XmlElement>&&> (other.firstChildElement)),
  91. attributes (static_cast<LinkedListPointer<XmlAttributeNode>&&> (other.attributes)),
  92. tagName (static_cast<String&&> (other.tagName))
  93. {
  94. }
  95. XmlElement& XmlElement::operator= (XmlElement&& other) noexcept
  96. {
  97. jassert (this != &other); // hopefully the compiler should make this situation impossible!
  98. removeAllAttributes();
  99. deleteAllChildElements();
  100. nextListItem = static_cast<LinkedListPointer<XmlElement>&&> (other.nextListItem);
  101. firstChildElement = static_cast<LinkedListPointer<XmlElement>&&> (other.firstChildElement);
  102. attributes = static_cast<LinkedListPointer<XmlAttributeNode>&&> (other.attributes);
  103. tagName = static_cast<String&&> (other.tagName);
  104. return *this;
  105. }
  106. #endif
  107. void XmlElement::copyChildrenAndAttributesFrom (const XmlElement& other)
  108. {
  109. jassert (firstChildElement.get() == nullptr);
  110. firstChildElement.addCopyOfList (other.firstChildElement);
  111. jassert (attributes.get() == nullptr);
  112. attributes.addCopyOfList (other.attributes);
  113. }
  114. XmlElement::~XmlElement() noexcept
  115. {
  116. firstChildElement.deleteAll();
  117. attributes.deleteAll();
  118. }
  119. //==============================================================================
  120. namespace XmlOutputFunctions
  121. {
  122. #if 0 // (These functions are just used to generate the lookup table used below)
  123. bool isLegalXmlCharSlow (const juce_wchar character) noexcept
  124. {
  125. if ((character >= 'a' && character <= 'z')
  126. || (character >= 'A' && character <= 'Z')
  127. || (character >= '0' && character <= '9'))
  128. return true;
  129. const char* t = " .,;:-()_+=?!'#@[]/\\*%~{}$|";
  130. do
  131. {
  132. if (((juce_wchar) (uint8) *t) == character)
  133. return true;
  134. }
  135. while (*++t != 0);
  136. return false;
  137. }
  138. void generateLegalCharLookupTable()
  139. {
  140. uint8 n[32] = { 0 };
  141. for (int i = 0; i < 256; ++i)
  142. if (isLegalXmlCharSlow (i))
  143. n[i >> 3] |= (1 << (i & 7));
  144. String s;
  145. for (int i = 0; i < 32; ++i)
  146. s << (int) n[i] << ", ";
  147. DBG (s);
  148. }
  149. #endif
  150. static bool isLegalXmlChar (const uint32 c) noexcept
  151. {
  152. static const unsigned char legalChars[] = { 0, 0, 0, 0, 187, 255, 255, 175, 255,
  153. 255, 255, 191, 254, 255, 255, 127 };
  154. return c < sizeof (legalChars) * 8
  155. && (legalChars [c >> 3] & (1 << (c & 7))) != 0;
  156. }
  157. static void escapeIllegalXmlChars (OutputStream& outputStream, const String& text, const bool changeNewLines)
  158. {
  159. String::CharPointerType t (text.getCharPointer());
  160. for (;;)
  161. {
  162. const uint32 character = (uint32) t.getAndAdvance();
  163. if (character == 0)
  164. break;
  165. if (isLegalXmlChar (character))
  166. {
  167. outputStream << (char) character;
  168. }
  169. else
  170. {
  171. switch (character)
  172. {
  173. case '&': outputStream << "&amp;"; break;
  174. case '"': outputStream << "&quot;"; break;
  175. case '>': outputStream << "&gt;"; break;
  176. case '<': outputStream << "&lt;"; break;
  177. case '\n':
  178. case '\r':
  179. if (! changeNewLines)
  180. {
  181. outputStream << (char) character;
  182. break;
  183. }
  184. // Note: deliberate fall-through here!
  185. default:
  186. outputStream << "&#" << ((int) character) << ';';
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. static void writeSpaces (OutputStream& out, const size_t numSpaces)
  193. {
  194. out.writeRepeatedByte (' ', numSpaces);
  195. }
  196. }
  197. void XmlElement::writeElementAsText (OutputStream& outputStream,
  198. const int indentationLevel,
  199. const int lineWrapLength) const
  200. {
  201. using namespace XmlOutputFunctions;
  202. if (indentationLevel >= 0)
  203. writeSpaces (outputStream, (size_t) indentationLevel);
  204. if (! isTextElement())
  205. {
  206. outputStream.writeByte ('<');
  207. outputStream << tagName;
  208. {
  209. const size_t attIndent = (size_t) (indentationLevel + tagName.length() + 1);
  210. int lineLen = 0;
  211. for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  212. {
  213. if (lineLen > lineWrapLength && indentationLevel >= 0)
  214. {
  215. outputStream << newLine;
  216. writeSpaces (outputStream, attIndent);
  217. lineLen = 0;
  218. }
  219. const int64 startPos = outputStream.getPosition();
  220. outputStream.writeByte (' ');
  221. outputStream << att->name;
  222. outputStream.write ("=\"", 2);
  223. escapeIllegalXmlChars (outputStream, att->value, true);
  224. outputStream.writeByte ('"');
  225. lineLen += (int) (outputStream.getPosition() - startPos);
  226. }
  227. }
  228. if (firstChildElement != nullptr)
  229. {
  230. outputStream.writeByte ('>');
  231. bool lastWasTextNode = false;
  232. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  233. {
  234. if (child->isTextElement())
  235. {
  236. escapeIllegalXmlChars (outputStream, child->getText(), false);
  237. lastWasTextNode = true;
  238. }
  239. else
  240. {
  241. if (indentationLevel >= 0 && ! lastWasTextNode)
  242. outputStream << newLine;
  243. child->writeElementAsText (outputStream,
  244. lastWasTextNode ? 0 : (indentationLevel + (indentationLevel >= 0 ? 2 : 0)), lineWrapLength);
  245. lastWasTextNode = false;
  246. }
  247. }
  248. if (indentationLevel >= 0 && ! lastWasTextNode)
  249. {
  250. outputStream << newLine;
  251. writeSpaces (outputStream, (size_t) indentationLevel);
  252. }
  253. outputStream.write ("</", 2);
  254. outputStream << tagName;
  255. outputStream.writeByte ('>');
  256. }
  257. else
  258. {
  259. outputStream.write ("/>", 2);
  260. }
  261. }
  262. else
  263. {
  264. escapeIllegalXmlChars (outputStream, getText(), false);
  265. }
  266. }
  267. String XmlElement::createDocument (StringRef dtdToUse,
  268. const bool allOnOneLine,
  269. const bool includeXmlHeader,
  270. StringRef encodingType,
  271. const int lineWrapLength) const
  272. {
  273. MemoryOutputStream mem (2048);
  274. writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);
  275. return mem.toUTF8();
  276. }
  277. void XmlElement::writeToStream (OutputStream& output,
  278. StringRef dtdToUse,
  279. const bool allOnOneLine,
  280. const bool includeXmlHeader,
  281. StringRef encodingType,
  282. const int lineWrapLength) const
  283. {
  284. using namespace XmlOutputFunctions;
  285. if (includeXmlHeader)
  286. {
  287. output << "<?xml version=\"1.0\" encoding=\"" << encodingType << "\"?>";
  288. if (allOnOneLine)
  289. output.writeByte (' ');
  290. else
  291. output << newLine << newLine;
  292. }
  293. if (dtdToUse.isNotEmpty())
  294. {
  295. output << dtdToUse;
  296. if (allOnOneLine)
  297. output.writeByte (' ');
  298. else
  299. output << newLine;
  300. }
  301. writeElementAsText (output, allOnOneLine ? -1 : 0, lineWrapLength);
  302. if (! allOnOneLine)
  303. output << newLine;
  304. }
  305. bool XmlElement::writeToFile (const File& file,
  306. StringRef dtdToUse,
  307. StringRef encodingType,
  308. const int lineWrapLength) const
  309. {
  310. TemporaryFile tempFile (file);
  311. {
  312. FileOutputStream out (tempFile.getFile());
  313. if (! out.openedOk())
  314. return false;
  315. writeToStream (out, dtdToUse, false, true, encodingType, lineWrapLength);
  316. }
  317. return tempFile.overwriteTargetFileWithTemporary();
  318. }
  319. //==============================================================================
  320. bool XmlElement::hasTagName (StringRef possibleTagName) const noexcept
  321. {
  322. const bool matches = tagName.equalsIgnoreCase (possibleTagName);
  323. // XML tags should be case-sensitive, so although this method allows a
  324. // case-insensitive match to pass, you should try to avoid this.
  325. jassert ((! matches) || tagName == possibleTagName);
  326. return matches;
  327. }
  328. String XmlElement::getNamespace() const
  329. {
  330. return tagName.upToFirstOccurrenceOf (":", false, false);
  331. }
  332. String XmlElement::getTagNameWithoutNamespace() const
  333. {
  334. return tagName.fromLastOccurrenceOf (":", false, false);
  335. }
  336. bool XmlElement::hasTagNameIgnoringNamespace (StringRef possibleTagName) const
  337. {
  338. return hasTagName (possibleTagName) || getTagNameWithoutNamespace() == possibleTagName;
  339. }
  340. XmlElement* XmlElement::getNextElementWithTagName (StringRef requiredTagName) const
  341. {
  342. XmlElement* e = nextListItem;
  343. while (e != nullptr && ! e->hasTagName (requiredTagName))
  344. e = e->nextListItem;
  345. return e;
  346. }
  347. //==============================================================================
  348. int XmlElement::getNumAttributes() const noexcept
  349. {
  350. return attributes.size();
  351. }
  352. const String& XmlElement::getAttributeName (const int index) const noexcept
  353. {
  354. if (const XmlAttributeNode* const att = attributes [index])
  355. return att->name.toString();
  356. return String::empty;
  357. }
  358. const String& XmlElement::getAttributeValue (const int index) const noexcept
  359. {
  360. if (const XmlAttributeNode* const att = attributes [index])
  361. return att->value;
  362. return String::empty;
  363. }
  364. XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
  365. {
  366. for (XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  367. if (att->name == attributeName)
  368. return att;
  369. return nullptr;
  370. }
  371. bool XmlElement::hasAttribute (StringRef attributeName) const noexcept
  372. {
  373. return getAttribute (attributeName) != nullptr;
  374. }
  375. //==============================================================================
  376. const String& XmlElement::getStringAttribute (StringRef attributeName) const noexcept
  377. {
  378. if (const XmlAttributeNode* att = getAttribute (attributeName))
  379. return att->value;
  380. return String::empty;
  381. }
  382. String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
  383. {
  384. if (const XmlAttributeNode* att = getAttribute (attributeName))
  385. return att->value;
  386. return defaultReturnValue;
  387. }
  388. int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
  389. {
  390. if (const XmlAttributeNode* att = getAttribute (attributeName))
  391. return att->value.getIntValue();
  392. return defaultReturnValue;
  393. }
  394. double XmlElement::getDoubleAttribute (StringRef attributeName, const double defaultReturnValue) const
  395. {
  396. if (const XmlAttributeNode* att = getAttribute (attributeName))
  397. return att->value.getDoubleValue();
  398. return defaultReturnValue;
  399. }
  400. bool XmlElement::getBoolAttribute (StringRef attributeName, const bool defaultReturnValue) const
  401. {
  402. if (const XmlAttributeNode* att = getAttribute (attributeName))
  403. {
  404. const juce_wchar firstChar = *(att->value.getCharPointer().findEndOfWhitespace());
  405. return firstChar == '1'
  406. || firstChar == 't'
  407. || firstChar == 'y'
  408. || firstChar == 'T'
  409. || firstChar == 'Y';
  410. }
  411. return defaultReturnValue;
  412. }
  413. bool XmlElement::compareAttribute (StringRef attributeName,
  414. StringRef stringToCompareAgainst,
  415. const bool ignoreCase) const noexcept
  416. {
  417. if (const XmlAttributeNode* att = getAttribute (attributeName))
  418. return ignoreCase ? att->value.equalsIgnoreCase (stringToCompareAgainst)
  419. : att->value == stringToCompareAgainst;
  420. return false;
  421. }
  422. //==============================================================================
  423. void XmlElement::setAttribute (const Identifier& attributeName, const String& value)
  424. {
  425. if (attributes == nullptr)
  426. {
  427. attributes = new XmlAttributeNode (attributeName, value);
  428. }
  429. else
  430. {
  431. for (XmlAttributeNode* att = attributes; ; att = att->nextListItem)
  432. {
  433. if (att->name == attributeName)
  434. {
  435. att->value = value;
  436. break;
  437. }
  438. if (att->nextListItem == nullptr)
  439. {
  440. att->nextListItem = new XmlAttributeNode (attributeName, value);
  441. break;
  442. }
  443. }
  444. }
  445. }
  446. void XmlElement::setAttribute (const Identifier& attributeName, const int number)
  447. {
  448. setAttribute (attributeName, String (number));
  449. }
  450. void XmlElement::setAttribute (const Identifier& attributeName, const double number)
  451. {
  452. setAttribute (attributeName, String (number, 20));
  453. }
  454. void XmlElement::removeAttribute (const Identifier& attributeName) noexcept
  455. {
  456. for (LinkedListPointer<XmlAttributeNode>* att = &attributes;
  457. att->get() != nullptr;
  458. att = &(att->get()->nextListItem))
  459. {
  460. if (att->get()->name == attributeName)
  461. {
  462. delete att->removeNext();
  463. break;
  464. }
  465. }
  466. }
  467. void XmlElement::removeAllAttributes() noexcept
  468. {
  469. attributes.deleteAll();
  470. }
  471. //==============================================================================
  472. int XmlElement::getNumChildElements() const noexcept
  473. {
  474. return firstChildElement.size();
  475. }
  476. XmlElement* XmlElement::getChildElement (const int index) const noexcept
  477. {
  478. return firstChildElement [index].get();
  479. }
  480. XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept
  481. {
  482. jassert (! childName.isEmpty());
  483. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  484. if (child->hasTagName (childName))
  485. return child;
  486. return nullptr;
  487. }
  488. XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept
  489. {
  490. jassert (! attributeName.isEmpty());
  491. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  492. if (child->compareAttribute (attributeName, attributeValue))
  493. return child;
  494. return nullptr;
  495. }
  496. void XmlElement::addChildElement (XmlElement* const newNode) noexcept
  497. {
  498. if (newNode != nullptr)
  499. {
  500. // The element being added must not be a child of another node!
  501. jassert (newNode->nextListItem == nullptr);
  502. firstChildElement.append (newNode);
  503. }
  504. }
  505. void XmlElement::insertChildElement (XmlElement* const newNode, int indexToInsertAt) noexcept
  506. {
  507. if (newNode != nullptr)
  508. {
  509. // The element being added must not be a child of another node!
  510. jassert (newNode->nextListItem == nullptr);
  511. firstChildElement.insertAtIndex (indexToInsertAt, newNode);
  512. }
  513. }
  514. void XmlElement::prependChildElement (XmlElement* newNode) noexcept
  515. {
  516. if (newNode != nullptr)
  517. {
  518. // The element being added must not be a child of another node!
  519. jassert (newNode->nextListItem == nullptr);
  520. firstChildElement.insertNext (newNode);
  521. }
  522. }
  523. XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
  524. {
  525. XmlElement* const newElement = new XmlElement (childTagName);
  526. addChildElement (newElement);
  527. return newElement;
  528. }
  529. bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
  530. XmlElement* const newNode) noexcept
  531. {
  532. if (newNode != nullptr)
  533. {
  534. if (LinkedListPointer<XmlElement>* const p = firstChildElement.findPointerTo (currentChildElement))
  535. {
  536. if (currentChildElement != newNode)
  537. delete p->replaceNext (newNode);
  538. return true;
  539. }
  540. }
  541. return false;
  542. }
  543. void XmlElement::removeChildElement (XmlElement* const childToRemove,
  544. const bool shouldDeleteTheChild) noexcept
  545. {
  546. if (childToRemove != nullptr)
  547. {
  548. firstChildElement.remove (childToRemove);
  549. if (shouldDeleteTheChild)
  550. delete childToRemove;
  551. }
  552. }
  553. bool XmlElement::isEquivalentTo (const XmlElement* const other,
  554. const bool ignoreOrderOfAttributes) const noexcept
  555. {
  556. if (this != other)
  557. {
  558. if (other == nullptr || tagName != other->tagName)
  559. return false;
  560. if (ignoreOrderOfAttributes)
  561. {
  562. int totalAtts = 0;
  563. for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  564. {
  565. if (! other->compareAttribute (att->name, att->value))
  566. return false;
  567. ++totalAtts;
  568. }
  569. if (totalAtts != other->getNumAttributes())
  570. return false;
  571. }
  572. else
  573. {
  574. const XmlAttributeNode* thisAtt = attributes;
  575. const XmlAttributeNode* otherAtt = other->attributes;
  576. for (;;)
  577. {
  578. if (thisAtt == nullptr || otherAtt == nullptr)
  579. {
  580. if (thisAtt == otherAtt) // both 0, so it's a match
  581. break;
  582. return false;
  583. }
  584. if (thisAtt->name != otherAtt->name
  585. || thisAtt->value != otherAtt->value)
  586. {
  587. return false;
  588. }
  589. thisAtt = thisAtt->nextListItem;
  590. otherAtt = otherAtt->nextListItem;
  591. }
  592. }
  593. const XmlElement* thisChild = firstChildElement;
  594. const XmlElement* otherChild = other->firstChildElement;
  595. for (;;)
  596. {
  597. if (thisChild == nullptr || otherChild == nullptr)
  598. {
  599. if (thisChild == otherChild) // both 0, so it's a match
  600. break;
  601. return false;
  602. }
  603. if (! thisChild->isEquivalentTo (otherChild, ignoreOrderOfAttributes))
  604. return false;
  605. thisChild = thisChild->nextListItem;
  606. otherChild = otherChild->nextListItem;
  607. }
  608. }
  609. return true;
  610. }
  611. void XmlElement::deleteAllChildElements() noexcept
  612. {
  613. firstChildElement.deleteAll();
  614. }
  615. void XmlElement::deleteAllChildElementsWithTagName (StringRef name) noexcept
  616. {
  617. for (XmlElement* child = firstChildElement; child != nullptr;)
  618. {
  619. XmlElement* const nextChild = child->nextListItem;
  620. if (child->hasTagName (name))
  621. removeChildElement (child, true);
  622. child = nextChild;
  623. }
  624. }
  625. bool XmlElement::containsChildElement (const XmlElement* const possibleChild) const noexcept
  626. {
  627. return firstChildElement.contains (possibleChild);
  628. }
  629. XmlElement* XmlElement::findParentElementOf (const XmlElement* const elementToLookFor) noexcept
  630. {
  631. if (this == elementToLookFor || elementToLookFor == nullptr)
  632. return nullptr;
  633. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  634. {
  635. if (elementToLookFor == child)
  636. return this;
  637. if (XmlElement* const found = child->findParentElementOf (elementToLookFor))
  638. return found;
  639. }
  640. return nullptr;
  641. }
  642. void XmlElement::getChildElementsAsArray (XmlElement** elems) const noexcept
  643. {
  644. firstChildElement.copyToArray (elems);
  645. }
  646. void XmlElement::reorderChildElements (XmlElement** const elems, const int num) noexcept
  647. {
  648. XmlElement* e = firstChildElement = elems[0];
  649. for (int i = 1; i < num; ++i)
  650. {
  651. e->nextListItem = elems[i];
  652. e = e->nextListItem;
  653. }
  654. e->nextListItem = nullptr;
  655. }
  656. //==============================================================================
  657. bool XmlElement::isTextElement() const noexcept
  658. {
  659. return tagName.isEmpty();
  660. }
  661. static const String juce_xmltextContentAttributeName ("text");
  662. const String& XmlElement::getText() const noexcept
  663. {
  664. jassert (isTextElement()); // you're trying to get the text from an element that
  665. // isn't actually a text element.. If this contains text sub-nodes, you
  666. // probably want to use getAllSubText instead.
  667. return getStringAttribute (juce_xmltextContentAttributeName);
  668. }
  669. void XmlElement::setText (const String& newText)
  670. {
  671. if (isTextElement())
  672. setAttribute (juce_xmltextContentAttributeName, newText);
  673. else
  674. jassertfalse; // you can only change the text in a text element, not a normal one.
  675. }
  676. String XmlElement::getAllSubText() const
  677. {
  678. if (isTextElement())
  679. return getText();
  680. if (getNumChildElements() == 1)
  681. return firstChildElement.get()->getAllSubText();
  682. MemoryOutputStream mem (1024);
  683. for (const XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  684. mem << child->getAllSubText();
  685. return mem.toUTF8();
  686. }
  687. String XmlElement::getChildElementAllSubText (StringRef childTagName, const String& defaultReturnValue) const
  688. {
  689. if (const XmlElement* const child = getChildByName (childTagName))
  690. return child->getAllSubText();
  691. return defaultReturnValue;
  692. }
  693. XmlElement* XmlElement::createTextElement (const String& text)
  694. {
  695. XmlElement* const e = new XmlElement ((int) 0);
  696. e->setAttribute (juce_xmltextContentAttributeName, text);
  697. return e;
  698. }
  699. void XmlElement::addTextElement (const String& text)
  700. {
  701. addChildElement (createTextElement (text));
  702. }
  703. void XmlElement::deleteAllTextElements() noexcept
  704. {
  705. for (XmlElement* child = firstChildElement; child != nullptr;)
  706. {
  707. XmlElement* const next = child->nextListItem;
  708. if (child->isTextElement())
  709. removeChildElement (child, true);
  710. child = next;
  711. }
  712. }