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.

924 lines
28KB

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