BaseXMLParser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/BaseXMLParser.h"
  29. #include "../../Include/RmlUi/Core/Profiling.h"
  30. #include "../../Include/RmlUi/Core/Stream.h"
  31. #include "XMLParseTools.h"
  32. #include <string.h>
  33. namespace Rml {
  34. BaseXMLParser::BaseXMLParser()
  35. {}
  36. BaseXMLParser::~BaseXMLParser()
  37. {}
  38. // Registers a tag as containing general character data.
  39. void BaseXMLParser::RegisterCDATATag(const String& tag)
  40. {
  41. if (!tag.empty())
  42. cdata_tags.insert(StringUtilities::ToLower(tag));
  43. }
  44. void BaseXMLParser::RegisterInnerXMLAttribute(const String& attribute_name)
  45. {
  46. attributes_for_inner_xml_data.insert(attribute_name);
  47. }
  48. // Parses the given stream as an XML file, and calls the handlers when
  49. // interesting phenomenon are encountered.
  50. void BaseXMLParser::Parse(Stream* stream)
  51. {
  52. source_url = &stream->GetSourceURL();
  53. xml_source.clear();
  54. // We read in the whole XML file here.
  55. // TODO: It doesn't look like the Stream interface is used for anything useful. We
  56. // might as well just use a span or StringView, and get completely rid of it.
  57. // @performance Otherwise, use the temporary allocator.
  58. const size_t source_size = stream->Length();
  59. stream->Read(xml_source, source_size);
  60. xml_index = 0;
  61. line_number = 1;
  62. line_number_open_tag = 1;
  63. inner_xml_data = false;
  64. inner_xml_data_terminate_depth = 0;
  65. inner_xml_data_index_begin = 0;
  66. // Read (er ... skip) the header, if one exists.
  67. ReadHeader();
  68. // Read the XML body.
  69. ReadBody();
  70. xml_source.clear();
  71. source_url = nullptr;
  72. }
  73. // Get the current file line number
  74. int BaseXMLParser::GetLineNumber() const
  75. {
  76. return line_number;
  77. }
  78. int BaseXMLParser::GetLineNumberOpenTag() const
  79. {
  80. return line_number_open_tag;
  81. }
  82. // Called when the parser finds the beginning of an element tag.
  83. void BaseXMLParser::HandleElementStart(const String& RMLUI_UNUSED_PARAMETER(name), const XMLAttributes& RMLUI_UNUSED_PARAMETER(attributes))
  84. {
  85. RMLUI_UNUSED(name);
  86. RMLUI_UNUSED(attributes);
  87. }
  88. // Called when the parser finds the end of an element tag.
  89. void BaseXMLParser::HandleElementEnd(const String& RMLUI_UNUSED_PARAMETER(name))
  90. {
  91. RMLUI_UNUSED(name);
  92. }
  93. // Called when the parser encounters data.
  94. void BaseXMLParser::HandleData(const String& RMLUI_UNUSED_PARAMETER(data), XMLDataType RMLUI_UNUSED_PARAMETER(type))
  95. {
  96. RMLUI_UNUSED(data);
  97. RMLUI_UNUSED(type);
  98. }
  99. /// Returns the source URL of this parse. Only valid during parsing.
  100. const URL* BaseXMLParser::GetSourceURLPtr() const
  101. {
  102. return source_url;
  103. }
  104. void BaseXMLParser::Next() {
  105. xml_index += 1;
  106. }
  107. bool BaseXMLParser::AtEnd() const {
  108. return xml_index >= xml_source.size();
  109. }
  110. char BaseXMLParser::Look() const {
  111. RMLUI_ASSERT(!AtEnd());
  112. return xml_source[xml_index];
  113. }
  114. void BaseXMLParser::HandleElementStartInternal(const String& name, const XMLAttributes& attributes)
  115. {
  116. line_number_open_tag = line_number;
  117. if (!inner_xml_data)
  118. HandleElementStart(name, attributes);
  119. }
  120. void BaseXMLParser::HandleElementEndInternal(const String& name)
  121. {
  122. if (!inner_xml_data)
  123. HandleElementEnd(name);
  124. }
  125. void BaseXMLParser::HandleDataInternal(const String& data, XMLDataType type)
  126. {
  127. if (!inner_xml_data)
  128. HandleData(data, type);
  129. }
  130. void BaseXMLParser::ReadHeader()
  131. {
  132. if (PeekString("<?"))
  133. {
  134. String temp;
  135. FindString(">", temp);
  136. }
  137. }
  138. void BaseXMLParser::ReadBody()
  139. {
  140. RMLUI_ZoneScoped;
  141. open_tag_depth = 0;
  142. line_number_open_tag = 0;
  143. for(;;)
  144. {
  145. // Find the next open tag.
  146. if (!FindString("<", data, true))
  147. break;
  148. const size_t xml_index_tag = xml_index - 1;
  149. // Check what kind of tag this is.
  150. if (PeekString("!--"))
  151. {
  152. // Comment.
  153. String temp;
  154. if (!FindString("-->", temp))
  155. break;
  156. }
  157. else if (PeekString("![CDATA["))
  158. {
  159. // CDATA tag; read everything (including markup) until the ending
  160. // CDATA tag.
  161. if (!ReadCDATA())
  162. break;
  163. }
  164. else if (PeekString("/"))
  165. {
  166. if (!ReadCloseTag(xml_index_tag))
  167. break;
  168. // Bail if we've hit the end of the XML data.
  169. if (open_tag_depth == 0)
  170. break;
  171. }
  172. else
  173. {
  174. if (!ReadOpenTag())
  175. break;
  176. }
  177. }
  178. // Check for error conditions
  179. if (open_tag_depth > 0)
  180. {
  181. Log::Message(Log::LT_WARNING, "XML parse error on line %d of %s.", GetLineNumber(), source_url->GetURL().c_str());
  182. }
  183. }
  184. bool BaseXMLParser::ReadOpenTag()
  185. {
  186. // Increase the open depth
  187. open_tag_depth++;
  188. // Opening tag; send data immediately and open the tag.
  189. if (!data.empty())
  190. {
  191. HandleDataInternal(data, XMLDataType::Text);
  192. data.clear();
  193. }
  194. String tag_name;
  195. if (!FindWord(tag_name, "/>"))
  196. return false;
  197. bool section_opened = false;
  198. if (PeekString(">"))
  199. {
  200. // Simple open tag.
  201. HandleElementStartInternal(tag_name, XMLAttributes());
  202. section_opened = true;
  203. }
  204. else if (PeekString("/") &&
  205. PeekString(">"))
  206. {
  207. // Empty open tag.
  208. HandleElementStartInternal(tag_name, XMLAttributes());
  209. HandleElementEndInternal(tag_name);
  210. // Tag immediately closed, reduce count
  211. open_tag_depth--;
  212. }
  213. else
  214. {
  215. // It appears we have some attributes. Let's parse them.
  216. bool parse_inner_xml_as_data = false;
  217. XMLAttributes attributes;
  218. if (!ReadAttributes(attributes, parse_inner_xml_as_data))
  219. return false;
  220. if (PeekString(">"))
  221. {
  222. HandleElementStartInternal(tag_name, attributes);
  223. section_opened = true;
  224. }
  225. else if (PeekString("/") &&
  226. PeekString(">"))
  227. {
  228. HandleElementStartInternal(tag_name, attributes);
  229. HandleElementEndInternal(tag_name);
  230. // Tag immediately closed, reduce count
  231. open_tag_depth--;
  232. }
  233. else
  234. {
  235. return false;
  236. }
  237. if (section_opened && parse_inner_xml_as_data && !inner_xml_data)
  238. {
  239. inner_xml_data = true;
  240. inner_xml_data_terminate_depth = open_tag_depth;
  241. inner_xml_data_index_begin = xml_index;
  242. }
  243. }
  244. // Check if this tag needs to be processed as CDATA.
  245. if (section_opened)
  246. {
  247. const String lcase_tag_name = StringUtilities::ToLower(tag_name);
  248. bool is_cdata_tag = (cdata_tags.find(lcase_tag_name) != cdata_tags.end());
  249. if (is_cdata_tag)
  250. {
  251. if (ReadCDATA(lcase_tag_name.c_str()))
  252. {
  253. open_tag_depth--;
  254. if (!data.empty())
  255. {
  256. HandleDataInternal(data, XMLDataType::CData);
  257. data.clear();
  258. }
  259. HandleElementEndInternal(tag_name);
  260. return true;
  261. }
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. bool BaseXMLParser::ReadCloseTag(const size_t xml_index_tag)
  268. {
  269. if (inner_xml_data && open_tag_depth == inner_xml_data_terminate_depth)
  270. {
  271. // Closing the tag that initiated the inner xml data parsing. Set all its contents as Data to be
  272. // submitted next, and disable the mode to resume normal parsing behavior.
  273. RMLUI_ASSERT(inner_xml_data_index_begin <= xml_index_tag);
  274. inner_xml_data = false;
  275. data = xml_source.substr(inner_xml_data_index_begin, xml_index_tag - inner_xml_data_index_begin);
  276. HandleDataInternal(data, XMLDataType::InnerXML);
  277. data.clear();
  278. }
  279. // Closing tag; send data immediately and close the tag.
  280. if (!data.empty())
  281. {
  282. HandleDataInternal(data, XMLDataType::Text);
  283. data.clear();
  284. }
  285. String tag_name;
  286. if (!FindString(">", tag_name))
  287. return false;
  288. HandleElementEndInternal(StringUtilities::StripWhitespace(tag_name));
  289. // Tag closed, reduce count
  290. open_tag_depth--;
  291. return true;
  292. }
  293. bool BaseXMLParser::ReadAttributes(XMLAttributes& attributes, bool& parse_raw_xml_content)
  294. {
  295. for (;;)
  296. {
  297. String attribute;
  298. String value;
  299. // Get the attribute name
  300. if (!FindWord(attribute, "=/>"))
  301. {
  302. return false;
  303. }
  304. // Check if theres an assigned value
  305. if (PeekString("="))
  306. {
  307. if (PeekString("\""))
  308. {
  309. if (!FindString("\"", value))
  310. return false;
  311. }
  312. else if (PeekString("'"))
  313. {
  314. if (!FindString("'", value))
  315. return false;
  316. }
  317. else if (!FindWord(value, "/>"))
  318. {
  319. return false;
  320. }
  321. }
  322. if (attributes_for_inner_xml_data.count(attribute) == 1)
  323. parse_raw_xml_content = true;
  324. attributes[attribute] = StringUtilities::DecodeRml(value);
  325. // Check for the end of the tag.
  326. if (PeekString("/", false) || PeekString(">", false))
  327. return true;
  328. }
  329. }
  330. bool BaseXMLParser::ReadCDATA(const char* tag_terminator)
  331. {
  332. String cdata;
  333. if (tag_terminator == nullptr)
  334. {
  335. FindString("]]>", cdata);
  336. data += cdata;
  337. return true;
  338. }
  339. else
  340. {
  341. for (;;)
  342. {
  343. // Search for the next tag opening.
  344. if (!FindString("<", cdata))
  345. return false;
  346. if (PeekString("/", false))
  347. {
  348. String tag;
  349. if (FindString(">", tag))
  350. {
  351. size_t slash_pos = tag.find('/');
  352. String tag_name = StringUtilities::StripWhitespace(slash_pos == String::npos ? tag : tag.substr(slash_pos + 1));
  353. if (StringUtilities::ToLower(tag_name) == tag_terminator)
  354. {
  355. data += cdata;
  356. return true;
  357. }
  358. else
  359. {
  360. cdata += '<' + tag + '>';
  361. }
  362. }
  363. else
  364. cdata += "<";
  365. }
  366. else
  367. cdata += "<";
  368. }
  369. }
  370. }
  371. // Reads from the stream until a complete word is found.
  372. bool BaseXMLParser::FindWord(String& word, const char* terminators)
  373. {
  374. while (!AtEnd())
  375. {
  376. char c = Look();
  377. // Count line numbers
  378. if (c == '\n')
  379. {
  380. line_number++;
  381. }
  382. // Ignore white space
  383. if (StringUtilities::IsWhitespace(c))
  384. {
  385. if (word.empty())
  386. {
  387. Next();
  388. continue;
  389. }
  390. else
  391. return true;
  392. }
  393. // Check for termination condition
  394. if (terminators && strchr(terminators, c))
  395. {
  396. return !word.empty();
  397. }
  398. word += c;
  399. Next();
  400. }
  401. return false;
  402. }
  403. // Reads from the stream until the given character set is found.
  404. bool BaseXMLParser::FindString(const char* string, String& data, bool escape_brackets)
  405. {
  406. int index = 0;
  407. bool in_brackets = false;
  408. bool in_string = false;
  409. char previous = 0;
  410. while (string[index])
  411. {
  412. if (AtEnd())
  413. return false;
  414. const char c = Look();
  415. // Count line numbers
  416. if (c == '\n')
  417. {
  418. line_number++;
  419. }
  420. if(escape_brackets)
  421. {
  422. const char* error_str = XMLParseTools::ParseDataBrackets(in_brackets, in_string, c, previous);
  423. if (error_str)
  424. {
  425. Log::Message(Log::LT_WARNING, "XML parse error. %s", error_str);
  426. return false;
  427. }
  428. }
  429. if (c == string[index] && !in_brackets)
  430. {
  431. index += 1;
  432. }
  433. else
  434. {
  435. if (index > 0)
  436. {
  437. data += String(string, index);
  438. index = 0;
  439. }
  440. data += c;
  441. }
  442. previous = c;
  443. Next();
  444. }
  445. return true;
  446. }
  447. // Returns true if the next sequence of characters in the stream matches the
  448. // given string.
  449. bool BaseXMLParser::PeekString(const char* string, bool consume)
  450. {
  451. const size_t start_index = xml_index;
  452. const int start_line = line_number;
  453. bool success = true;
  454. int i = 0;
  455. while (string[i])
  456. {
  457. if (AtEnd())
  458. {
  459. success = false;
  460. break;
  461. }
  462. const char c = Look();
  463. // Count line numbers
  464. if (c == '\n')
  465. {
  466. line_number++;
  467. }
  468. // Seek past all the whitespace if we haven't hit the initial character yet.
  469. if (i == 0 && StringUtilities::IsWhitespace(c))
  470. {
  471. Next();
  472. }
  473. else
  474. {
  475. if (c != string[i])
  476. {
  477. success = false;
  478. break;
  479. }
  480. i++;
  481. Next();
  482. }
  483. }
  484. // Set the index to the start index unless we are consuming.
  485. if (!consume || !success)
  486. {
  487. xml_index = start_index;
  488. line_number = start_line;
  489. }
  490. return success;
  491. }
  492. } // namespace Rml