BaseXMLParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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-2023 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. BaseXMLParser::~BaseXMLParser() {}
  36. void BaseXMLParser::RegisterCDATATag(const String& tag)
  37. {
  38. if (!tag.empty())
  39. cdata_tags.insert(StringUtilities::ToLower(tag));
  40. }
  41. void BaseXMLParser::RegisterInnerXMLAttribute(const String& attribute_name)
  42. {
  43. attributes_for_inner_xml_data.insert(attribute_name);
  44. }
  45. void BaseXMLParser::Parse(Stream* stream)
  46. {
  47. source_url = &stream->GetSourceURL();
  48. xml_source.clear();
  49. // We read in the whole XML file here.
  50. // TODO: It doesn't look like the Stream interface is used for anything useful. We
  51. // might as well just use a span or StringView, and get completely rid of it.
  52. // @performance Otherwise, use the temporary allocator.
  53. const size_t source_size = stream->Length();
  54. stream->Read(xml_source, source_size);
  55. xml_index = 0;
  56. line_number = 1;
  57. line_number_open_tag = 1;
  58. inner_xml_data = false;
  59. inner_xml_data_terminate_depth = 0;
  60. inner_xml_data_index_begin = 0;
  61. // Read (er ... skip) the header, if one exists.
  62. ReadHeader();
  63. // Read the XML body.
  64. ReadBody();
  65. xml_source.clear();
  66. source_url = nullptr;
  67. }
  68. int BaseXMLParser::GetLineNumber() const
  69. {
  70. return line_number;
  71. }
  72. int BaseXMLParser::GetLineNumberOpenTag() const
  73. {
  74. return line_number_open_tag;
  75. }
  76. void BaseXMLParser::HandleElementStart(const String& /*name*/, const XMLAttributes& /*attributes*/) {}
  77. void BaseXMLParser::HandleElementEnd(const String& /*name*/) {}
  78. void BaseXMLParser::HandleData(const String& /*data*/, XMLDataType /*type*/) {}
  79. const URL* BaseXMLParser::GetSourceURLPtr() const
  80. {
  81. return source_url;
  82. }
  83. void BaseXMLParser::Next()
  84. {
  85. xml_index += 1;
  86. }
  87. bool BaseXMLParser::AtEnd() const
  88. {
  89. return xml_index >= xml_source.size();
  90. }
  91. char BaseXMLParser::Look() const
  92. {
  93. RMLUI_ASSERT(!AtEnd());
  94. return xml_source[xml_index];
  95. }
  96. void BaseXMLParser::HandleElementStartInternal(const String& name, const XMLAttributes& attributes)
  97. {
  98. line_number_open_tag = line_number;
  99. if (!inner_xml_data)
  100. HandleElementStart(name, attributes);
  101. }
  102. void BaseXMLParser::HandleElementEndInternal(const String& name)
  103. {
  104. if (!inner_xml_data)
  105. HandleElementEnd(name);
  106. }
  107. void BaseXMLParser::HandleDataInternal(const String& data, XMLDataType type)
  108. {
  109. if (!inner_xml_data)
  110. HandleData(data, type);
  111. }
  112. void BaseXMLParser::ReadHeader()
  113. {
  114. if (PeekString("<?"))
  115. {
  116. String temp;
  117. FindString(">", temp);
  118. }
  119. }
  120. void BaseXMLParser::ReadBody()
  121. {
  122. RMLUI_ZoneScoped;
  123. open_tag_depth = 0;
  124. line_number_open_tag = 0;
  125. for (;;)
  126. {
  127. // Find the next open tag.
  128. if (!FindString("<", data, true))
  129. break;
  130. const size_t xml_index_tag = xml_index - 1;
  131. // Check what kind of tag this is.
  132. if (PeekString("!--"))
  133. {
  134. // Comment.
  135. String temp;
  136. if (!FindString("-->", temp))
  137. break;
  138. }
  139. else if (PeekString("![CDATA["))
  140. {
  141. // CDATA tag; read everything (including markup) until the ending
  142. // CDATA tag.
  143. if (!ReadCDATA())
  144. break;
  145. }
  146. else if (PeekString("/"))
  147. {
  148. if (!ReadCloseTag(xml_index_tag))
  149. break;
  150. // Bail if we've hit the end of the XML data.
  151. if (open_tag_depth == 0)
  152. break;
  153. }
  154. else
  155. {
  156. if (!ReadOpenTag())
  157. break;
  158. }
  159. }
  160. // Check for error conditions
  161. if (open_tag_depth > 0)
  162. {
  163. Log::Message(Log::LT_WARNING, "XML parse error on line %d of %s.", GetLineNumber(), source_url->GetURL().c_str());
  164. }
  165. }
  166. bool BaseXMLParser::ReadOpenTag()
  167. {
  168. // Increase the open depth
  169. open_tag_depth++;
  170. // Opening tag; send data immediately and open the tag.
  171. if (!data.empty())
  172. {
  173. HandleDataInternal(data, XMLDataType::Text);
  174. data.clear();
  175. }
  176. String tag_name;
  177. if (!FindWord(tag_name, "/>"))
  178. return false;
  179. bool section_opened = false;
  180. if (PeekString(">"))
  181. {
  182. // Simple open tag.
  183. HandleElementStartInternal(tag_name, XMLAttributes());
  184. section_opened = true;
  185. }
  186. else if (PeekString("/") && PeekString(">"))
  187. {
  188. // Empty open tag.
  189. HandleElementStartInternal(tag_name, XMLAttributes());
  190. HandleElementEndInternal(tag_name);
  191. // Tag immediately closed, reduce count
  192. open_tag_depth--;
  193. }
  194. else
  195. {
  196. // It appears we have some attributes. Let's parse them.
  197. bool parse_inner_xml_as_data = false;
  198. XMLAttributes attributes;
  199. if (!ReadAttributes(attributes, parse_inner_xml_as_data))
  200. return false;
  201. if (PeekString(">"))
  202. {
  203. HandleElementStartInternal(tag_name, attributes);
  204. section_opened = true;
  205. }
  206. else if (PeekString("/") && PeekString(">"))
  207. {
  208. HandleElementStartInternal(tag_name, attributes);
  209. HandleElementEndInternal(tag_name);
  210. // Tag immediately closed, reduce count
  211. open_tag_depth--;
  212. }
  213. else
  214. {
  215. return false;
  216. }
  217. if (section_opened && parse_inner_xml_as_data && !inner_xml_data)
  218. {
  219. inner_xml_data = true;
  220. inner_xml_data_terminate_depth = open_tag_depth;
  221. inner_xml_data_index_begin = xml_index;
  222. }
  223. }
  224. // Check if this tag needs to be processed as CDATA.
  225. if (section_opened)
  226. {
  227. const String lcase_tag_name = StringUtilities::ToLower(tag_name);
  228. bool is_cdata_tag = (cdata_tags.find(lcase_tag_name) != cdata_tags.end());
  229. if (is_cdata_tag)
  230. {
  231. if (ReadCDATA(lcase_tag_name.c_str()))
  232. {
  233. open_tag_depth--;
  234. if (!data.empty())
  235. {
  236. HandleDataInternal(data, XMLDataType::CData);
  237. data.clear();
  238. }
  239. HandleElementEndInternal(tag_name);
  240. return true;
  241. }
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. bool BaseXMLParser::ReadCloseTag(const size_t xml_index_tag)
  248. {
  249. if (inner_xml_data && open_tag_depth == inner_xml_data_terminate_depth)
  250. {
  251. // Closing the tag that initiated the inner xml data parsing. Set all its contents as Data to be
  252. // submitted next, and disable the mode to resume normal parsing behavior.
  253. RMLUI_ASSERT(inner_xml_data_index_begin <= xml_index_tag);
  254. inner_xml_data = false;
  255. data = xml_source.substr(inner_xml_data_index_begin, xml_index_tag - inner_xml_data_index_begin);
  256. HandleDataInternal(data, XMLDataType::InnerXML);
  257. data.clear();
  258. }
  259. // Closing tag; send data immediately and close the tag.
  260. if (!data.empty())
  261. {
  262. HandleDataInternal(data, XMLDataType::Text);
  263. data.clear();
  264. }
  265. String tag_name;
  266. if (!FindString(">", tag_name))
  267. return false;
  268. HandleElementEndInternal(StringUtilities::StripWhitespace(tag_name));
  269. // Tag closed, reduce count
  270. open_tag_depth--;
  271. return true;
  272. }
  273. bool BaseXMLParser::ReadAttributes(XMLAttributes& attributes, bool& parse_raw_xml_content)
  274. {
  275. for (;;)
  276. {
  277. String attribute;
  278. String value;
  279. // Get the attribute name
  280. if (!FindWord(attribute, "=/>"))
  281. {
  282. return false;
  283. }
  284. // Check if theres an assigned value
  285. if (PeekString("="))
  286. {
  287. if (PeekString("\""))
  288. {
  289. if (!FindString("\"", value))
  290. return false;
  291. }
  292. else if (PeekString("'"))
  293. {
  294. if (!FindString("'", value))
  295. return false;
  296. }
  297. else if (!FindWord(value, "/>"))
  298. {
  299. return false;
  300. }
  301. }
  302. if (attributes_for_inner_xml_data.count(attribute) == 1)
  303. parse_raw_xml_content = true;
  304. attributes[attribute] = StringUtilities::DecodeRml(value);
  305. // Check for the end of the tag.
  306. if (PeekString("/", false) || PeekString(">", false))
  307. return true;
  308. }
  309. }
  310. bool BaseXMLParser::ReadCDATA(const char* tag_terminator)
  311. {
  312. String cdata;
  313. if (tag_terminator == nullptr)
  314. {
  315. FindString("]]>", cdata);
  316. data += cdata;
  317. return true;
  318. }
  319. else
  320. {
  321. for (;;)
  322. {
  323. // Search for the next tag opening.
  324. if (!FindString("<", cdata))
  325. return false;
  326. if (PeekString("/", false))
  327. {
  328. String tag;
  329. if (FindString(">", tag))
  330. {
  331. size_t slash_pos = tag.find('/');
  332. String tag_name = StringUtilities::StripWhitespace(slash_pos == String::npos ? tag : tag.substr(slash_pos + 1));
  333. if (StringUtilities::ToLower(std::move(tag_name)) == tag_terminator)
  334. {
  335. data += cdata;
  336. return true;
  337. }
  338. else
  339. {
  340. cdata += '<' + tag + '>';
  341. }
  342. }
  343. else
  344. cdata += "<";
  345. }
  346. else
  347. cdata += "<";
  348. }
  349. }
  350. }
  351. bool BaseXMLParser::FindWord(String& word, const char* terminators)
  352. {
  353. while (!AtEnd())
  354. {
  355. char c = Look();
  356. // Count line numbers
  357. if (c == '\n')
  358. {
  359. line_number++;
  360. }
  361. // Ignore white space
  362. if (StringUtilities::IsWhitespace(c))
  363. {
  364. if (word.empty())
  365. {
  366. Next();
  367. continue;
  368. }
  369. else
  370. return true;
  371. }
  372. // Check for termination condition
  373. if (terminators && strchr(terminators, c))
  374. {
  375. return !word.empty();
  376. }
  377. word += c;
  378. Next();
  379. }
  380. return false;
  381. }
  382. bool BaseXMLParser::FindString(const char* string, String& data, bool escape_brackets)
  383. {
  384. const char first_char = string[0];
  385. bool in_brackets = false;
  386. bool in_string = false;
  387. char previous = 0;
  388. while (!AtEnd())
  389. {
  390. const char c = Look();
  391. // Count line numbers
  392. if (c == '\n')
  393. {
  394. line_number++;
  395. }
  396. if (escape_brackets)
  397. {
  398. const char* error_str = XMLParseTools::ParseDataBrackets(in_brackets, in_string, c, previous);
  399. if (error_str)
  400. {
  401. Log::Message(Log::LT_WARNING, "XML parse error. %s", error_str);
  402. return false;
  403. }
  404. }
  405. if (c == first_char && !in_brackets && PeekString(string))
  406. {
  407. return true;
  408. }
  409. data += c;
  410. previous = c;
  411. Next();
  412. }
  413. return false;
  414. }
  415. bool BaseXMLParser::PeekString(const char* string, bool consume)
  416. {
  417. const size_t start_index = xml_index;
  418. const int start_line = line_number;
  419. bool success = true;
  420. int i = 0;
  421. while (string[i])
  422. {
  423. if (AtEnd())
  424. {
  425. success = false;
  426. break;
  427. }
  428. const char c = Look();
  429. // Count line numbers
  430. if (c == '\n')
  431. {
  432. line_number++;
  433. }
  434. // Seek past all the whitespace if we haven't hit the initial character yet.
  435. if (i == 0 && StringUtilities::IsWhitespace(c))
  436. {
  437. Next();
  438. }
  439. else
  440. {
  441. if (c != string[i])
  442. {
  443. success = false;
  444. break;
  445. }
  446. i++;
  447. Next();
  448. }
  449. }
  450. // Set the index to the start index unless we are consuming.
  451. if (!consume || !success)
  452. {
  453. xml_index = start_index;
  454. line_number = start_line;
  455. }
  456. return success;
  457. }
  458. } // namespace Rml