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.

920 lines
27KB

  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. #include "XmlElement.h"
  21. #include "../streams/MemoryOutputStream.h"
  22. #include "../streams/OutputStream.h"
  23. #include "../text/NewLine.h"
  24. namespace water {
  25. inline bool isValidXmlNameStartCharacter (const water_uchar character) noexcept
  26. {
  27. return character == ':'
  28. || character == '_'
  29. || (character >= 'a' && character <= 'z')
  30. || (character >= 'A' && character <= 'Z')
  31. || (character >= 0xc0 && character <= 0xd6)
  32. || (character >= 0xd8 && character <= 0xf6)
  33. || (character >= 0xf8 && character <= 0x2ff)
  34. || (character >= 0x370 && character <= 0x37d)
  35. || (character >= 0x37f && character <= 0x1fff)
  36. || (character >= 0x200c && character <= 0x200d)
  37. || (character >= 0x2070 && character <= 0x218f)
  38. || (character >= 0x2c00 && character <= 0x2fef)
  39. || (character >= 0x3001 && character <= 0xd7ff)
  40. || (character >= 0xf900 && character <= 0xfdcf)
  41. || (character >= 0xfdf0 && character <= 0xfffd)
  42. || (character >= 0x10000 && character <= 0xeffff);
  43. }
  44. inline bool isValidXmlNameBodyCharacter (const water_uchar character) noexcept
  45. {
  46. return isValidXmlNameStartCharacter (character)
  47. || character == '-'
  48. || character == '.'
  49. || character == 0xb7
  50. || (character >= '0' && character <= '9')
  51. || (character >= 0x300 && character <= 0x036f)
  52. || (character >= 0x203f && character <= 0x2040);
  53. }
  54. XmlElement::XmlAttributeNode::XmlAttributeNode (const XmlAttributeNode& other) noexcept
  55. : name (other.name),
  56. value (other.value)
  57. {
  58. }
  59. XmlElement::XmlAttributeNode::XmlAttributeNode (const Identifier& n, const String& v) noexcept
  60. : name (n), value (v)
  61. {
  62. wassert (isValidXmlName (name));
  63. }
  64. XmlElement::XmlAttributeNode::XmlAttributeNode (String::CharPointerType nameStart, String::CharPointerType nameEnd)
  65. : name (nameStart, nameEnd)
  66. {
  67. wassert (isValidXmlName (name));
  68. }
  69. //==============================================================================
  70. XmlElement::XmlElement (const String& tag)
  71. : tagName (tag)
  72. {
  73. wassert (isValidXmlName (tagName));
  74. }
  75. XmlElement::XmlElement (const char* tag)
  76. : tagName (tag)
  77. {
  78. wassert (isValidXmlName (tagName));
  79. }
  80. XmlElement::XmlElement (StringRef tag)
  81. : tagName (tag)
  82. {
  83. wassert (isValidXmlName (tagName));
  84. }
  85. XmlElement::XmlElement (const Identifier& tag)
  86. : tagName (tag.toString())
  87. {
  88. wassert (isValidXmlName (tagName));
  89. }
  90. XmlElement::XmlElement (String::CharPointerType tagNameStart, String::CharPointerType tagNameEnd)
  91. : tagName (StartEndString (tagNameStart, tagNameEnd))
  92. {
  93. wassert (isValidXmlName (tagName));
  94. }
  95. XmlElement::XmlElement (int /*dummy*/) noexcept
  96. {
  97. }
  98. XmlElement::XmlElement (const XmlElement& other)
  99. : tagName (other.tagName)
  100. {
  101. copyChildrenAndAttributesFrom (other);
  102. }
  103. XmlElement& XmlElement::operator= (const XmlElement& other)
  104. {
  105. if (this != &other)
  106. {
  107. removeAllAttributes();
  108. deleteAllChildElements();
  109. tagName = other.tagName;
  110. copyChildrenAndAttributesFrom (other);
  111. }
  112. return *this;
  113. }
  114. void XmlElement::copyChildrenAndAttributesFrom (const XmlElement& other)
  115. {
  116. wassert (firstChildElement.get() == nullptr);
  117. firstChildElement.addCopyOfList (other.firstChildElement);
  118. wassert (attributes.get() == nullptr);
  119. attributes.addCopyOfList (other.attributes);
  120. }
  121. XmlElement::~XmlElement() noexcept
  122. {
  123. firstChildElement.deleteAll();
  124. attributes.deleteAll();
  125. }
  126. //==============================================================================
  127. namespace XmlOutputFunctions
  128. {
  129. #if 0 // (These functions are just used to generate the lookup table used below)
  130. bool isLegalXmlCharSlow (const water_uchar character) noexcept
  131. {
  132. if ((character >= 'a' && character <= 'z')
  133. || (character >= 'A' && character <= 'Z')
  134. || (character >= '0' && character <= '9'))
  135. return true;
  136. const char* t = " .,;:-()_+=?!'#@[]/\\*%~{}$|";
  137. do
  138. {
  139. if (((water_uchar) (uint8) *t) == character)
  140. return true;
  141. }
  142. while (*++t != 0);
  143. return false;
  144. }
  145. void generateLegalCharLookupTable()
  146. {
  147. uint8 n[32] = { 0 };
  148. for (int i = 0; i < 256; ++i)
  149. if (isLegalXmlCharSlow (i))
  150. n[i >> 3] |= (1 << (i & 7));
  151. String s;
  152. for (int i = 0; i < 32; ++i)
  153. s << (int) n[i] << ", ";
  154. DBG (s);
  155. }
  156. #endif
  157. static bool isLegalXmlChar (const uint32 c) noexcept
  158. {
  159. static const unsigned char legalChars[] = { 0, 0, 0, 0, 187, 255, 255, 175, 255,
  160. 255, 255, 191, 254, 255, 255, 127 };
  161. return c < sizeof (legalChars) * 8
  162. && (legalChars [c >> 3] & (1 << (c & 7))) != 0;
  163. }
  164. static void escapeIllegalXmlChars (OutputStream& outputStream, const String& text, const bool changeNewLines)
  165. {
  166. String::CharPointerType t (text.getCharPointer());
  167. for (;;)
  168. {
  169. const uint32 character = (uint32) t.getAndAdvance();
  170. if (character == 0)
  171. break;
  172. if (isLegalXmlChar (character))
  173. {
  174. outputStream << (char) character;
  175. }
  176. else
  177. {
  178. switch (character)
  179. {
  180. case '&': outputStream << "&amp;"; break;
  181. case '"': outputStream << "&quot;"; break;
  182. case '>': outputStream << "&gt;"; break;
  183. case '<': outputStream << "&lt;"; break;
  184. case '\n':
  185. case '\r':
  186. if (! changeNewLines)
  187. {
  188. outputStream << (char) character;
  189. break;
  190. }
  191. // fall-through
  192. default:
  193. outputStream << "&#" << ((int) character) << ';';
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. static void writeSpaces (OutputStream& out, const size_t numSpaces)
  200. {
  201. out.writeRepeatedByte (' ', numSpaces);
  202. }
  203. }
  204. void XmlElement::writeElementAsText (OutputStream& outputStream,
  205. const int indentationLevel,
  206. const int lineWrapLength) const
  207. {
  208. using namespace XmlOutputFunctions;
  209. NewLine newLine;
  210. if (indentationLevel >= 0)
  211. writeSpaces (outputStream, (size_t) indentationLevel);
  212. if (! isTextElement())
  213. {
  214. outputStream.writeByte ('<');
  215. outputStream << tagName;
  216. {
  217. const size_t attIndent = (size_t) (indentationLevel + tagName.length() + 1);
  218. int lineLen = 0;
  219. for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  220. {
  221. if (lineLen > lineWrapLength && indentationLevel >= 0)
  222. {
  223. outputStream << newLine;
  224. writeSpaces (outputStream, attIndent);
  225. lineLen = 0;
  226. }
  227. const int64 startPos = outputStream.getPosition();
  228. outputStream.writeByte (' ');
  229. outputStream << att->name;
  230. outputStream.write ("=\"", 2);
  231. escapeIllegalXmlChars (outputStream, att->value, true);
  232. outputStream.writeByte ('"');
  233. lineLen += (int) (outputStream.getPosition() - startPos);
  234. }
  235. }
  236. if (firstChildElement != nullptr)
  237. {
  238. outputStream.writeByte ('>');
  239. bool lastWasTextNode = false;
  240. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  241. {
  242. if (child->isTextElement())
  243. {
  244. escapeIllegalXmlChars (outputStream, child->getText(), false);
  245. lastWasTextNode = true;
  246. }
  247. else
  248. {
  249. if (indentationLevel >= 0 && ! lastWasTextNode)
  250. outputStream << newLine;
  251. child->writeElementAsText (outputStream,
  252. lastWasTextNode ? 0 : (indentationLevel + (indentationLevel >= 0 ? 2 : 0)), lineWrapLength);
  253. lastWasTextNode = false;
  254. }
  255. }
  256. if (indentationLevel >= 0 && ! lastWasTextNode)
  257. {
  258. outputStream << newLine;
  259. writeSpaces (outputStream, (size_t) indentationLevel);
  260. }
  261. outputStream.write ("</", 2);
  262. outputStream << tagName;
  263. outputStream.writeByte ('>');
  264. }
  265. else
  266. {
  267. outputStream.write ("/>", 2);
  268. }
  269. }
  270. else
  271. {
  272. escapeIllegalXmlChars (outputStream, getText(), false);
  273. }
  274. }
  275. String XmlElement::createDocument (StringRef dtdToUse,
  276. const bool allOnOneLine,
  277. const bool includeXmlHeader,
  278. StringRef encodingType,
  279. const int lineWrapLength) const
  280. {
  281. MemoryOutputStream mem (2048);
  282. writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);
  283. return mem.toUTF8();
  284. }
  285. void XmlElement::writeToStream (OutputStream& output,
  286. StringRef dtdToUse,
  287. const bool allOnOneLine,
  288. const bool includeXmlHeader,
  289. StringRef encodingType,
  290. const int lineWrapLength) const
  291. {
  292. using namespace XmlOutputFunctions;
  293. NewLine newLine;
  294. if (includeXmlHeader)
  295. {
  296. output << "<?xml version=\"1.0\" encoding=\"" << encodingType << "\"?>";
  297. if (allOnOneLine)
  298. output.writeByte (' ');
  299. else
  300. output << newLine << newLine;
  301. }
  302. if (dtdToUse.isNotEmpty())
  303. {
  304. output << dtdToUse;
  305. if (allOnOneLine)
  306. output.writeByte (' ');
  307. else
  308. output << newLine;
  309. }
  310. writeElementAsText (output, allOnOneLine ? -1 : 0, lineWrapLength);
  311. if (! allOnOneLine)
  312. output << newLine;
  313. }
  314. #if 0
  315. bool XmlElement::writeToFile (const File& file,
  316. StringRef dtdToUse,
  317. StringRef encodingType,
  318. const int lineWrapLength) const
  319. {
  320. TemporaryFile tempFile (file);
  321. {
  322. FileOutputStream out (tempFile.getFile());
  323. if (! out.openedOk())
  324. return false;
  325. writeToStream (out, dtdToUse, false, true, encodingType, lineWrapLength);
  326. out.flush(); // (called explicitly to force an fsync on posix)
  327. if (out.getStatus().failed())
  328. return false;
  329. }
  330. return tempFile.overwriteTargetFileWithTemporary();
  331. }
  332. #endif
  333. //==============================================================================
  334. bool XmlElement::hasTagName (StringRef possibleTagName) const noexcept
  335. {
  336. const bool matches = tagName.equalsIgnoreCase (possibleTagName);
  337. // XML tags should be case-sensitive, so although this method allows a
  338. // case-insensitive match to pass, you should try to avoid this.
  339. wassert ((! matches) || tagName == possibleTagName);
  340. return matches;
  341. }
  342. String XmlElement::getNamespace() const
  343. {
  344. return tagName.upToFirstOccurrenceOf (":", false, false);
  345. }
  346. String XmlElement::getTagNameWithoutNamespace() const
  347. {
  348. return tagName.fromLastOccurrenceOf (":", false, false);
  349. }
  350. bool XmlElement::hasTagNameIgnoringNamespace (StringRef possibleTagName) const
  351. {
  352. return hasTagName (possibleTagName) || getTagNameWithoutNamespace() == possibleTagName;
  353. }
  354. XmlElement* XmlElement::getNextElementWithTagName (StringRef requiredTagName) const
  355. {
  356. XmlElement* e = nextListItem;
  357. while (e != nullptr && ! e->hasTagName (requiredTagName))
  358. e = e->nextListItem;
  359. return e;
  360. }
  361. //==============================================================================
  362. int XmlElement::getNumAttributes() const noexcept
  363. {
  364. return attributes.size();
  365. }
  366. static const String& getEmptyStringRef() noexcept
  367. {
  368. static String empty;
  369. return empty;
  370. }
  371. const String& XmlElement::getAttributeName (const int index) const noexcept
  372. {
  373. if (const XmlAttributeNode* const att = attributes [index])
  374. return att->name.toString();
  375. return getEmptyStringRef();
  376. }
  377. const String& XmlElement::getAttributeValue (const int index) const noexcept
  378. {
  379. if (const XmlAttributeNode* const att = attributes [index])
  380. return att->value;
  381. return getEmptyStringRef();
  382. }
  383. XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
  384. {
  385. for (XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  386. if (att->name == attributeName)
  387. return att;
  388. return nullptr;
  389. }
  390. bool XmlElement::hasAttribute (StringRef attributeName) const noexcept
  391. {
  392. return getAttribute (attributeName) != nullptr;
  393. }
  394. //==============================================================================
  395. const String& XmlElement::getStringAttribute (StringRef attributeName) const noexcept
  396. {
  397. if (const XmlAttributeNode* att = getAttribute (attributeName))
  398. return att->value;
  399. return getEmptyStringRef();
  400. }
  401. String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
  402. {
  403. if (const XmlAttributeNode* att = getAttribute (attributeName))
  404. return att->value;
  405. return defaultReturnValue;
  406. }
  407. int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
  408. {
  409. if (const XmlAttributeNode* att = getAttribute (attributeName))
  410. return att->value.getIntValue();
  411. return defaultReturnValue;
  412. }
  413. double XmlElement::getDoubleAttribute (StringRef attributeName, const double defaultReturnValue) const
  414. {
  415. if (const XmlAttributeNode* att = getAttribute (attributeName))
  416. return att->value.getDoubleValue();
  417. return defaultReturnValue;
  418. }
  419. bool XmlElement::getBoolAttribute (StringRef attributeName, const bool defaultReturnValue) const
  420. {
  421. if (const XmlAttributeNode* att = getAttribute (attributeName))
  422. {
  423. const water_uchar firstChar = *(att->value.getCharPointer().findEndOfWhitespace());
  424. return firstChar == '1'
  425. || firstChar == 't'
  426. || firstChar == 'y'
  427. || firstChar == 'T'
  428. || firstChar == 'Y';
  429. }
  430. return defaultReturnValue;
  431. }
  432. bool XmlElement::compareAttribute (StringRef attributeName,
  433. StringRef stringToCompareAgainst,
  434. const bool ignoreCase) const noexcept
  435. {
  436. if (const XmlAttributeNode* att = getAttribute (attributeName))
  437. return ignoreCase ? att->value.equalsIgnoreCase (stringToCompareAgainst)
  438. : att->value == stringToCompareAgainst;
  439. return false;
  440. }
  441. //==============================================================================
  442. void XmlElement::setAttribute (const Identifier& attributeName, const String& value)
  443. {
  444. if (attributes == nullptr)
  445. {
  446. attributes = new XmlAttributeNode (attributeName, value);
  447. }
  448. else
  449. {
  450. for (XmlAttributeNode* att = attributes; ; att = att->nextListItem)
  451. {
  452. if (att->name == attributeName)
  453. {
  454. att->value = value;
  455. break;
  456. }
  457. if (att->nextListItem == nullptr)
  458. {
  459. att->nextListItem = new XmlAttributeNode (attributeName, value);
  460. break;
  461. }
  462. }
  463. }
  464. }
  465. void XmlElement::setAttribute (const Identifier& attributeName, const int number)
  466. {
  467. setAttribute (attributeName, String (number));
  468. }
  469. void XmlElement::setAttribute (const Identifier& attributeName, const double number)
  470. {
  471. setAttribute (attributeName, String (number, 20));
  472. }
  473. void XmlElement::removeAttribute (const Identifier& attributeName) noexcept
  474. {
  475. for (LinkedListPointer<XmlAttributeNode>* att = &attributes;
  476. att->get() != nullptr;
  477. att = &(att->get()->nextListItem))
  478. {
  479. if (att->get()->name == attributeName)
  480. {
  481. delete att->removeNext();
  482. break;
  483. }
  484. }
  485. }
  486. void XmlElement::removeAllAttributes() noexcept
  487. {
  488. attributes.deleteAll();
  489. }
  490. //==============================================================================
  491. int XmlElement::getNumChildElements() const noexcept
  492. {
  493. return firstChildElement.size();
  494. }
  495. XmlElement* XmlElement::getChildElement (const int index) const noexcept
  496. {
  497. return firstChildElement [index].get();
  498. }
  499. XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept
  500. {
  501. wassert (! childName.isEmpty());
  502. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  503. if (child->hasTagName (childName))
  504. return child;
  505. return nullptr;
  506. }
  507. XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept
  508. {
  509. wassert (! attributeName.isEmpty());
  510. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  511. if (child->compareAttribute (attributeName, attributeValue))
  512. return child;
  513. return nullptr;
  514. }
  515. void XmlElement::addChildElement (XmlElement* const newNode) noexcept
  516. {
  517. if (newNode != nullptr)
  518. {
  519. // The element being added must not be a child of another node!
  520. wassert (newNode->nextListItem == nullptr);
  521. firstChildElement.append (newNode);
  522. }
  523. }
  524. void XmlElement::insertChildElement (XmlElement* const newNode, int indexToInsertAt) noexcept
  525. {
  526. if (newNode != nullptr)
  527. {
  528. // The element being added must not be a child of another node!
  529. wassert (newNode->nextListItem == nullptr);
  530. firstChildElement.insertAtIndex (indexToInsertAt, newNode);
  531. }
  532. }
  533. void XmlElement::prependChildElement (XmlElement* newNode) noexcept
  534. {
  535. if (newNode != nullptr)
  536. {
  537. // The element being added must not be a child of another node!
  538. wassert (newNode->nextListItem == nullptr);
  539. firstChildElement.insertNext (newNode);
  540. }
  541. }
  542. XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
  543. {
  544. XmlElement* const newElement = new XmlElement (childTagName);
  545. addChildElement (newElement);
  546. return newElement;
  547. }
  548. bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
  549. XmlElement* const newNode) noexcept
  550. {
  551. if (newNode != nullptr)
  552. {
  553. if (LinkedListPointer<XmlElement>* const p = firstChildElement.findPointerTo (currentChildElement))
  554. {
  555. if (currentChildElement != newNode)
  556. delete p->replaceNext (newNode);
  557. return true;
  558. }
  559. }
  560. return false;
  561. }
  562. void XmlElement::removeChildElement (XmlElement* const childToRemove,
  563. const bool shouldDeleteTheChild) noexcept
  564. {
  565. if (childToRemove != nullptr)
  566. {
  567. firstChildElement.remove (childToRemove);
  568. if (shouldDeleteTheChild)
  569. delete childToRemove;
  570. }
  571. }
  572. bool XmlElement::isEquivalentTo (const XmlElement* const other,
  573. const bool ignoreOrderOfAttributes) const noexcept
  574. {
  575. if (this != other)
  576. {
  577. if (other == nullptr || tagName != other->tagName)
  578. return false;
  579. if (ignoreOrderOfAttributes)
  580. {
  581. int totalAtts = 0;
  582. for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  583. {
  584. if (! other->compareAttribute (att->name, att->value))
  585. return false;
  586. ++totalAtts;
  587. }
  588. if (totalAtts != other->getNumAttributes())
  589. return false;
  590. }
  591. else
  592. {
  593. const XmlAttributeNode* thisAtt = attributes;
  594. const XmlAttributeNode* otherAtt = other->attributes;
  595. for (;;)
  596. {
  597. if (thisAtt == nullptr || otherAtt == nullptr)
  598. {
  599. if (thisAtt == otherAtt) // both nullptr, so it's a match
  600. break;
  601. return false;
  602. }
  603. if (thisAtt->name != otherAtt->name
  604. || thisAtt->value != otherAtt->value)
  605. {
  606. return false;
  607. }
  608. thisAtt = thisAtt->nextListItem;
  609. otherAtt = otherAtt->nextListItem;
  610. }
  611. }
  612. const XmlElement* thisChild = firstChildElement;
  613. const XmlElement* otherChild = other->firstChildElement;
  614. for (;;)
  615. {
  616. if (thisChild == nullptr || otherChild == nullptr)
  617. {
  618. if (thisChild == otherChild) // both 0, so it's a match
  619. break;
  620. return false;
  621. }
  622. if (! thisChild->isEquivalentTo (otherChild, ignoreOrderOfAttributes))
  623. return false;
  624. thisChild = thisChild->nextListItem;
  625. otherChild = otherChild->nextListItem;
  626. }
  627. }
  628. return true;
  629. }
  630. void XmlElement::deleteAllChildElements() noexcept
  631. {
  632. firstChildElement.deleteAll();
  633. }
  634. void XmlElement::deleteAllChildElementsWithTagName (StringRef name) noexcept
  635. {
  636. for (XmlElement* child = firstChildElement; child != nullptr;)
  637. {
  638. XmlElement* const nextChild = child->nextListItem;
  639. if (child->hasTagName (name))
  640. removeChildElement (child, true);
  641. child = nextChild;
  642. }
  643. }
  644. bool XmlElement::containsChildElement (const XmlElement* const possibleChild) const noexcept
  645. {
  646. return firstChildElement.contains (possibleChild);
  647. }
  648. XmlElement* XmlElement::findParentElementOf (const XmlElement* const elementToLookFor) noexcept
  649. {
  650. if (this == elementToLookFor || elementToLookFor == nullptr)
  651. return nullptr;
  652. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  653. {
  654. if (elementToLookFor == child)
  655. return this;
  656. if (XmlElement* const found = child->findParentElementOf (elementToLookFor))
  657. return found;
  658. }
  659. return nullptr;
  660. }
  661. void XmlElement::getChildElementsAsArray (XmlElement** elems) const noexcept
  662. {
  663. firstChildElement.copyToArray (elems);
  664. }
  665. void XmlElement::reorderChildElements (XmlElement** const elems, const int num) noexcept
  666. {
  667. XmlElement* e = firstChildElement = elems[0];
  668. for (int i = 1; i < num; ++i)
  669. {
  670. e->nextListItem = elems[i];
  671. e = e->nextListItem;
  672. }
  673. e->nextListItem = nullptr;
  674. }
  675. //==============================================================================
  676. bool XmlElement::isTextElement() const noexcept
  677. {
  678. return tagName.isEmpty();
  679. }
  680. static const String water_xmltextContentAttributeName ()
  681. {
  682. return String ("text");
  683. }
  684. const String& XmlElement::getText() const noexcept
  685. {
  686. wassert (isTextElement()); // you're trying to get the text from an element that
  687. // isn't actually a text element.. If this contains text sub-nodes, you
  688. // probably want to use getAllSubText instead.
  689. return getStringAttribute (water_xmltextContentAttributeName());
  690. }
  691. void XmlElement::setText (const String& newText)
  692. {
  693. CARLA_SAFE_ASSERT_RETURN(isTextElement(),);
  694. setAttribute (water_xmltextContentAttributeName(), newText);
  695. }
  696. String XmlElement::getAllSubText() const
  697. {
  698. if (isTextElement())
  699. return getText();
  700. if (getNumChildElements() == 1)
  701. return firstChildElement.get()->getAllSubText();
  702. MemoryOutputStream mem (1024);
  703. for (const XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  704. mem << child->getAllSubText();
  705. return mem.toUTF8();
  706. }
  707. String XmlElement::getChildElementAllSubText (StringRef childTagName, const String& defaultReturnValue) const
  708. {
  709. if (const XmlElement* const child = getChildByName (childTagName))
  710. return child->getAllSubText();
  711. return defaultReturnValue;
  712. }
  713. XmlElement* XmlElement::createTextElement (const String& text)
  714. {
  715. XmlElement* const e = new XmlElement ((int) 0);
  716. e->setAttribute (water_xmltextContentAttributeName(), text);
  717. return e;
  718. }
  719. bool XmlElement::isValidXmlName (StringRef text) noexcept
  720. {
  721. if (text.isEmpty() || ! isValidXmlNameStartCharacter (text.text.getAndAdvance()))
  722. return false;
  723. for (;;)
  724. {
  725. if (text.isEmpty())
  726. return true;
  727. if (! isValidXmlNameBodyCharacter (text.text.getAndAdvance()))
  728. return false;
  729. }
  730. }
  731. void XmlElement::addTextElement (const String& text)
  732. {
  733. addChildElement (createTextElement (text));
  734. }
  735. void XmlElement::deleteAllTextElements() noexcept
  736. {
  737. for (XmlElement* child = firstChildElement; child != nullptr;)
  738. {
  739. XmlElement* const next = child->nextListItem;
  740. if (child->isTextElement())
  741. removeChildElement (child, true);
  742. child = next;
  743. }
  744. }
  745. }