xml_parser.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*************************************************************************/
  2. /* xml_parser.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "xml_parser.h"
  31. #include "core/string/print_string.h"
  32. //#define DEBUG_XML
  33. VARIANT_ENUM_CAST(XMLParser::NodeType);
  34. static inline bool _is_white_space(char c) {
  35. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  36. }
  37. //! sets the state that text was found. Returns true if set should be set
  38. bool XMLParser::_set_text(char *start, char *end) {
  39. // check if text is more than 2 characters, and if not, check if there is
  40. // only white space, so that this text won't be reported
  41. if (end - start < 3) {
  42. char *p = start;
  43. for (; p != end; ++p) {
  44. if (!_is_white_space(*p)) {
  45. break;
  46. }
  47. }
  48. if (p == end) {
  49. return false;
  50. }
  51. }
  52. // set current text to the parsed text, and replace xml special characters
  53. String s = String::utf8(start, (int)(end - start));
  54. node_name = s.xml_unescape();
  55. // current XML node type is text
  56. node_type = NODE_TEXT;
  57. return true;
  58. }
  59. void XMLParser::_parse_closing_xml_element() {
  60. node_type = NODE_ELEMENT_END;
  61. node_empty = false;
  62. attributes.clear();
  63. ++P;
  64. const char *pBeginClose = P;
  65. while (*P != '>') {
  66. ++P;
  67. }
  68. node_name = String::utf8(pBeginClose, (int)(P - pBeginClose));
  69. #ifdef DEBUG_XML
  70. print_line("XML CLOSE: " + node_name);
  71. #endif
  72. ++P;
  73. }
  74. void XMLParser::_ignore_definition() {
  75. node_type = NODE_UNKNOWN;
  76. char *F = P;
  77. // move until end marked with '>' reached
  78. while (*P != '>') {
  79. ++P;
  80. }
  81. node_name.parse_utf8(F, P - F);
  82. ++P;
  83. }
  84. bool XMLParser::_parse_cdata() {
  85. if (*(P + 1) != '[') {
  86. return false;
  87. }
  88. node_type = NODE_CDATA;
  89. // skip '<![CDATA['
  90. int count = 0;
  91. while (*P && count < 8) {
  92. ++P;
  93. ++count;
  94. }
  95. if (!*P) {
  96. return true;
  97. }
  98. char *cDataBegin = P;
  99. char *cDataEnd = nullptr;
  100. // find end of CDATA
  101. while (*P && !cDataEnd) {
  102. if (*P == '>' &&
  103. (*(P - 1) == ']') &&
  104. (*(P - 2) == ']')) {
  105. cDataEnd = P - 2;
  106. }
  107. ++P;
  108. }
  109. if (cDataEnd) {
  110. node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin));
  111. } else {
  112. node_name = "";
  113. }
  114. #ifdef DEBUG_XML
  115. print_line("XML CDATA: " + node_name);
  116. #endif
  117. return true;
  118. }
  119. void XMLParser::_parse_comment() {
  120. node_type = NODE_COMMENT;
  121. P += 1;
  122. char *pCommentBegin = P;
  123. int count = 1;
  124. // move until end of comment reached
  125. while (count) {
  126. if (*P == '>') {
  127. --count;
  128. } else if (*P == '<') {
  129. ++count;
  130. }
  131. ++P;
  132. }
  133. P -= 3;
  134. node_name = String::utf8(pCommentBegin + 2, (int)(P - pCommentBegin - 2));
  135. P += 3;
  136. #ifdef DEBUG_XML
  137. print_line("XML COMMENT: " + node_name);
  138. #endif
  139. }
  140. void XMLParser::_parse_opening_xml_element() {
  141. node_type = NODE_ELEMENT;
  142. node_empty = false;
  143. attributes.clear();
  144. // find name
  145. const char *startName = P;
  146. // find end of element
  147. while (*P != '>' && !_is_white_space(*P)) {
  148. ++P;
  149. }
  150. const char *endName = P;
  151. // find attributes
  152. while (*P != '>') {
  153. if (_is_white_space(*P)) {
  154. ++P;
  155. } else {
  156. if (*P != '/') {
  157. // we've got an attribute
  158. // read the attribute names
  159. const char *attributeNameBegin = P;
  160. while (!_is_white_space(*P) && *P != '=') {
  161. ++P;
  162. }
  163. const char *attributeNameEnd = P;
  164. ++P;
  165. // read the attribute value
  166. // check for quotes and single quotes, thx to murphy
  167. while ((*P != '\"') && (*P != '\'') && *P) {
  168. ++P;
  169. }
  170. if (!*P) { // malformatted xml file
  171. return;
  172. }
  173. const char attributeQuoteChar = *P;
  174. ++P;
  175. const char *attributeValueBegin = P;
  176. while (*P != attributeQuoteChar && *P) {
  177. ++P;
  178. }
  179. if (!*P) { // malformatted xml file
  180. return;
  181. }
  182. const char *attributeValueEnd = P;
  183. ++P;
  184. Attribute attr;
  185. attr.name = String::utf8(attributeNameBegin,
  186. (int)(attributeNameEnd - attributeNameBegin));
  187. String s = String::utf8(attributeValueBegin,
  188. (int)(attributeValueEnd - attributeValueBegin));
  189. attr.value = s.xml_unescape();
  190. attributes.push_back(attr);
  191. } else {
  192. // tag is closed directly
  193. ++P;
  194. node_empty = true;
  195. break;
  196. }
  197. }
  198. }
  199. // check if this tag is closing directly
  200. if (endName > startName && *(endName - 1) == '/') {
  201. // directly closing tag
  202. node_empty = true;
  203. endName--;
  204. }
  205. node_name = String::utf8(startName, (int)(endName - startName));
  206. #ifdef DEBUG_XML
  207. print_line("XML OPEN: " + node_name);
  208. #endif
  209. ++P;
  210. }
  211. void XMLParser::_parse_current_node() {
  212. char *start = P;
  213. node_offset = P - data;
  214. // more forward until '<' found
  215. while (*P != '<' && *P) {
  216. ++P;
  217. }
  218. if (!*P) {
  219. return;
  220. }
  221. if (P - start > 0) {
  222. // we found some text, store it
  223. if (_set_text(start, P)) {
  224. return;
  225. }
  226. }
  227. ++P;
  228. // based on current token, parse and report next element
  229. switch (*P) {
  230. case '/':
  231. _parse_closing_xml_element();
  232. break;
  233. case '?':
  234. _ignore_definition();
  235. break;
  236. case '!':
  237. if (!_parse_cdata()) {
  238. _parse_comment();
  239. }
  240. break;
  241. default:
  242. _parse_opening_xml_element();
  243. break;
  244. }
  245. }
  246. uint64_t XMLParser::get_node_offset() const {
  247. return node_offset;
  248. }
  249. Error XMLParser::seek(uint64_t p_pos) {
  250. ERR_FAIL_COND_V(!data, ERR_FILE_EOF);
  251. ERR_FAIL_COND_V(p_pos >= length, ERR_FILE_EOF);
  252. P = data + p_pos;
  253. return read();
  254. }
  255. void XMLParser::_bind_methods() {
  256. ClassDB::bind_method(D_METHOD("read"), &XMLParser::read);
  257. ClassDB::bind_method(D_METHOD("get_node_type"), &XMLParser::get_node_type);
  258. ClassDB::bind_method(D_METHOD("get_node_name"), &XMLParser::get_node_name);
  259. ClassDB::bind_method(D_METHOD("get_node_data"), &XMLParser::get_node_data);
  260. ClassDB::bind_method(D_METHOD("get_node_offset"), &XMLParser::get_node_offset);
  261. ClassDB::bind_method(D_METHOD("get_attribute_count"), &XMLParser::get_attribute_count);
  262. ClassDB::bind_method(D_METHOD("get_attribute_name", "idx"), &XMLParser::get_attribute_name);
  263. ClassDB::bind_method(D_METHOD("get_attribute_value", "idx"), (String(XMLParser::*)(int) const) & XMLParser::get_attribute_value);
  264. ClassDB::bind_method(D_METHOD("has_attribute", "name"), &XMLParser::has_attribute);
  265. ClassDB::bind_method(D_METHOD("get_named_attribute_value", "name"), (String(XMLParser::*)(const String &) const) & XMLParser::get_attribute_value);
  266. ClassDB::bind_method(D_METHOD("get_named_attribute_value_safe", "name"), &XMLParser::get_attribute_value_safe);
  267. ClassDB::bind_method(D_METHOD("is_empty"), &XMLParser::is_empty);
  268. ClassDB::bind_method(D_METHOD("get_current_line"), &XMLParser::get_current_line);
  269. ClassDB::bind_method(D_METHOD("skip_section"), &XMLParser::skip_section);
  270. ClassDB::bind_method(D_METHOD("seek", "position"), &XMLParser::seek);
  271. ClassDB::bind_method(D_METHOD("open", "file"), &XMLParser::open);
  272. ClassDB::bind_method(D_METHOD("open_buffer", "buffer"), &XMLParser::open_buffer);
  273. BIND_ENUM_CONSTANT(NODE_NONE);
  274. BIND_ENUM_CONSTANT(NODE_ELEMENT);
  275. BIND_ENUM_CONSTANT(NODE_ELEMENT_END);
  276. BIND_ENUM_CONSTANT(NODE_TEXT);
  277. BIND_ENUM_CONSTANT(NODE_COMMENT);
  278. BIND_ENUM_CONSTANT(NODE_CDATA);
  279. BIND_ENUM_CONSTANT(NODE_UNKNOWN);
  280. }
  281. Error XMLParser::read() {
  282. // if end not reached, parse the node
  283. if (P && (P - data) < (int64_t)length - 1 && *P != 0) {
  284. _parse_current_node();
  285. return OK;
  286. }
  287. return ERR_FILE_EOF;
  288. }
  289. XMLParser::NodeType XMLParser::get_node_type() {
  290. return node_type;
  291. }
  292. String XMLParser::get_node_data() const {
  293. ERR_FAIL_COND_V(node_type != NODE_TEXT, "");
  294. return node_name;
  295. }
  296. String XMLParser::get_node_name() const {
  297. ERR_FAIL_COND_V(node_type == NODE_TEXT, "");
  298. return node_name;
  299. }
  300. int XMLParser::get_attribute_count() const {
  301. return attributes.size();
  302. }
  303. String XMLParser::get_attribute_name(int p_idx) const {
  304. ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
  305. return attributes[p_idx].name;
  306. }
  307. String XMLParser::get_attribute_value(int p_idx) const {
  308. ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
  309. return attributes[p_idx].value;
  310. }
  311. bool XMLParser::has_attribute(const String &p_name) const {
  312. for (int i = 0; i < attributes.size(); i++) {
  313. if (attributes[i].name == p_name) {
  314. return true;
  315. }
  316. }
  317. return false;
  318. }
  319. String XMLParser::get_attribute_value(const String &p_name) const {
  320. int idx = -1;
  321. for (int i = 0; i < attributes.size(); i++) {
  322. if (attributes[i].name == p_name) {
  323. idx = i;
  324. break;
  325. }
  326. }
  327. ERR_FAIL_COND_V_MSG(idx < 0, "", "Attribute not found: " + p_name + ".");
  328. return attributes[idx].value;
  329. }
  330. String XMLParser::get_attribute_value_safe(const String &p_name) const {
  331. int idx = -1;
  332. for (int i = 0; i < attributes.size(); i++) {
  333. if (attributes[i].name == p_name) {
  334. idx = i;
  335. break;
  336. }
  337. }
  338. if (idx < 0) {
  339. return "";
  340. }
  341. return attributes[idx].value;
  342. }
  343. bool XMLParser::is_empty() const {
  344. return node_empty;
  345. }
  346. Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) {
  347. ERR_FAIL_COND_V(p_buffer.size() == 0, ERR_INVALID_DATA);
  348. if (data) {
  349. memdelete_arr(data);
  350. }
  351. length = p_buffer.size();
  352. data = memnew_arr(char, length + 1);
  353. copymem(data, p_buffer.ptr(), length);
  354. data[length] = 0;
  355. P = data;
  356. return OK;
  357. }
  358. Error XMLParser::open(const String &p_path) {
  359. Error err;
  360. FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err);
  361. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
  362. length = file->get_len();
  363. ERR_FAIL_COND_V(length < 1, ERR_FILE_CORRUPT);
  364. if (data) {
  365. memdelete_arr(data);
  366. }
  367. data = memnew_arr(char, length + 1);
  368. file->get_buffer((uint8_t *)data, length);
  369. data[length] = 0;
  370. P = data;
  371. memdelete(file);
  372. return OK;
  373. }
  374. void XMLParser::skip_section() {
  375. // skip if this element is empty anyway.
  376. if (is_empty()) {
  377. return;
  378. }
  379. // read until we've reached the last element in this section
  380. int tagcount = 1;
  381. while (tagcount && read() == OK) {
  382. if (get_node_type() == XMLParser::NODE_ELEMENT &&
  383. !is_empty()) {
  384. ++tagcount;
  385. } else if (get_node_type() == XMLParser::NODE_ELEMENT_END) {
  386. --tagcount;
  387. }
  388. }
  389. }
  390. void XMLParser::close() {
  391. if (data) {
  392. memdelete_arr(data);
  393. }
  394. data = nullptr;
  395. length = 0;
  396. P = nullptr;
  397. node_empty = false;
  398. node_type = NODE_NONE;
  399. node_offset = 0;
  400. }
  401. int XMLParser::get_current_line() const {
  402. return 0;
  403. }
  404. XMLParser::XMLParser() {
  405. }
  406. XMLParser::~XMLParser() {
  407. if (data) {
  408. memdelete_arr(data);
  409. }
  410. }