Audio plugin host https://kx.studio/carla
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.

juce_XmlElement.cpp 28KB

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