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.

923 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. static const std::string& getEmptyStdStringRef() noexcept
  372. {
  373. static std::string empty;
  374. return empty;
  375. }
  376. const std::string& XmlElement::getAttributeName (const int index) const noexcept
  377. {
  378. if (const XmlAttributeNode* const att = attributes [index])
  379. return att->name.toString();
  380. return getEmptyStdStringRef();
  381. }
  382. const String& XmlElement::getAttributeValue (const int index) const noexcept
  383. {
  384. if (const XmlAttributeNode* const att = attributes [index])
  385. return att->value;
  386. return getEmptyStringRef();
  387. }
  388. XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
  389. {
  390. for (XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  391. if (att->name == attributeName)
  392. return att;
  393. return nullptr;
  394. }
  395. bool XmlElement::hasAttribute (StringRef attributeName) const noexcept
  396. {
  397. return getAttribute (attributeName) != nullptr;
  398. }
  399. //==============================================================================
  400. const String& XmlElement::getStringAttribute (StringRef attributeName) const noexcept
  401. {
  402. if (const XmlAttributeNode* att = getAttribute (attributeName))
  403. return att->value;
  404. return getEmptyStringRef();
  405. }
  406. String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
  407. {
  408. if (const XmlAttributeNode* att = getAttribute (attributeName))
  409. return att->value;
  410. return defaultReturnValue;
  411. }
  412. int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
  413. {
  414. if (const XmlAttributeNode* att = getAttribute (attributeName))
  415. return att->value.getIntValue();
  416. return defaultReturnValue;
  417. }
  418. double XmlElement::getDoubleAttribute (StringRef attributeName, const double defaultReturnValue) const
  419. {
  420. if (const XmlAttributeNode* att = getAttribute (attributeName))
  421. return att->value.getDoubleValue();
  422. return defaultReturnValue;
  423. }
  424. bool XmlElement::getBoolAttribute (StringRef attributeName, const bool defaultReturnValue) const
  425. {
  426. if (const XmlAttributeNode* att = getAttribute (attributeName))
  427. {
  428. const water_uchar firstChar = *(att->value.getCharPointer().findEndOfWhitespace());
  429. return firstChar == '1'
  430. || firstChar == 't'
  431. || firstChar == 'y'
  432. || firstChar == 'T'
  433. || firstChar == 'Y';
  434. }
  435. return defaultReturnValue;
  436. }
  437. bool XmlElement::compareAttribute (StringRef attributeName,
  438. StringRef stringToCompareAgainst,
  439. const bool ignoreCase) const noexcept
  440. {
  441. if (const XmlAttributeNode* att = getAttribute (attributeName))
  442. return ignoreCase ? att->value.equalsIgnoreCase (stringToCompareAgainst)
  443. : att->value == stringToCompareAgainst;
  444. return false;
  445. }
  446. //==============================================================================
  447. void XmlElement::setAttribute (const Identifier& attributeName, const String& value)
  448. {
  449. if (attributes == nullptr)
  450. {
  451. attributes = new XmlAttributeNode (attributeName, value);
  452. }
  453. else
  454. {
  455. for (XmlAttributeNode* att = attributes; ; att = att->nextListItem)
  456. {
  457. if (att->name == attributeName)
  458. {
  459. att->value = value;
  460. break;
  461. }
  462. if (att->nextListItem == nullptr)
  463. {
  464. att->nextListItem = new XmlAttributeNode (attributeName, value);
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. void XmlElement::setAttribute (const Identifier& attributeName, const int number)
  471. {
  472. setAttribute (attributeName, String (number));
  473. }
  474. void XmlElement::setAttribute (const Identifier& attributeName, const double number)
  475. {
  476. setAttribute (attributeName, String (number, 20));
  477. }
  478. void XmlElement::removeAttribute (const Identifier& attributeName) noexcept
  479. {
  480. for (LinkedListPointer<XmlAttributeNode>* att = &attributes;
  481. att->get() != nullptr;
  482. att = &(att->get()->nextListItem))
  483. {
  484. if (att->get()->name == attributeName)
  485. {
  486. delete att->removeNext();
  487. break;
  488. }
  489. }
  490. }
  491. void XmlElement::removeAllAttributes() noexcept
  492. {
  493. attributes.deleteAll();
  494. }
  495. //==============================================================================
  496. int XmlElement::getNumChildElements() const noexcept
  497. {
  498. return firstChildElement.size();
  499. }
  500. XmlElement* XmlElement::getChildElement (const int index) const noexcept
  501. {
  502. return firstChildElement [index].get();
  503. }
  504. XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept
  505. {
  506. wassert (! childName.isEmpty());
  507. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  508. if (child->hasTagName (childName))
  509. return child;
  510. return nullptr;
  511. }
  512. XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept
  513. {
  514. wassert (! attributeName.isEmpty());
  515. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  516. if (child->compareAttribute (attributeName, attributeValue))
  517. return child;
  518. return nullptr;
  519. }
  520. void XmlElement::addChildElement (XmlElement* const newNode) noexcept
  521. {
  522. if (newNode != nullptr)
  523. {
  524. // The element being added must not be a child of another node!
  525. wassert (newNode->nextListItem == nullptr);
  526. firstChildElement.append (newNode);
  527. }
  528. }
  529. void XmlElement::insertChildElement (XmlElement* const newNode, int indexToInsertAt) noexcept
  530. {
  531. if (newNode != nullptr)
  532. {
  533. // The element being added must not be a child of another node!
  534. wassert (newNode->nextListItem == nullptr);
  535. firstChildElement.insertAtIndex (indexToInsertAt, newNode);
  536. }
  537. }
  538. void XmlElement::prependChildElement (XmlElement* newNode) noexcept
  539. {
  540. if (newNode != nullptr)
  541. {
  542. // The element being added must not be a child of another node!
  543. wassert (newNode->nextListItem == nullptr);
  544. firstChildElement.insertNext (newNode);
  545. }
  546. }
  547. XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
  548. {
  549. XmlElement* const newElement = new XmlElement (childTagName);
  550. addChildElement (newElement);
  551. return newElement;
  552. }
  553. bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
  554. XmlElement* const newNode) noexcept
  555. {
  556. if (newNode != nullptr)
  557. {
  558. if (LinkedListPointer<XmlElement>* const p = firstChildElement.findPointerTo (currentChildElement))
  559. {
  560. if (currentChildElement != newNode)
  561. delete p->replaceNext (newNode);
  562. return true;
  563. }
  564. }
  565. return false;
  566. }
  567. void XmlElement::removeChildElement (XmlElement* const childToRemove,
  568. const bool shouldDeleteTheChild) noexcept
  569. {
  570. if (childToRemove != nullptr)
  571. {
  572. firstChildElement.remove (childToRemove);
  573. if (shouldDeleteTheChild)
  574. delete childToRemove;
  575. }
  576. }
  577. bool XmlElement::isEquivalentTo (const XmlElement* const other,
  578. const bool ignoreOrderOfAttributes) const noexcept
  579. {
  580. if (this != other)
  581. {
  582. if (other == nullptr || tagName != other->tagName)
  583. return false;
  584. if (ignoreOrderOfAttributes)
  585. {
  586. int totalAtts = 0;
  587. for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
  588. {
  589. if (! other->compareAttribute (att->name, att->value))
  590. return false;
  591. ++totalAtts;
  592. }
  593. if (totalAtts != other->getNumAttributes())
  594. return false;
  595. }
  596. else
  597. {
  598. const XmlAttributeNode* thisAtt = attributes;
  599. const XmlAttributeNode* otherAtt = other->attributes;
  600. for (;;)
  601. {
  602. if (thisAtt == nullptr || otherAtt == nullptr)
  603. {
  604. if (thisAtt == otherAtt) // both nullptr, so it's a match
  605. break;
  606. return false;
  607. }
  608. if (thisAtt->name != otherAtt->name
  609. || thisAtt->value != otherAtt->value)
  610. {
  611. return false;
  612. }
  613. thisAtt = thisAtt->nextListItem;
  614. otherAtt = otherAtt->nextListItem;
  615. }
  616. }
  617. const XmlElement* thisChild = firstChildElement;
  618. const XmlElement* otherChild = other->firstChildElement;
  619. for (;;)
  620. {
  621. if (thisChild == nullptr || otherChild == nullptr)
  622. {
  623. if (thisChild == otherChild) // both 0, so it's a match
  624. break;
  625. return false;
  626. }
  627. if (! thisChild->isEquivalentTo (otherChild, ignoreOrderOfAttributes))
  628. return false;
  629. thisChild = thisChild->nextListItem;
  630. otherChild = otherChild->nextListItem;
  631. }
  632. }
  633. return true;
  634. }
  635. void XmlElement::deleteAllChildElements() noexcept
  636. {
  637. firstChildElement.deleteAll();
  638. }
  639. void XmlElement::deleteAllChildElementsWithTagName (StringRef name) noexcept
  640. {
  641. for (XmlElement* child = firstChildElement; child != nullptr;)
  642. {
  643. XmlElement* const nextChild = child->nextListItem;
  644. if (child->hasTagName (name))
  645. removeChildElement (child, true);
  646. child = nextChild;
  647. }
  648. }
  649. bool XmlElement::containsChildElement (const XmlElement* const possibleChild) const noexcept
  650. {
  651. return firstChildElement.contains (possibleChild);
  652. }
  653. XmlElement* XmlElement::findParentElementOf (const XmlElement* const elementToLookFor) noexcept
  654. {
  655. if (this == elementToLookFor || elementToLookFor == nullptr)
  656. return nullptr;
  657. for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  658. {
  659. if (elementToLookFor == child)
  660. return this;
  661. if (XmlElement* const found = child->findParentElementOf (elementToLookFor))
  662. return found;
  663. }
  664. return nullptr;
  665. }
  666. void XmlElement::getChildElementsAsArray (XmlElement** elems) const noexcept
  667. {
  668. firstChildElement.copyToArray (elems);
  669. }
  670. void XmlElement::reorderChildElements (XmlElement** const elems, const int num) noexcept
  671. {
  672. XmlElement* e = firstChildElement = elems[0];
  673. for (int i = 1; i < num; ++i)
  674. {
  675. e->nextListItem = elems[i];
  676. e = e->nextListItem;
  677. }
  678. e->nextListItem = nullptr;
  679. }
  680. //==============================================================================
  681. bool XmlElement::isTextElement() const noexcept
  682. {
  683. return tagName.isEmpty();
  684. }
  685. static const char* const water_xmltextContentAttributeName = "text";
  686. const String& XmlElement::getText() const noexcept
  687. {
  688. wassert (isTextElement()); // you're trying to get the text from an element that
  689. // isn't actually a text element.. If this contains text sub-nodes, you
  690. // probably want to use getAllSubText instead.
  691. return getStringAttribute (water_xmltextContentAttributeName);
  692. }
  693. void XmlElement::setText (const String& newText)
  694. {
  695. CARLA_SAFE_ASSERT_RETURN(isTextElement(),);
  696. setAttribute (water_xmltextContentAttributeName, newText);
  697. }
  698. String XmlElement::getAllSubText() const
  699. {
  700. if (isTextElement())
  701. return getText();
  702. if (getNumChildElements() == 1)
  703. return firstChildElement.get()->getAllSubText();
  704. MemoryOutputStream mem (1024);
  705. for (const XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
  706. mem << child->getAllSubText();
  707. return mem.toUTF8();
  708. }
  709. String XmlElement::getChildElementAllSubText (StringRef childTagName, const String& defaultReturnValue) const
  710. {
  711. if (const XmlElement* const child = getChildByName (childTagName))
  712. return child->getAllSubText();
  713. return defaultReturnValue;
  714. }
  715. XmlElement* XmlElement::createTextElement (const String& text)
  716. {
  717. XmlElement* const e = new XmlElement ((int) 0);
  718. e->setAttribute (water_xmltextContentAttributeName, text);
  719. return e;
  720. }
  721. bool XmlElement::isValidXmlName (StringRef text) noexcept
  722. {
  723. if (text.isEmpty() || ! isValidXmlNameStartCharacter (text.text.getAndAdvance()))
  724. return false;
  725. for (;;)
  726. {
  727. if (text.isEmpty())
  728. return true;
  729. if (! isValidXmlNameBodyCharacter (text.text.getAndAdvance()))
  730. return false;
  731. }
  732. }
  733. void XmlElement::addTextElement (const String& text)
  734. {
  735. addChildElement (createTextElement (text));
  736. }
  737. void XmlElement::deleteAllTextElements() noexcept
  738. {
  739. for (XmlElement* child = firstChildElement; child != nullptr;)
  740. {
  741. XmlElement* const next = child->nextListItem;
  742. if (child->isTextElement())
  743. removeChildElement (child, true);
  744. child = next;
  745. }
  746. }
  747. }