BaseXMLParser.cpp 12 KB

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