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.

925 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. inline static bool isValidXmlNameStartCharacter (juce_wchar character) noexcept
  20. {
  21. return character == ':'
  22. || character == '_'
  23. || (character >= 'a' && character <= 'z')
  24. || (character >= 'A' && character <= 'Z')
  25. || (character >= 0xc0 && character <= 0xd6)
  26. || (character >= 0xd8 && character <= 0xf6)
  27. || (character >= 0xf8 && character <= 0x2ff)
  28. || (character >= 0x370 && character <= 0x37d)
  29. || (character >= 0x37f && character <= 0x1fff)
  30. || (character >= 0x200c && character <= 0x200d)
  31. || (character >= 0x2070 && character <= 0x218f)
  32. || (character >= 0x2c00 && character <= 0x2fef)
  33. || (character >= 0x3001 && character <= 0xd7ff)
  34. || (character >= 0xf900 && character <= 0xfdcf)
  35. || (character >= 0xfdf0 && character <= 0xfffd)
  36. || (character >= 0x10000 && character <= 0xeffff);
  37. }
  38. inline static bool isValidXmlNameBodyCharacter (juce_wchar character) noexcept
  39. {
  40. return isValidXmlNameStartCharacter (character)
  41. || character == '-'
  42. || character == '.'
  43. || character == 0xb7
  44. || (character >= '0' && character <= '9')
  45. || (character >= 0x300 && character <= 0x036f)
  46. || (character >= 0x203f && character <= 0x2040);
  47. }
  48. XmlElement::XmlAttributeNode::XmlAttributeNode (const XmlAttributeNode& other) noexcept
  49. : name (other.name),
  50. value (other.value)
  51. {
  52. }
  53. XmlElement::XmlAttributeNode::XmlAttributeNode (const Identifier& n, const String& v) noexcept
  54. : name (n), value (v)
  55. {
  56. jassert (isValidXmlName (name));
  57. }
  58. XmlElement::XmlAttributeNode::XmlAttributeNode (String::CharPointerType nameStart, String::CharPointerType nameEnd)
  59. : name (nameStart, nameEnd)
  60. {
  61. jassert (isValidXmlName (name));
  62. }
  63. //==============================================================================
  64. XmlElement::XmlElement (const String& tag)
  65. : tagName (StringPool::getGlobalPool().getPooledString (tag))
  66. {
  67. jassert (isValidXmlName (tagName));
  68. }
  69. XmlElement::XmlElement (const char* tag)
  70. : tagName (StringPool::getGlobalPool().getPooledString (tag))
  71. {
  72. jassert (isValidXmlName (tagName));
  73. }
  74. XmlElement::XmlElement (StringRef tag)
  75. : tagName (StringPool::getGlobalPool().getPooledString (tag))
  76. {
  77. jassert (isValidXmlName (tagName));
  78. }
  79. XmlElement::XmlElement (const Identifier& tag)
  80. : tagName (tag.toString())
  81. {
  82. jassert (isValidXmlName (tagName));
  83. }
  84. XmlElement::XmlElement (String::CharPointerType tagNameStart, String::CharPointerType tagNameEnd)
  85. : tagName (StringPool::getGlobalPool().getPooledString (tagNameStart, tagNameEnd))
  86. {
  87. jassert (isValidXmlName (tagName));
  88. }
  89. XmlElement::XmlElement (int /*dummy*/) noexcept
  90. {
  91. }
  92. XmlElement::XmlElement (const XmlElement& other)
  93. : tagName (other.tagName)
  94. {
  95. copyChildrenAndAttributesFrom (other);
  96. }
  97. XmlElement& XmlElement::operator= (const XmlElement& other)
  98. {
  99. if (this != &other)
  100. {
  101. removeAllAttributes();
  102. deleteAllChildElements();
  103. tagName = other.tagName;
  104. copyChildrenAndAttributesFrom (other);
  105. }
  106. return *this;
  107. }
  108. XmlElement::XmlElement (XmlElement&& other) noexcept
  109. : nextListItem (static_cast<LinkedListPointer<XmlElement>&&> (other.nextListItem)),
  110. firstChildElement (static_cast<LinkedListPointer<XmlElement>&&> (other.firstChildElement)),
  111. attributes (static_cast<LinkedListPointer<XmlAttributeNode>&&> (other.attributes)),
  112. tagName (static_cast<String&&> (other.tagName))
  113. {
  114. }
  115. XmlElement& XmlElement::operator= (XmlElement&& other) noexcept
  116. {
  117. jassert (this != &other); // hopefully the compiler should make this situation impossible!
  118. removeAllAttributes();
  119. deleteAllChildElements();
  120. nextListItem = static_cast<LinkedListPointer<XmlElement>&&> (other.nextListItem);
  121. firstChildElement = static_cast<LinkedListPointer<XmlElement>&&> (other.firstChildElement);
  122. attributes = static_cast<LinkedListPointer<XmlAttributeNode>&&> (other.attributes);
  123. tagName = static_cast<String&&> (other.tagName);
  124. return *this;
  125. }
  126. void XmlElement::copyChildrenAndAttributesFrom (const XmlElement& other)
  127. {
  128. jassert (firstChildElement.get() == nullptr);
  129. firstChildElement.addCopyOfList (other.firstChildElement);
  130. jassert (attributes.get() == nullptr);
  131. attributes.addCopyOfList (other.attributes);
  132. }
  133. XmlElement::~XmlElement() noexcept
  134. {
  135. firstChildElement.deleteAll();
  136. attributes.deleteAll();
  137. }
  138. //==============================================================================
  139. namespace XmlOutputFunctions
  140. {
  141. #if 0 // (These functions are just used to generate the lookup table used below)
  142. bool isLegalXmlCharSlow (const juce_wchar character) noexcept
  143. {
  144. if ((character >= 'a' && character <= 'z')
  145. || (character >= 'A' && character <= 'Z')
  146. || (character >= '0' && character <= '9'))
  147. return true;
  148. const char* t = " .,;:-()_+=?!'#@[]/\\*%~{}$|";
  149. do
  150. {
  151. if (((juce_wchar) (uint8) *t) == character)
  152. return true;
  153. }
  154. while (*++t != 0);
  155. return false;
  156. }
  157. void generateLegalCharLookupTable()
  158. {
  159. uint8 n[32] = { 0 };
  160. for (int i = 0; i < 256; ++i)
  161. if (isLegalXmlCharSlow (i))
  162. n[i >> 3] |= (1 << (i & 7));
  163. String s;
  164. for (int i = 0; i < 32; ++i)
  165. s << (int) n[i] << ", ";
  166. DBG (s);
  167. }
  168. #endif
  169. static bool isLegalXmlChar (const uint32 c) noexcept
  170. {
  171. static const unsigned char legalChars[] = { 0, 0, 0, 0, 187, 255, 255, 175, 255,
  172. 255, 255, 191, 254, 255, 255, 127 };
  173. return c < sizeof (legalChars) * 8
  174. && (legalChars [c >> 3] & (1 << (c & 7))) != 0;
  175. }
  176. static void escapeIllegalXmlChars (OutputStream& outputStream, const String& text, const bool changeNewLines)
  177. {
  178. auto t = text.getCharPointer();
  179. for (;;)
  180. {
  181. auto character = (uint32) t.getAndAdvance();
  182. if (character == 0)
  183. break;
  184. if (isLegalXmlChar (character))
  185. {
  186. outputStream << (char) character;
  187. }
  188. else
  189. {
  190. switch (character)
  191. {
  192. case '&': outputStream << "&amp;"; break;
  193. case '"': outputStream << "&quot;"; break;
  194. case '>': outputStream << "&gt;"; break;
  195. case '<': outputStream << "&lt;"; break;
  196. case '\n':
  197. case '\r':
  198. if (! changeNewLines)
  199. {
  200. outputStream << (char) character;
  201. break;
  202. }
  203. // Note: deliberate fall-through here!
  204. default:
  205. outputStream << "&#" << ((int) character) << ';';
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. static void writeSpaces (OutputStream& out, const size_t numSpaces)
  212. {
  213. out.writeRepeatedByte (' ', numSpaces);
  214. }
  215. }
  216. void XmlElement::writeElementAsText (OutputStream& outputStream,
  217. const int indentationLevel,
  218. const int lineWrapLength) const
  219. {
  220. using namespace XmlOutputFunctions;
  221. if (indentationLevel >= 0)
  222. writeSpaces (outputStream, (size_t) indentationLevel);
  223. if (! isTextElement())
  224. {
  225. outputStream.writeByte ('<');
  226. outputStream << tagName;
  227. {
  228. auto attIndent = (size_t) (indentationLevel + tagName.length() + 1);
  229. int lineLen = 0;
  230. for (auto* att = attributes.get(); att != nullptr; att = att->nextListItem)
  231. {
  232. if (lineLen > lineWrapLength && indentationLevel >= 0)
  233. {
  234. outputStream << newLine;
  235. writeSpaces (outputStream, attIndent);
  236. lineLen = 0;
  237. }
  238. auto startPos = outputStream.getPosition();
  239. outputStream.writeByte (' ');
  240. outputStream << att->name;
  241. outputStream.write ("=\"", 2);
  242. escapeIllegalXmlChars (outputStream, att->value, true);
  243. outputStream.writeByte ('"');
  244. lineLen += (int) (outputStream.getPosition() - startPos);
  245. }
  246. }
  247. if (auto* child = firstChildElement.get())
  248. {
  249. outputStream.writeByte ('>');
  250. bool lastWasTextNode = false;
  251. for (; child != nullptr; child = child->nextListItem)
  252. {
  253. if (child->isTextElement())
  254. {
  255. escapeIllegalXmlChars (outputStream, child->getText(), false);
  256. lastWasTextNode = true;
  257. }
  258. else
  259. {
  260. if (indentationLevel >= 0 && ! lastWasTextNode)
  261. outputStream << newLine;
  262. child->writeElementAsText (outputStream,
  263. lastWasTextNode ? 0 : (indentationLevel + (indentationLevel >= 0 ? 2 : 0)), lineWrapLength);
  264. lastWasTextNode = false;
  265. }
  266. }
  267. if (indentationLevel >= 0 && ! lastWasTextNode)
  268. {
  269. outputStream << newLine;
  270. writeSpaces (outputStream, (size_t) indentationLevel);
  271. }
  272. outputStream.write ("</", 2);
  273. outputStream << tagName;
  274. outputStream.writeByte ('>');
  275. }
  276. else
  277. {
  278. outputStream.write ("/>", 2);
  279. }
  280. }
  281. else
  282. {
  283. escapeIllegalXmlChars (outputStream, getText(), false);
  284. }
  285. }
  286. String XmlElement::createDocument (StringRef dtdToUse,
  287. const bool allOnOneLine,
  288. const bool includeXmlHeader,
  289. StringRef encodingType,
  290. const int lineWrapLength) const
  291. {
  292. MemoryOutputStream mem (2048);
  293. writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);
  294. return mem.toUTF8();
  295. }
  296. void XmlElement::writeToStream (OutputStream& output, StringRef dtdToUse,
  297. bool allOnOneLine, bool includeXmlHeader,
  298. StringRef encodingType, int lineWrapLength) const
  299. {
  300. using namespace XmlOutputFunctions;
  301. if (includeXmlHeader)
  302. {
  303. output << "<?xml version=\"1.0\" encoding=\"" << encodingType << "\"?>";
  304. if (allOnOneLine)
  305. output.writeByte (' ');
  306. else
  307. output << newLine << newLine;
  308. }
  309. if (dtdToUse.isNotEmpty())
  310. {
  311. output << dtdToUse;
  312. if (allOnOneLine)
  313. output.writeByte (' ');
  314. else
  315. output << newLine;
  316. }
  317. writeElementAsText (output, allOnOneLine ? -1 : 0, lineWrapLength);
  318. if (! allOnOneLine)
  319. output << newLine;
  320. }
  321. bool XmlElement::writeToFile (const File& file, StringRef dtdToUse,
  322. StringRef encodingType, int lineWrapLength) const
  323. {
  324. TemporaryFile tempFile (file);
  325. {
  326. FileOutputStream out (tempFile.getFile());
  327. if (! out.openedOk())
  328. return false;
  329. writeToStream (out, dtdToUse, false, true, encodingType, lineWrapLength);
  330. out.flush(); // (called explicitly to force an fsync on posix)
  331. if (out.getStatus().failed())
  332. return false;
  333. }
  334. return tempFile.overwriteTargetFileWithTemporary();
  335. }
  336. //==============================================================================
  337. bool XmlElement::hasTagName (StringRef possibleTagName) const noexcept
  338. {
  339. const bool matches = tagName.equalsIgnoreCase (possibleTagName);
  340. // XML tags should be case-sensitive, so although this method allows a
  341. // case-insensitive match to pass, you should try to avoid this.
  342. jassert ((! matches) || tagName == possibleTagName);
  343. return matches;
  344. }
  345. String XmlElement::getNamespace() const
  346. {
  347. return tagName.upToFirstOccurrenceOf (":", false, false);
  348. }
  349. String XmlElement::getTagNameWithoutNamespace() const
  350. {
  351. return tagName.fromLastOccurrenceOf (":", false, false);
  352. }
  353. bool XmlElement::hasTagNameIgnoringNamespace (StringRef possibleTagName) const
  354. {
  355. return hasTagName (possibleTagName) || getTagNameWithoutNamespace() == possibleTagName;
  356. }
  357. XmlElement* XmlElement::getNextElementWithTagName (StringRef requiredTagName) const
  358. {
  359. auto* e = nextListItem.get();
  360. while (e != nullptr && ! e->hasTagName (requiredTagName))
  361. e = e->nextListItem;
  362. return e;
  363. }
  364. //==============================================================================
  365. int XmlElement::getNumAttributes() const noexcept
  366. {
  367. return attributes.size();
  368. }
  369. static const String& getEmptyStringRef() noexcept
  370. {
  371. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  372. return String::empty;
  373. #else
  374. static String empty;
  375. return empty;
  376. #endif
  377. }
  378. const String& XmlElement::getAttributeName (const int index) const noexcept
  379. {
  380. if (auto* att = attributes[index].get())
  381. return att->name.toString();
  382. return getEmptyStringRef();
  383. }
  384. const String& XmlElement::getAttributeValue (const int index) const noexcept
  385. {
  386. if (auto* att = attributes[index].get())
  387. return att->value;
  388. return getEmptyStringRef();
  389. }
  390. XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
  391. {
  392. for (auto* att = attributes.get(); 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 (auto* att = getAttribute (attributeName))
  405. return att->value;
  406. return getEmptyStringRef();
  407. }
  408. String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
  409. {
  410. if (auto* att = getAttribute (attributeName))
  411. return att->value;
  412. return defaultReturnValue;
  413. }
  414. int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
  415. {
  416. if (auto* 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 (auto* 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 (auto* att = getAttribute (attributeName))
  429. {
  430. auto 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 (auto* 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 (auto* att = attributes.get(); ; 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 (auto* att = &attributes; att->get() != nullptr; 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. jassert (! childName.isEmpty());
  507. for (auto* child = firstChildElement.get(); 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. jassert (! attributeName.isEmpty());
  515. for (auto* child = firstChildElement.get(); 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. jassert (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. jassert (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. jassert (newNode->nextListItem == nullptr);
  544. firstChildElement.insertNext (newNode);
  545. }
  546. }
  547. XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
  548. {
  549. auto 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 (auto* 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 (auto* att = attributes.get(); 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. auto* thisAtt = attributes.get();
  599. auto* otherAtt = other->attributes.get();
  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. auto* thisChild = firstChildElement.get();
  618. auto* otherChild = other->firstChildElement.get();
  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 (auto* child = firstChildElement.get(); child != nullptr;)
  642. {
  643. auto* nextChild = child->nextListItem.get();
  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 (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
  658. {
  659. if (elementToLookFor == child)
  660. return this;
  661. if (auto* 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** elems, int num) noexcept
  671. {
  672. auto* e = elems[0];
  673. firstChildElement = e;
  674. for (int i = 1; i < num; ++i)
  675. {
  676. e->nextListItem = elems[i];
  677. e = e->nextListItem;
  678. }
  679. e->nextListItem = nullptr;
  680. }
  681. //==============================================================================
  682. bool XmlElement::isTextElement() const noexcept
  683. {
  684. return tagName.isEmpty();
  685. }
  686. static const String juce_xmltextContentAttributeName ("text");
  687. const String& XmlElement::getText() const noexcept
  688. {
  689. jassert (isTextElement()); // you're trying to get the text from an element that
  690. // isn't actually a text element.. If this contains text sub-nodes, you
  691. // probably want to use getAllSubText instead.
  692. return getStringAttribute (juce_xmltextContentAttributeName);
  693. }
  694. void XmlElement::setText (const String& newText)
  695. {
  696. if (isTextElement())
  697. setAttribute (juce_xmltextContentAttributeName, newText);
  698. else
  699. jassertfalse; // you can only change the text in a text element, not a normal one.
  700. }
  701. String XmlElement::getAllSubText() const
  702. {
  703. if (isTextElement())
  704. return getText();
  705. if (getNumChildElements() == 1)
  706. return firstChildElement.get()->getAllSubText();
  707. MemoryOutputStream mem (1024);
  708. for (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
  709. mem << child->getAllSubText();
  710. return mem.toUTF8();
  711. }
  712. String XmlElement::getChildElementAllSubText (StringRef childTagName, const String& defaultReturnValue) const
  713. {
  714. if (auto* child = getChildByName (childTagName))
  715. return child->getAllSubText();
  716. return defaultReturnValue;
  717. }
  718. XmlElement* XmlElement::createTextElement (const String& text)
  719. {
  720. auto e = new XmlElement ((int) 0);
  721. e->setAttribute (juce_xmltextContentAttributeName, text);
  722. return e;
  723. }
  724. bool XmlElement::isValidXmlName (StringRef text) noexcept
  725. {
  726. if (text.isEmpty() || ! isValidXmlNameStartCharacter (text.text.getAndAdvance()))
  727. return false;
  728. for (;;)
  729. {
  730. if (text.isEmpty())
  731. return true;
  732. if (! isValidXmlNameBodyCharacter (text.text.getAndAdvance()))
  733. return false;
  734. }
  735. }
  736. void XmlElement::addTextElement (const String& text)
  737. {
  738. addChildElement (createTextElement (text));
  739. }
  740. void XmlElement::deleteAllTextElements() noexcept
  741. {
  742. for (auto* child = firstChildElement.get(); child != nullptr;)
  743. {
  744. auto* next = child->nextListItem.get();
  745. if (child->isTextElement())
  746. removeChildElement (child, true);
  747. child = next;
  748. }
  749. }
  750. } // namespace juce