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_XmlDocument.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. XmlDocument::XmlDocument (const String& documentText)
  24. : originalText (documentText),
  25. input (nullptr),
  26. outOfData (false),
  27. errorOccurred (false),
  28. needToLoadDTD (false),
  29. ignoreEmptyTextElements (true)
  30. {
  31. }
  32. XmlDocument::XmlDocument (const File& file)
  33. : input (nullptr),
  34. outOfData (false),
  35. errorOccurred (false),
  36. needToLoadDTD (false),
  37. ignoreEmptyTextElements (true),
  38. inputSource (new FileInputSource (file))
  39. {
  40. }
  41. XmlDocument::~XmlDocument()
  42. {
  43. }
  44. XmlElement* XmlDocument::parse (const File& file)
  45. {
  46. XmlDocument doc (file);
  47. return doc.getDocumentElement();
  48. }
  49. XmlElement* XmlDocument::parse (const String& xmlData)
  50. {
  51. XmlDocument doc (xmlData);
  52. return doc.getDocumentElement();
  53. }
  54. void XmlDocument::setInputSource (InputSource* const newSource) noexcept
  55. {
  56. inputSource = newSource;
  57. }
  58. void XmlDocument::setEmptyTextElementsIgnored (const bool shouldBeIgnored) noexcept
  59. {
  60. ignoreEmptyTextElements = shouldBeIgnored;
  61. }
  62. namespace XmlIdentifierChars
  63. {
  64. static bool isIdentifierCharSlow (const juce_wchar c) noexcept
  65. {
  66. return CharacterFunctions::isLetterOrDigit (c)
  67. || c == '_' || c == '-' || c == ':' || c == '.';
  68. }
  69. static bool isIdentifierChar (const juce_wchar c) noexcept
  70. {
  71. static const uint32 legalChars[] = { 0, 0x7ff6000, 0x87fffffe, 0x7fffffe, 0 };
  72. return ((int) c < (int) numElementsInArray (legalChars) * 32) ? ((legalChars [c >> 5] & (1 << (c & 31))) != 0)
  73. : isIdentifierCharSlow (c);
  74. }
  75. /*static void generateIdentifierCharConstants()
  76. {
  77. uint32 n[8] = { 0 };
  78. for (int i = 0; i < 256; ++i)
  79. if (isIdentifierCharSlow (i))
  80. n[i >> 5] |= (1 << (i & 31));
  81. String s;
  82. for (int i = 0; i < 8; ++i)
  83. s << "0x" << String::toHexString ((int) n[i]) << ", ";
  84. DBG (s);
  85. }*/
  86. static String::CharPointerType findEndOfToken (String::CharPointerType p)
  87. {
  88. while (isIdentifierChar (*p))
  89. ++p;
  90. return p;
  91. }
  92. }
  93. XmlElement* XmlDocument::getDocumentElement (const bool onlyReadOuterDocumentElement)
  94. {
  95. if (originalText.isEmpty() && inputSource != nullptr)
  96. {
  97. ScopedPointer<InputStream> in (inputSource->createInputStream());
  98. if (in != nullptr)
  99. {
  100. MemoryOutputStream data;
  101. data.writeFromInputStream (*in, onlyReadOuterDocumentElement ? 8192 : -1);
  102. if (data.getDataSize() > 2)
  103. {
  104. data.writeByte (0);
  105. const char* text = static_cast<const char*> (data.getData());
  106. if (CharPointer_UTF8::isByteOrderMark (text))
  107. text += 3;
  108. // parse the input buffer directly to avoid copying it all to a string..
  109. return parseDocumentElement (String::CharPointerType (text), onlyReadOuterDocumentElement);
  110. }
  111. }
  112. }
  113. return parseDocumentElement (originalText.getCharPointer(), onlyReadOuterDocumentElement);
  114. }
  115. const String& XmlDocument::getLastParseError() const noexcept
  116. {
  117. return lastError;
  118. }
  119. void XmlDocument::setLastError (const String& desc, const bool carryOn)
  120. {
  121. lastError = desc;
  122. errorOccurred = ! carryOn;
  123. }
  124. String XmlDocument::getFileContents (const String& filename) const
  125. {
  126. if (inputSource != nullptr)
  127. {
  128. const ScopedPointer<InputStream> in (inputSource->createInputStreamFor (filename.trim().unquoted()));
  129. if (in != nullptr)
  130. return in->readEntireStreamAsString();
  131. }
  132. return String();
  133. }
  134. juce_wchar XmlDocument::readNextChar() noexcept
  135. {
  136. const juce_wchar c = input.getAndAdvance();
  137. if (c == 0)
  138. {
  139. outOfData = true;
  140. --input;
  141. }
  142. return c;
  143. }
  144. XmlElement* XmlDocument::parseDocumentElement (String::CharPointerType textToParse,
  145. const bool onlyReadOuterDocumentElement)
  146. {
  147. input = textToParse;
  148. errorOccurred = false;
  149. outOfData = false;
  150. needToLoadDTD = true;
  151. if (textToParse.isEmpty())
  152. {
  153. lastError = "not enough input";
  154. }
  155. else if (! parseHeader())
  156. {
  157. lastError = "malformed header";
  158. }
  159. else if (! parseDTD())
  160. {
  161. lastError = "malformed DTD";
  162. }
  163. else
  164. {
  165. lastError.clear();
  166. ScopedPointer<XmlElement> result (readNextElement (! onlyReadOuterDocumentElement));
  167. if (! errorOccurred)
  168. return result.release();
  169. }
  170. return nullptr;
  171. }
  172. bool XmlDocument::parseHeader()
  173. {
  174. skipNextWhiteSpace();
  175. if (CharacterFunctions::compareUpTo (input, CharPointer_UTF8 ("<?xml"), 5) == 0)
  176. {
  177. const String::CharPointerType headerEnd (CharacterFunctions::find (input, CharPointer_UTF8 ("?>")));
  178. if (headerEnd.isEmpty())
  179. return false;
  180. #if JUCE_DEBUG
  181. const String encoding (String (input, headerEnd)
  182. .fromFirstOccurrenceOf ("encoding", false, true)
  183. .fromFirstOccurrenceOf ("=", false, false)
  184. .fromFirstOccurrenceOf ("\"", false, false)
  185. .upToFirstOccurrenceOf ("\"", false, false).trim());
  186. /* If you load an XML document with a non-UTF encoding type, it may have been
  187. loaded wrongly.. Since all the files are read via the normal juce file streams,
  188. they're treated as UTF-8, so by the time it gets to the parser, the encoding will
  189. have been lost. Best plan is to stick to utf-8 or if you have specific files to
  190. read, use your own code to convert them to a unicode String, and pass that to the
  191. XML parser.
  192. */
  193. jassert (encoding.isEmpty() || encoding.startsWithIgnoreCase ("utf-"));
  194. #endif
  195. input = headerEnd + 2;
  196. skipNextWhiteSpace();
  197. }
  198. return true;
  199. }
  200. bool XmlDocument::parseDTD()
  201. {
  202. if (CharacterFunctions::compareUpTo (input, CharPointer_UTF8 ("<!DOCTYPE"), 9) == 0)
  203. {
  204. input += 9;
  205. const String::CharPointerType dtdStart (input);
  206. for (int n = 1; n > 0;)
  207. {
  208. const juce_wchar c = readNextChar();
  209. if (outOfData)
  210. return false;
  211. if (c == '<')
  212. ++n;
  213. else if (c == '>')
  214. --n;
  215. }
  216. dtdText = String (dtdStart, input - 1).trim();
  217. }
  218. return true;
  219. }
  220. void XmlDocument::skipNextWhiteSpace()
  221. {
  222. for (;;)
  223. {
  224. input = input.findEndOfWhitespace();
  225. if (input.isEmpty())
  226. {
  227. outOfData = true;
  228. break;
  229. }
  230. if (*input == '<')
  231. {
  232. if (input[1] == '!'
  233. && input[2] == '-'
  234. && input[3] == '-')
  235. {
  236. input += 4;
  237. const int closeComment = input.indexOf (CharPointer_UTF8 ("-->"));
  238. if (closeComment < 0)
  239. {
  240. outOfData = true;
  241. break;
  242. }
  243. input += closeComment + 3;
  244. continue;
  245. }
  246. if (input[1] == '?')
  247. {
  248. input += 2;
  249. const int closeBracket = input.indexOf (CharPointer_UTF8 ("?>"));
  250. if (closeBracket < 0)
  251. {
  252. outOfData = true;
  253. break;
  254. }
  255. input += closeBracket + 2;
  256. continue;
  257. }
  258. }
  259. break;
  260. }
  261. }
  262. void XmlDocument::readQuotedString (String& result)
  263. {
  264. const juce_wchar quote = readNextChar();
  265. while (! outOfData)
  266. {
  267. const juce_wchar c = readNextChar();
  268. if (c == quote)
  269. break;
  270. --input;
  271. if (c == '&')
  272. {
  273. readEntity (result);
  274. }
  275. else
  276. {
  277. const String::CharPointerType start (input);
  278. for (;;)
  279. {
  280. const juce_wchar character = *input;
  281. if (character == quote)
  282. {
  283. result.appendCharPointer (start, input);
  284. ++input;
  285. return;
  286. }
  287. else if (character == '&')
  288. {
  289. result.appendCharPointer (start, input);
  290. break;
  291. }
  292. else if (character == 0)
  293. {
  294. setLastError ("unmatched quotes", false);
  295. outOfData = true;
  296. break;
  297. }
  298. ++input;
  299. }
  300. }
  301. }
  302. }
  303. XmlElement* XmlDocument::readNextElement (const bool alsoParseSubElements)
  304. {
  305. XmlElement* node = nullptr;
  306. skipNextWhiteSpace();
  307. if (outOfData)
  308. return nullptr;
  309. if (*input == '<')
  310. {
  311. ++input;
  312. String::CharPointerType endOfToken (XmlIdentifierChars::findEndOfToken (input));
  313. if (endOfToken == input)
  314. {
  315. // no tag name - but allow for a gap after the '<' before giving an error
  316. skipNextWhiteSpace();
  317. endOfToken = XmlIdentifierChars::findEndOfToken (input);
  318. if (endOfToken == input)
  319. {
  320. setLastError ("tag name missing", false);
  321. return node;
  322. }
  323. }
  324. node = new XmlElement (input, endOfToken);
  325. input = endOfToken;
  326. LinkedListPointer<XmlElement::XmlAttributeNode>::Appender attributeAppender (node->attributes);
  327. // look for attributes
  328. for (;;)
  329. {
  330. skipNextWhiteSpace();
  331. const juce_wchar c = *input;
  332. // empty tag..
  333. if (c == '/' && input[1] == '>')
  334. {
  335. input += 2;
  336. break;
  337. }
  338. // parse the guts of the element..
  339. if (c == '>')
  340. {
  341. ++input;
  342. if (alsoParseSubElements)
  343. readChildElements (*node);
  344. break;
  345. }
  346. // get an attribute..
  347. if (XmlIdentifierChars::isIdentifierChar (c))
  348. {
  349. String::CharPointerType attNameEnd (XmlIdentifierChars::findEndOfToken (input));
  350. if (attNameEnd != input)
  351. {
  352. const String::CharPointerType attNameStart (input);
  353. input = attNameEnd;
  354. skipNextWhiteSpace();
  355. if (readNextChar() == '=')
  356. {
  357. skipNextWhiteSpace();
  358. const juce_wchar nextChar = *input;
  359. if (nextChar == '"' || nextChar == '\'')
  360. {
  361. XmlElement::XmlAttributeNode* const newAtt
  362. = new XmlElement::XmlAttributeNode (attNameStart, attNameEnd);
  363. readQuotedString (newAtt->value);
  364. attributeAppender.append (newAtt);
  365. continue;
  366. }
  367. }
  368. else
  369. {
  370. setLastError ("expected '=' after attribute '"
  371. + String (attNameStart, attNameEnd) + "'", false);
  372. return node;
  373. }
  374. }
  375. }
  376. else
  377. {
  378. if (! outOfData)
  379. setLastError ("illegal character found in " + node->getTagName() + ": '" + c + "'", false);
  380. }
  381. break;
  382. }
  383. }
  384. return node;
  385. }
  386. void XmlDocument::readChildElements (XmlElement& parent)
  387. {
  388. LinkedListPointer<XmlElement>::Appender childAppender (parent.firstChildElement);
  389. for (;;)
  390. {
  391. const String::CharPointerType preWhitespaceInput (input);
  392. skipNextWhiteSpace();
  393. if (outOfData)
  394. {
  395. setLastError ("unmatched tags", false);
  396. break;
  397. }
  398. if (*input == '<')
  399. {
  400. const juce_wchar c1 = input[1];
  401. if (c1 == '/')
  402. {
  403. // our close tag..
  404. const int closeTag = input.indexOf ((juce_wchar) '>');
  405. if (closeTag >= 0)
  406. input += closeTag + 1;
  407. break;
  408. }
  409. if (c1 == '!' && CharacterFunctions::compareUpTo (input + 2, CharPointer_UTF8 ("[CDATA["), 7) == 0)
  410. {
  411. input += 9;
  412. const String::CharPointerType inputStart (input);
  413. for (;;)
  414. {
  415. const juce_wchar c0 = *input;
  416. if (c0 == 0)
  417. {
  418. setLastError ("unterminated CDATA section", false);
  419. outOfData = true;
  420. break;
  421. }
  422. else if (c0 == ']'
  423. && input[1] == ']'
  424. && input[2] == '>')
  425. {
  426. childAppender.append (XmlElement::createTextElement (String (inputStart, input)));
  427. input += 3;
  428. break;
  429. }
  430. ++input;
  431. }
  432. }
  433. else
  434. {
  435. // this is some other element, so parse and add it..
  436. if (XmlElement* const n = readNextElement (true))
  437. childAppender.append (n);
  438. else
  439. break;
  440. }
  441. }
  442. else // must be a character block
  443. {
  444. input = preWhitespaceInput; // roll back to include the leading whitespace
  445. MemoryOutputStream textElementContent;
  446. bool contentShouldBeUsed = ! ignoreEmptyTextElements;
  447. for (;;)
  448. {
  449. const juce_wchar c = *input;
  450. if (c == '<')
  451. {
  452. if (input[1] == '!' && input[2] == '-' && input[3] == '-')
  453. {
  454. input += 4;
  455. const int closeComment = input.indexOf (CharPointer_UTF8 ("-->"));
  456. if (closeComment < 0)
  457. {
  458. setLastError ("unterminated comment", false);
  459. outOfData = true;
  460. return;
  461. }
  462. input += closeComment + 3;
  463. continue;
  464. }
  465. break;
  466. }
  467. if (c == 0)
  468. {
  469. setLastError ("unmatched tags", false);
  470. outOfData = true;
  471. return;
  472. }
  473. if (c == '&')
  474. {
  475. String entity;
  476. readEntity (entity);
  477. if (entity.startsWithChar ('<') && entity [1] != 0)
  478. {
  479. const String::CharPointerType oldInput (input);
  480. const bool oldOutOfData = outOfData;
  481. input = entity.getCharPointer();
  482. outOfData = false;
  483. while (XmlElement* n = readNextElement (true))
  484. childAppender.append (n);
  485. input = oldInput;
  486. outOfData = oldOutOfData;
  487. }
  488. else
  489. {
  490. textElementContent << entity;
  491. contentShouldBeUsed = contentShouldBeUsed || entity.containsNonWhitespaceChars();
  492. }
  493. }
  494. else
  495. {
  496. for (;; ++input)
  497. {
  498. juce_wchar nextChar = *input;
  499. if (nextChar == '\r')
  500. {
  501. nextChar = '\n';
  502. if (input[1] == '\n')
  503. continue;
  504. }
  505. if (nextChar == '<' || nextChar == '&')
  506. break;
  507. if (nextChar == 0)
  508. {
  509. setLastError ("unmatched tags", false);
  510. outOfData = true;
  511. return;
  512. }
  513. textElementContent.appendUTF8Char (nextChar);
  514. contentShouldBeUsed = contentShouldBeUsed || ! CharacterFunctions::isWhitespace (nextChar);
  515. }
  516. }
  517. }
  518. if (contentShouldBeUsed)
  519. childAppender.append (XmlElement::createTextElement (textElementContent.toUTF8()));
  520. }
  521. }
  522. }
  523. void XmlDocument::readEntity (String& result)
  524. {
  525. // skip over the ampersand
  526. ++input;
  527. if (input.compareIgnoreCaseUpTo (CharPointer_UTF8 ("amp;"), 4) == 0)
  528. {
  529. input += 4;
  530. result += '&';
  531. }
  532. else if (input.compareIgnoreCaseUpTo (CharPointer_UTF8 ("quot;"), 5) == 0)
  533. {
  534. input += 5;
  535. result += '"';
  536. }
  537. else if (input.compareIgnoreCaseUpTo (CharPointer_UTF8 ("apos;"), 5) == 0)
  538. {
  539. input += 5;
  540. result += '\'';
  541. }
  542. else if (input.compareIgnoreCaseUpTo (CharPointer_UTF8 ("lt;"), 3) == 0)
  543. {
  544. input += 3;
  545. result += '<';
  546. }
  547. else if (input.compareIgnoreCaseUpTo (CharPointer_UTF8 ("gt;"), 3) == 0)
  548. {
  549. input += 3;
  550. result += '>';
  551. }
  552. else if (*input == '#')
  553. {
  554. int charCode = 0;
  555. ++input;
  556. if (*input == 'x' || *input == 'X')
  557. {
  558. ++input;
  559. int numChars = 0;
  560. while (input[0] != ';')
  561. {
  562. const int hexValue = CharacterFunctions::getHexDigitValue (input[0]);
  563. if (hexValue < 0 || ++numChars > 8)
  564. {
  565. setLastError ("illegal escape sequence", true);
  566. break;
  567. }
  568. charCode = (charCode << 4) | hexValue;
  569. ++input;
  570. }
  571. ++input;
  572. }
  573. else if (input[0] >= '0' && input[0] <= '9')
  574. {
  575. int numChars = 0;
  576. while (input[0] != ';')
  577. {
  578. if (++numChars > 12)
  579. {
  580. setLastError ("illegal escape sequence", true);
  581. break;
  582. }
  583. charCode = charCode * 10 + ((int) input[0] - '0');
  584. ++input;
  585. }
  586. ++input;
  587. }
  588. else
  589. {
  590. setLastError ("illegal escape sequence", true);
  591. result += '&';
  592. return;
  593. }
  594. result << (juce_wchar) charCode;
  595. }
  596. else
  597. {
  598. const String::CharPointerType entityNameStart (input);
  599. const int closingSemiColon = input.indexOf ((juce_wchar) ';');
  600. if (closingSemiColon < 0)
  601. {
  602. outOfData = true;
  603. result += '&';
  604. }
  605. else
  606. {
  607. input += closingSemiColon + 1;
  608. result += expandExternalEntity (String (entityNameStart, (size_t) closingSemiColon));
  609. }
  610. }
  611. }
  612. String XmlDocument::expandEntity (const String& ent)
  613. {
  614. if (ent.equalsIgnoreCase ("amp")) return String::charToString ('&');
  615. if (ent.equalsIgnoreCase ("quot")) return String::charToString ('"');
  616. if (ent.equalsIgnoreCase ("apos")) return String::charToString ('\'');
  617. if (ent.equalsIgnoreCase ("lt")) return String::charToString ('<');
  618. if (ent.equalsIgnoreCase ("gt")) return String::charToString ('>');
  619. if (ent[0] == '#')
  620. {
  621. const juce_wchar char1 = ent[1];
  622. if (char1 == 'x' || char1 == 'X')
  623. return String::charToString (static_cast<juce_wchar> (ent.substring (2).getHexValue32()));
  624. if (char1 >= '0' && char1 <= '9')
  625. return String::charToString (static_cast<juce_wchar> (ent.substring (1).getIntValue()));
  626. setLastError ("illegal escape sequence", false);
  627. return String::charToString ('&');
  628. }
  629. return expandExternalEntity (ent);
  630. }
  631. String XmlDocument::expandExternalEntity (const String& entity)
  632. {
  633. if (needToLoadDTD)
  634. {
  635. if (dtdText.isNotEmpty())
  636. {
  637. dtdText = dtdText.trimCharactersAtEnd (">");
  638. tokenisedDTD.addTokens (dtdText, true);
  639. if (tokenisedDTD [tokenisedDTD.size() - 2].equalsIgnoreCase ("system")
  640. && tokenisedDTD [tokenisedDTD.size() - 1].isQuotedString())
  641. {
  642. const String fn (tokenisedDTD [tokenisedDTD.size() - 1]);
  643. tokenisedDTD.clear();
  644. tokenisedDTD.addTokens (getFileContents (fn), true);
  645. }
  646. else
  647. {
  648. tokenisedDTD.clear();
  649. const int openBracket = dtdText.indexOfChar ('[');
  650. if (openBracket > 0)
  651. {
  652. const int closeBracket = dtdText.lastIndexOfChar (']');
  653. if (closeBracket > openBracket)
  654. tokenisedDTD.addTokens (dtdText.substring (openBracket + 1,
  655. closeBracket), true);
  656. }
  657. }
  658. for (int i = tokenisedDTD.size(); --i >= 0;)
  659. {
  660. if (tokenisedDTD[i].startsWithChar ('%')
  661. && tokenisedDTD[i].endsWithChar (';'))
  662. {
  663. const String parsed (getParameterEntity (tokenisedDTD[i].substring (1, tokenisedDTD[i].length() - 1)));
  664. StringArray newToks;
  665. newToks.addTokens (parsed, true);
  666. tokenisedDTD.remove (i);
  667. for (int j = newToks.size(); --j >= 0;)
  668. tokenisedDTD.insert (i, newToks[j]);
  669. }
  670. }
  671. }
  672. needToLoadDTD = false;
  673. }
  674. for (int i = 0; i < tokenisedDTD.size(); ++i)
  675. {
  676. if (tokenisedDTD[i] == entity)
  677. {
  678. if (tokenisedDTD[i - 1].equalsIgnoreCase ("<!entity"))
  679. {
  680. String ent (tokenisedDTD [i + 1].trimCharactersAtEnd (">").trim().unquoted());
  681. // check for sub-entities..
  682. int ampersand = ent.indexOfChar ('&');
  683. while (ampersand >= 0)
  684. {
  685. const int semiColon = ent.indexOf (i + 1, ";");
  686. if (semiColon < 0)
  687. {
  688. setLastError ("entity without terminating semi-colon", false);
  689. break;
  690. }
  691. const String resolved (expandEntity (ent.substring (i + 1, semiColon)));
  692. ent = ent.substring (0, ampersand)
  693. + resolved
  694. + ent.substring (semiColon + 1);
  695. ampersand = ent.indexOfChar (semiColon + 1, '&');
  696. }
  697. return ent;
  698. }
  699. }
  700. }
  701. setLastError ("unknown entity", true);
  702. return entity;
  703. }
  704. String XmlDocument::getParameterEntity (const String& entity)
  705. {
  706. for (int i = 0; i < tokenisedDTD.size(); ++i)
  707. {
  708. if (tokenisedDTD[i] == entity
  709. && tokenisedDTD [i - 1] == "%"
  710. && tokenisedDTD [i - 2].equalsIgnoreCase ("<!entity"))
  711. {
  712. const String ent (tokenisedDTD [i + 1].trimCharactersAtEnd (">"));
  713. if (ent.equalsIgnoreCase ("system"))
  714. return getFileContents (tokenisedDTD [i + 2].trimCharactersAtEnd (">"));
  715. return ent.trim().unquoted();
  716. }
  717. }
  718. return entity;
  719. }