juce_XmlDocument.cpp 26 KB

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