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.

927 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. void XmlElement::setTagName (StringRef newTagName)
  365. {
  366. jassert (isValidXmlName (newTagName));
  367. tagName = StringPool::getGlobalPool().getPooledString (newTagName);
  368. }
  369. //==============================================================================
  370. int XmlElement::getNumAttributes() const noexcept
  371. {
  372. return attributes.size();
  373. }
  374. static const String& getEmptyStringRef() noexcept
  375. {
  376. static String empty;
  377. return empty;
  378. }
  379. const String& XmlElement::getAttributeName (const int index) const noexcept
  380. {
  381. if (auto* att = attributes[index].get())
  382. return att->name.toString();
  383. return getEmptyStringRef();
  384. }
  385. const String& XmlElement::getAttributeValue (const int index) const noexcept
  386. {
  387. if (auto* att = attributes[index].get())
  388. return att->value;
  389. return getEmptyStringRef();
  390. }
  391. XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
  392. {
  393. for (auto* att = attributes.get(); att != nullptr; att = att->nextListItem)
  394. if (att->name == attributeName)
  395. return att;
  396. return nullptr;
  397. }
  398. bool XmlElement::hasAttribute (StringRef attributeName) const noexcept
  399. {
  400. return getAttribute (attributeName) != nullptr;
  401. }
  402. //==============================================================================
  403. const String& XmlElement::getStringAttribute (StringRef attributeName) const noexcept
  404. {
  405. if (auto* att = getAttribute (attributeName))
  406. return att->value;
  407. return getEmptyStringRef();
  408. }
  409. String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
  410. {
  411. if (auto* att = getAttribute (attributeName))
  412. return att->value;
  413. return defaultReturnValue;
  414. }
  415. int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
  416. {
  417. if (auto* att = getAttribute (attributeName))
  418. return att->value.getIntValue();
  419. return defaultReturnValue;
  420. }
  421. double XmlElement::getDoubleAttribute (StringRef attributeName, const double defaultReturnValue) const
  422. {
  423. if (auto* att = getAttribute (attributeName))
  424. return att->value.getDoubleValue();
  425. return defaultReturnValue;
  426. }
  427. bool XmlElement::getBoolAttribute (StringRef attributeName, const bool defaultReturnValue) const
  428. {
  429. if (auto* att = getAttribute (attributeName))
  430. {
  431. auto firstChar = *(att->value.getCharPointer().findEndOfWhitespace());
  432. return firstChar == '1'
  433. || firstChar == 't'
  434. || firstChar == 'y'
  435. || firstChar == 'T'
  436. || firstChar == 'Y';
  437. }
  438. return defaultReturnValue;
  439. }
  440. bool XmlElement::compareAttribute (StringRef attributeName,
  441. StringRef stringToCompareAgainst,
  442. const bool ignoreCase) const noexcept
  443. {
  444. if (auto* att = getAttribute (attributeName))
  445. return ignoreCase ? att->value.equalsIgnoreCase (stringToCompareAgainst)
  446. : att->value == stringToCompareAgainst;
  447. return false;
  448. }
  449. //==============================================================================
  450. void XmlElement::setAttribute (const Identifier& attributeName, const String& value)
  451. {
  452. if (attributes == nullptr)
  453. {
  454. attributes = new XmlAttributeNode (attributeName, value);
  455. }
  456. else
  457. {
  458. for (auto* att = attributes.get(); ; att = att->nextListItem)
  459. {
  460. if (att->name == attributeName)
  461. {
  462. att->value = value;
  463. break;
  464. }
  465. if (att->nextListItem == nullptr)
  466. {
  467. att->nextListItem = new XmlAttributeNode (attributeName, value);
  468. break;
  469. }
  470. }
  471. }
  472. }
  473. void XmlElement::setAttribute (const Identifier& attributeName, const int number)
  474. {
  475. setAttribute (attributeName, String (number));
  476. }
  477. void XmlElement::setAttribute (const Identifier& attributeName, const double number)
  478. {
  479. setAttribute (attributeName, String (number, 20));
  480. }
  481. void XmlElement::removeAttribute (const Identifier& attributeName) noexcept
  482. {
  483. for (auto* att = &attributes; att->get() != nullptr; att = &(att->get()->nextListItem))
  484. {
  485. if (att->get()->name == attributeName)
  486. {
  487. delete att->removeNext();
  488. break;
  489. }
  490. }
  491. }
  492. void XmlElement::removeAllAttributes() noexcept
  493. {
  494. attributes.deleteAll();
  495. }
  496. //==============================================================================
  497. int XmlElement::getNumChildElements() const noexcept
  498. {
  499. return firstChildElement.size();
  500. }
  501. XmlElement* XmlElement::getChildElement (const int index) const noexcept
  502. {
  503. return firstChildElement [index].get();
  504. }
  505. XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept
  506. {
  507. jassert (! childName.isEmpty());
  508. for (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
  509. if (child->hasTagName (childName))
  510. return child;
  511. return nullptr;
  512. }
  513. XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept
  514. {
  515. jassert (! attributeName.isEmpty());
  516. for (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
  517. if (child->compareAttribute (attributeName, attributeValue))
  518. return child;
  519. return nullptr;
  520. }
  521. void XmlElement::addChildElement (XmlElement* const newNode) noexcept
  522. {
  523. if (newNode != nullptr)
  524. {
  525. // The element being added must not be a child of another node!
  526. jassert (newNode->nextListItem == nullptr);
  527. firstChildElement.append (newNode);
  528. }
  529. }
  530. void XmlElement::insertChildElement (XmlElement* const newNode, int indexToInsertAt) noexcept
  531. {
  532. if (newNode != nullptr)
  533. {
  534. // The element being added must not be a child of another node!
  535. jassert (newNode->nextListItem == nullptr);
  536. firstChildElement.insertAtIndex (indexToInsertAt, newNode);
  537. }
  538. }
  539. void XmlElement::prependChildElement (XmlElement* newNode) noexcept
  540. {
  541. if (newNode != nullptr)
  542. {
  543. // The element being added must not be a child of another node!
  544. jassert (newNode->nextListItem == nullptr);
  545. firstChildElement.insertNext (newNode);
  546. }
  547. }
  548. XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
  549. {
  550. auto newElement = new XmlElement (childTagName);
  551. addChildElement (newElement);
  552. return newElement;
  553. }
  554. bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
  555. XmlElement* const newNode) noexcept
  556. {
  557. if (newNode != nullptr)
  558. {
  559. if (auto* p = firstChildElement.findPointerTo (currentChildElement))
  560. {
  561. if (currentChildElement != newNode)
  562. delete p->replaceNext (newNode);
  563. return true;
  564. }
  565. }
  566. return false;
  567. }
  568. void XmlElement::removeChildElement (XmlElement* const childToRemove,
  569. const bool shouldDeleteTheChild) noexcept
  570. {
  571. if (childToRemove != nullptr)
  572. {
  573. firstChildElement.remove (childToRemove);
  574. if (shouldDeleteTheChild)
  575. delete childToRemove;
  576. }
  577. }
  578. bool XmlElement::isEquivalentTo (const XmlElement* const other,
  579. const bool ignoreOrderOfAttributes) const noexcept
  580. {
  581. if (this != other)
  582. {
  583. if (other == nullptr || tagName != other->tagName)
  584. return false;
  585. if (ignoreOrderOfAttributes)
  586. {
  587. int totalAtts = 0;
  588. for (auto* att = attributes.get(); att != nullptr; att = att->nextListItem)
  589. {
  590. if (! other->compareAttribute (att->name, att->value))
  591. return false;
  592. ++totalAtts;
  593. }
  594. if (totalAtts != other->getNumAttributes())
  595. return false;
  596. }
  597. else
  598. {
  599. auto* thisAtt = attributes.get();
  600. auto* otherAtt = other->attributes.get();
  601. for (;;)
  602. {
  603. if (thisAtt == nullptr || otherAtt == nullptr)
  604. {
  605. if (thisAtt == otherAtt) // both nullptr, so it's a match
  606. break;
  607. return false;
  608. }
  609. if (thisAtt->name != otherAtt->name
  610. || thisAtt->value != otherAtt->value)
  611. {
  612. return false;
  613. }
  614. thisAtt = thisAtt->nextListItem;
  615. otherAtt = otherAtt->nextListItem;
  616. }
  617. }
  618. auto* thisChild = firstChildElement.get();
  619. auto* otherChild = other->firstChildElement.get();
  620. for (;;)
  621. {
  622. if (thisChild == nullptr || otherChild == nullptr)
  623. {
  624. if (thisChild == otherChild) // both 0, so it's a match
  625. break;
  626. return false;
  627. }
  628. if (! thisChild->isEquivalentTo (otherChild, ignoreOrderOfAttributes))
  629. return false;
  630. thisChild = thisChild->nextListItem;
  631. otherChild = otherChild->nextListItem;
  632. }
  633. }
  634. return true;
  635. }
  636. void XmlElement::deleteAllChildElements() noexcept
  637. {
  638. firstChildElement.deleteAll();
  639. }
  640. void XmlElement::deleteAllChildElementsWithTagName (StringRef name) noexcept
  641. {
  642. for (auto* child = firstChildElement.get(); child != nullptr;)
  643. {
  644. auto* nextChild = child->nextListItem.get();
  645. if (child->hasTagName (name))
  646. removeChildElement (child, true);
  647. child = nextChild;
  648. }
  649. }
  650. bool XmlElement::containsChildElement (const XmlElement* const possibleChild) const noexcept
  651. {
  652. return firstChildElement.contains (possibleChild);
  653. }
  654. XmlElement* XmlElement::findParentElementOf (const XmlElement* const elementToLookFor) noexcept
  655. {
  656. if (this == elementToLookFor || elementToLookFor == nullptr)
  657. return nullptr;
  658. for (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
  659. {
  660. if (elementToLookFor == child)
  661. return this;
  662. if (auto* found = child->findParentElementOf (elementToLookFor))
  663. return found;
  664. }
  665. return nullptr;
  666. }
  667. void XmlElement::getChildElementsAsArray (XmlElement** elems) const noexcept
  668. {
  669. firstChildElement.copyToArray (elems);
  670. }
  671. void XmlElement::reorderChildElements (XmlElement** elems, int num) noexcept
  672. {
  673. auto* e = elems[0];
  674. firstChildElement = e;
  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 (auto* child = firstChildElement.get(); 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 (auto* child = getChildByName (childTagName))
  716. return child->getAllSubText();
  717. return defaultReturnValue;
  718. }
  719. XmlElement* XmlElement::createTextElement (const String& text)
  720. {
  721. auto 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 (auto* child = firstChildElement.get(); child != nullptr;)
  744. {
  745. auto* next = child->nextListItem.get();
  746. if (child->isTextElement())
  747. removeChildElement (child, true);
  748. child = next;
  749. }
  750. }
  751. } // namespace juce