BaseXMLParser.cpp 11 KB

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