juce_XmlElement.cpp 27 KB

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