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.

941 lines
28KB

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