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.

945 lines
28KB

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