BaseXMLParser.cpp 9.8 KB

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