BaseXMLParser.cpp 11 KB

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