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.

931 lines
28KB

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