BaseXMLParser.cpp 11 KB

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