BaseXMLParser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/BaseXMLParser.h"
  30. namespace Rml {
  31. namespace Core {
  32. // Most file layers cache 4k.
  33. const int DEFAULT_BUFFER_SIZE = 4096;
  34. BaseXMLParser::BaseXMLParser()
  35. {
  36. read = nullptr;
  37. buffer = nullptr;
  38. buffer_used = 0;
  39. buffer_size = 0;
  40. open_tag_depth = 0;
  41. }
  42. BaseXMLParser::~BaseXMLParser()
  43. {
  44. }
  45. // Registers a tag as containing general character data.
  46. void BaseXMLParser::RegisterCDATATag(const String& tag)
  47. {
  48. if (!tag.empty())
  49. cdata_tags.insert(StringUtilities::ToLower(tag));
  50. }
  51. // Parses the given stream as an XML file, and calls the handlers when
  52. // interesting phenomenon are encountered.
  53. void BaseXMLParser::Parse(Stream* stream)
  54. {
  55. xml_source = stream;
  56. buffer_size = DEFAULT_BUFFER_SIZE;
  57. buffer = (unsigned char*) malloc(buffer_size);
  58. read = buffer;
  59. line_number = 1;
  60. FillBuffer();
  61. // Read (er ... skip) the header, if one exists.
  62. ReadHeader();
  63. // Read the XML body.
  64. ReadBody();
  65. free(buffer);
  66. }
  67. // Get the current file line number
  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. // Called when the parser finds the beginning of an element tag.
  77. void BaseXMLParser::HandleElementStart(const String& RMLUI_UNUSED_PARAMETER(name), const XMLAttributes& RMLUI_UNUSED_PARAMETER(attributes))
  78. {
  79. RMLUI_UNUSED(name);
  80. RMLUI_UNUSED(attributes);
  81. }
  82. // Called when the parser finds the end of an element tag.
  83. void BaseXMLParser::HandleElementEnd(const String& RMLUI_UNUSED_PARAMETER(name))
  84. {
  85. RMLUI_UNUSED(name);
  86. }
  87. // Called when the parser encounters data.
  88. void BaseXMLParser::HandleData(const String& RMLUI_UNUSED_PARAMETER(data))
  89. {
  90. RMLUI_UNUSED(data);
  91. }
  92. void BaseXMLParser::ReadHeader()
  93. {
  94. if (PeekString((unsigned char*) "<?"))
  95. {
  96. String temp;
  97. FindString((unsigned char*) ">", temp);
  98. }
  99. }
  100. void BaseXMLParser::ReadBody()
  101. {
  102. RMLUI_ZoneScoped;
  103. open_tag_depth = 0;
  104. line_number_open_tag = 0;
  105. for(;;)
  106. {
  107. // Find the next open tag.
  108. if (!FindString((unsigned char*) "<", data))
  109. break;
  110. // Check what kind of tag this is.
  111. if (PeekString((const unsigned char*) "!--"))
  112. {
  113. // Comment.
  114. String temp;
  115. if (!FindString((const unsigned char*) "-->", temp))
  116. break;
  117. }
  118. else if (PeekString((const unsigned char*) "![CDATA["))
  119. {
  120. // CDATA tag; read everything (including markup) until the ending
  121. // CDATA tag.
  122. if (!ReadCDATA())
  123. break;
  124. }
  125. else if (PeekString((const unsigned char*) "/"))
  126. {
  127. if (!ReadCloseTag())
  128. break;
  129. // Bail if we've hit the end of the XML data.
  130. if (open_tag_depth == 0)
  131. {
  132. xml_source->Seek((long)((read - buffer) - buffer_used), SEEK_CUR);
  133. break;
  134. }
  135. }
  136. else
  137. {
  138. if (ReadOpenTag())
  139. line_number_open_tag = line_number;
  140. else
  141. break;
  142. }
  143. }
  144. // Check for error conditions
  145. if (open_tag_depth > 0)
  146. {
  147. Log::Message(Log::LT_WARNING, "XML parse error on line %d of %s.", GetLineNumber(), xml_source->GetSourceURL().GetURL().c_str());
  148. }
  149. }
  150. bool BaseXMLParser::ReadOpenTag()
  151. {
  152. // Increase the open depth
  153. open_tag_depth++;
  154. // Opening tag; send data immediately and open the tag.
  155. if (!data.empty())
  156. {
  157. HandleData(data);
  158. data.clear();
  159. }
  160. String tag_name;
  161. if (!FindWord(tag_name, "/>"))
  162. return false;
  163. bool section_opened = false;
  164. if (PeekString((const unsigned char*) ">"))
  165. {
  166. // Simple open tag.
  167. HandleElementStart(tag_name, XMLAttributes());
  168. section_opened = true;
  169. }
  170. else if (PeekString((const unsigned char*) "/") &&
  171. PeekString((const unsigned char*) ">"))
  172. {
  173. // Empty open tag.
  174. HandleElementStart(tag_name, XMLAttributes());
  175. HandleElementEnd(tag_name);
  176. // Tag immediately closed, reduce count
  177. open_tag_depth--;
  178. }
  179. else
  180. {
  181. // It appears we have some attributes. Let's parse them.
  182. XMLAttributes attributes;
  183. if (!ReadAttributes(attributes))
  184. return false;
  185. if (PeekString((const unsigned char*) ">"))
  186. {
  187. HandleElementStart(tag_name, attributes);
  188. section_opened = true;
  189. }
  190. else if (PeekString((const unsigned char*) "/") &&
  191. PeekString((const unsigned char*) ">"))
  192. {
  193. HandleElementStart(tag_name, attributes);
  194. HandleElementEnd(tag_name);
  195. // Tag immediately closed, reduce count
  196. open_tag_depth--;
  197. }
  198. else
  199. {
  200. return false;
  201. }
  202. }
  203. // Check if this tag needs to processed as CDATA.
  204. if (section_opened)
  205. {
  206. String lcase_tag_name = StringUtilities::ToLower(tag_name);
  207. if (cdata_tags.find(lcase_tag_name) != cdata_tags.end())
  208. {
  209. if (ReadCDATA(lcase_tag_name.c_str()))
  210. {
  211. open_tag_depth--;
  212. if (!data.empty())
  213. {
  214. HandleData(data);
  215. data.clear();
  216. }
  217. HandleElementEnd(tag_name);
  218. return true;
  219. }
  220. return false;
  221. }
  222. }
  223. return true;
  224. }
  225. bool BaseXMLParser::ReadCloseTag()
  226. {
  227. // Closing tag; send data immediately and close the tag.
  228. if (!data.empty())
  229. {
  230. HandleData(data);
  231. data.clear();
  232. }
  233. String tag_name;
  234. if (!FindString((const unsigned char*) ">", tag_name))
  235. return false;
  236. HandleElementEnd(StringUtilities::StripWhitespace(tag_name));
  237. // Tag closed, reduce count
  238. open_tag_depth--;
  239. return true;
  240. }
  241. bool BaseXMLParser::ReadAttributes(XMLAttributes& attributes)
  242. {
  243. for (;;)
  244. {
  245. String attribute;
  246. String value;
  247. // Get the attribute name
  248. if (!FindWord(attribute, "=/>"))
  249. {
  250. return false;
  251. }
  252. // Check if theres an assigned value
  253. if (PeekString((const unsigned char*)"="))
  254. {
  255. if (PeekString((const unsigned char*) "\""))
  256. {
  257. if (!FindString((const unsigned char*) "\"", value))
  258. return false;
  259. }
  260. else if (PeekString((const unsigned char*) "'"))
  261. {
  262. if (!FindString((const unsigned char*) "'", value))
  263. return false;
  264. }
  265. else if (!FindWord(value, "/>"))
  266. {
  267. return false;
  268. }
  269. }
  270. attributes[attribute] = value;
  271. // Check for the end of the tag.
  272. if (PeekString((const unsigned char*) "/", false) ||
  273. PeekString((const unsigned char*) ">", false))
  274. return true;
  275. }
  276. }
  277. bool BaseXMLParser::ReadCDATA(const char* terminator)
  278. {
  279. String cdata;
  280. if (terminator == nullptr)
  281. {
  282. FindString((const unsigned char*) "]]>", cdata);
  283. data += cdata;
  284. return true;
  285. }
  286. else
  287. {
  288. for (;;)
  289. {
  290. // Search for the next tag opening.
  291. if (!FindString((const unsigned char*) "<", cdata))
  292. return false;
  293. if (PeekString((const unsigned char*) "/", false))
  294. {
  295. String tag;
  296. if (FindString((const unsigned char*) ">", tag))
  297. {
  298. size_t slash_pos = tag.find('/');
  299. String tag_name = StringUtilities::StripWhitespace(slash_pos == String::npos ? tag : tag.substr(slash_pos + 1));
  300. if (StringUtilities::ToLower(tag_name) == terminator)
  301. {
  302. data += cdata;
  303. return true;
  304. }
  305. else
  306. {
  307. cdata += "<";
  308. cdata += tag;
  309. cdata += ">";
  310. }
  311. }
  312. else
  313. cdata += "<";
  314. }
  315. else
  316. cdata += "<";
  317. }
  318. }
  319. }
  320. // Reads from the stream until a complete word is found.
  321. bool BaseXMLParser::FindWord(String& word, const char* terminators)
  322. {
  323. for (;;)
  324. {
  325. if (read >= buffer + buffer_used)
  326. {
  327. if (!FillBuffer())
  328. return false;
  329. }
  330. // Ignore white space
  331. if (StringUtilities::IsWhitespace(*read))
  332. {
  333. if (word.empty())
  334. {
  335. read++;
  336. continue;
  337. }
  338. else
  339. return true;
  340. }
  341. // Check for termination condition
  342. if (terminators && strchr(terminators, *read))
  343. {
  344. return !word.empty();
  345. }
  346. word += *read;
  347. read++;
  348. }
  349. }
  350. // Reads from the stream until the given character set is found.
  351. bool BaseXMLParser::FindString(const unsigned char* string, String& data)
  352. {
  353. int index = 0;
  354. while (string[index])
  355. {
  356. if (read >= buffer + buffer_used)
  357. {
  358. if (!FillBuffer())
  359. return false;
  360. }
  361. // Count line numbers
  362. if (*read == '\n')
  363. {
  364. line_number++;
  365. }
  366. if (*read == string[index])
  367. {
  368. index += 1;
  369. }
  370. else
  371. {
  372. if (index > 0)
  373. {
  374. data += String((const char*)string, index);
  375. index = 0;
  376. }
  377. data += *read;
  378. }
  379. read++;
  380. }
  381. return true;
  382. }
  383. // Returns true if the next sequence of characters in the stream matches the
  384. // given string.
  385. bool BaseXMLParser::PeekString(const unsigned char* string, bool consume)
  386. {
  387. unsigned char* peek_read = read;
  388. int i = 0;
  389. while (string[i])
  390. {
  391. // If we're about to read past the end of the buffer, read into the
  392. // overflow buffer.
  393. if ((peek_read - buffer) + i >= buffer_used)
  394. {
  395. int peek_offset = (int)(peek_read - read);
  396. FillBuffer();
  397. peek_read = read + peek_offset;
  398. if (peek_read - buffer + i >= buffer_used)
  399. {
  400. // Wierd, seems our buffer is too small, realloc it bigger.
  401. buffer_size *= 2;
  402. int read_offset = (int)(read - buffer);
  403. unsigned char* new_buffer = (unsigned char*) realloc(buffer, buffer_size);
  404. RMLUI_ASSERTMSG(new_buffer != nullptr, "Unable to allocate larger buffer for Peek() call");
  405. if(new_buffer == nullptr)
  406. {
  407. return false;
  408. }
  409. buffer = new_buffer;
  410. // Restore the read pointers.
  411. read = buffer + read_offset;
  412. peek_read = read + peek_offset;
  413. // Attempt to fill our new buffer size.
  414. if (!FillBuffer())
  415. return false;
  416. }
  417. }
  418. // Seek past all the whitespace if we haven't hit the initial character yet.
  419. if (i == 0 && StringUtilities::IsWhitespace(*peek_read))
  420. {
  421. peek_read++;
  422. }
  423. else
  424. {
  425. if (*peek_read != string[i])
  426. return false;
  427. i++;
  428. peek_read++;
  429. }
  430. }
  431. // Set the read pointer to the end of the peek.
  432. if (consume)
  433. {
  434. read = peek_read;
  435. }
  436. return true;
  437. }
  438. // Fill the buffer as much as possible, without removing any content that is still pending
  439. bool BaseXMLParser::FillBuffer()
  440. {
  441. int bytes_free = buffer_size;
  442. int bytes_remaining = Math::Max((int)(buffer_used - (read - buffer)), 0);
  443. // If theres any data still in the buffer, shift it down, and fill it again
  444. if (bytes_remaining > 0)
  445. {
  446. memmove(buffer, read, bytes_remaining);
  447. bytes_free = buffer_size - bytes_remaining;
  448. }
  449. read = buffer;
  450. size_t bytes_read = xml_source->Read(&buffer[bytes_remaining], bytes_free);
  451. buffer_used = (int)(bytes_read + bytes_remaining);
  452. return bytes_read > 0;
  453. }
  454. }
  455. }