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.

858 lines
26KB

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