BaseXMLParser.cpp 11 KB

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