xml_parser.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**************************************************************************/
  2. /* xml_parser.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. static inline bool _is_white_space(char c) {
  34. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  35. }
  36. //! sets the state that text was found. Returns true if set should be set
  37. bool XMLParser::_set_text(const char *start, const char *end) {
  38. // check if text is more than 2 characters, and if not, check if there is
  39. // only white space, so that this text won't be reported
  40. if (end - start < 3) {
  41. const char *p = start;
  42. for (; p != end; ++p) {
  43. if (!_is_white_space(*p)) {
  44. break;
  45. }
  46. }
  47. if (p == end) {
  48. return false;
  49. }
  50. }
  51. // set current text to the parsed text, and replace xml special characters
  52. String s = String::utf8(start, (int)(end - start));
  53. node_name = s.xml_unescape();
  54. // current XML node type is text
  55. node_type = NODE_TEXT;
  56. return true;
  57. }
  58. void XMLParser::_parse_closing_xml_element() {
  59. node_type = NODE_ELEMENT_END;
  60. node_empty = false;
  61. attributes.clear();
  62. next_char();
  63. const char *pBeginClose = P;
  64. while (*P && *P != '>') {
  65. next_char();
  66. }
  67. node_name = String::utf8(pBeginClose, (int)(P - pBeginClose));
  68. #ifdef DEBUG_XML
  69. print_line("XML CLOSE: " + node_name);
  70. #endif
  71. if (*P) {
  72. next_char();
  73. }
  74. }
  75. void XMLParser::_ignore_definition() {
  76. node_type = NODE_UNKNOWN;
  77. const char *F = P;
  78. // move until end marked with '>' reached
  79. while (*P && *P != '>') {
  80. next_char();
  81. }
  82. node_name.parse_utf8(F, P - F);
  83. if (*P) {
  84. next_char();
  85. }
  86. }
  87. bool XMLParser::_parse_cdata() {
  88. if (*(P + 1) != '[') {
  89. return false;
  90. }
  91. node_type = NODE_CDATA;
  92. // skip '<![CDATA['
  93. int count = 0;
  94. while (*P && count < 8) {
  95. next_char();
  96. ++count;
  97. }
  98. if (!*P) {
  99. node_name = "";
  100. return true;
  101. }
  102. const char *cDataBegin = P;
  103. const char *cDataEnd = nullptr;
  104. // find end of CDATA
  105. while (*P && !cDataEnd) {
  106. if (*P == '>' &&
  107. (*(P - 1) == ']') &&
  108. (*(P - 2) == ']')) {
  109. cDataEnd = P - 2;
  110. }
  111. next_char();
  112. }
  113. if (!cDataEnd) {
  114. cDataEnd = P;
  115. }
  116. node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin));
  117. #ifdef DEBUG_XML
  118. print_line("XML CDATA: " + node_name);
  119. #endif
  120. return true;
  121. }
  122. void XMLParser::_parse_comment() {
  123. node_type = NODE_COMMENT;
  124. P += 1;
  125. const char *pEndOfInput = data + length;
  126. const char *pCommentBegin;
  127. const char *pCommentEnd;
  128. if (P + 1 < pEndOfInput && P[0] == '-' && P[1] == '-') {
  129. // Comment, use '-->' as end.
  130. pCommentBegin = P + 2;
  131. for (pCommentEnd = pCommentBegin; pCommentEnd + 2 < pEndOfInput; pCommentEnd++) {
  132. if (pCommentEnd[0] == '-' && pCommentEnd[1] == '-' && pCommentEnd[2] == '>') {
  133. break;
  134. }
  135. }
  136. if (pCommentEnd + 2 < pEndOfInput) {
  137. P = pCommentEnd + 3;
  138. } else {
  139. P = pCommentEnd = pEndOfInput;
  140. }
  141. } else {
  142. // Like document type definition, match angle brackets.
  143. pCommentBegin = P;
  144. int count = 1;
  145. while (*P && count) {
  146. if (*P == '>') {
  147. --count;
  148. } else if (*P == '<') {
  149. ++count;
  150. }
  151. next_char();
  152. }
  153. if (count) {
  154. pCommentEnd = P;
  155. } else {
  156. pCommentEnd = P - 1;
  157. }
  158. }
  159. node_name = String::utf8(pCommentBegin, (int)(pCommentEnd - pCommentBegin));
  160. #ifdef DEBUG_XML
  161. print_line("XML COMMENT: " + node_name);
  162. #endif
  163. }
  164. void XMLParser::_parse_opening_xml_element() {
  165. node_type = NODE_ELEMENT;
  166. node_empty = false;
  167. attributes.clear();
  168. // find name
  169. const char *startName = P;
  170. // find end of element
  171. while (*P && *P != '>' && !_is_white_space(*P)) {
  172. next_char();
  173. }
  174. const char *endName = P;
  175. // find attributes
  176. while (*P && *P != '>') {
  177. if (_is_white_space(*P)) {
  178. next_char();
  179. } else {
  180. if (*P != '/') {
  181. // we've got an attribute
  182. // read the attribute names
  183. const char *attributeNameBegin = P;
  184. while (*P && !_is_white_space(*P) && *P != '=') {
  185. next_char();
  186. }
  187. if (!*P) {
  188. break;
  189. }
  190. const char *attributeNameEnd = P;
  191. next_char();
  192. // read the attribute value
  193. // check for quotes and single quotes, thx to murphy
  194. while ((*P != '\"') && (*P != '\'') && *P) {
  195. next_char();
  196. }
  197. if (!*P) { // malformatted xml file
  198. break;
  199. }
  200. const char attributeQuoteChar = *P;
  201. next_char();
  202. const char *attributeValueBegin = P;
  203. while (*P != attributeQuoteChar && *P) {
  204. next_char();
  205. }
  206. const char *attributeValueEnd = P;
  207. if (*P) {
  208. next_char();
  209. }
  210. Attribute attr;
  211. attr.name = String::utf8(attributeNameBegin,
  212. (int)(attributeNameEnd - attributeNameBegin));
  213. String s = String::utf8(attributeValueBegin,
  214. (int)(attributeValueEnd - attributeValueBegin));
  215. attr.value = s.xml_unescape();
  216. attributes.push_back(attr);
  217. } else {
  218. // tag is closed directly
  219. next_char();
  220. node_empty = true;
  221. break;
  222. }
  223. }
  224. }
  225. // check if this tag is closing directly
  226. if (endName > startName && *(endName - 1) == '/') {
  227. // directly closing tag
  228. node_empty = true;
  229. endName--;
  230. }
  231. node_name = String::utf8(startName, (int)(endName - startName));
  232. #ifdef DEBUG_XML
  233. print_line("XML OPEN: " + node_name);
  234. #endif
  235. if (*P) {
  236. next_char();
  237. }
  238. }
  239. void XMLParser::_parse_current_node() {
  240. const char *start = P;
  241. node_offset = P - data;
  242. // more forward until '<' found
  243. while (*P != '<' && *P) {
  244. next_char();
  245. }
  246. if (P - start > 0) {
  247. // we found some text, store it
  248. if (_set_text(start, P)) {
  249. return;
  250. }
  251. }
  252. if (!*P) {
  253. return;
  254. }
  255. next_char();
  256. // based on current token, parse and report next element
  257. switch (*P) {
  258. case '/':
  259. _parse_closing_xml_element();
  260. break;
  261. case '?':
  262. _ignore_definition();
  263. break;
  264. case '!':
  265. if (!_parse_cdata()) {
  266. _parse_comment();
  267. }
  268. break;
  269. default:
  270. _parse_opening_xml_element();
  271. break;
  272. }
  273. }
  274. uint64_t XMLParser::get_node_offset() const {
  275. return node_offset;
  276. }
  277. Error XMLParser::seek(uint64_t p_pos) {
  278. ERR_FAIL_NULL_V(data, ERR_FILE_EOF);
  279. ERR_FAIL_COND_V(p_pos >= length, ERR_FILE_EOF);
  280. P = data + p_pos;
  281. return read();
  282. }
  283. void XMLParser::_bind_methods() {
  284. ClassDB::bind_method(D_METHOD("read"), &XMLParser::read);
  285. ClassDB::bind_method(D_METHOD("get_node_type"), &XMLParser::get_node_type);
  286. ClassDB::bind_method(D_METHOD("get_node_name"), &XMLParser::get_node_name);
  287. ClassDB::bind_method(D_METHOD("get_node_data"), &XMLParser::get_node_data);
  288. ClassDB::bind_method(D_METHOD("get_node_offset"), &XMLParser::get_node_offset);
  289. ClassDB::bind_method(D_METHOD("get_attribute_count"), &XMLParser::get_attribute_count);
  290. ClassDB::bind_method(D_METHOD("get_attribute_name", "idx"), &XMLParser::get_attribute_name);
  291. ClassDB::bind_method(D_METHOD("get_attribute_value", "idx"), &XMLParser::get_attribute_value);
  292. ClassDB::bind_method(D_METHOD("has_attribute", "name"), &XMLParser::has_attribute);
  293. ClassDB::bind_method(D_METHOD("get_named_attribute_value", "name"), &XMLParser::get_named_attribute_value);
  294. ClassDB::bind_method(D_METHOD("get_named_attribute_value_safe", "name"), &XMLParser::get_named_attribute_value_safe);
  295. ClassDB::bind_method(D_METHOD("is_empty"), &XMLParser::is_empty);
  296. ClassDB::bind_method(D_METHOD("get_current_line"), &XMLParser::get_current_line);
  297. ClassDB::bind_method(D_METHOD("skip_section"), &XMLParser::skip_section);
  298. ClassDB::bind_method(D_METHOD("seek", "position"), &XMLParser::seek);
  299. ClassDB::bind_method(D_METHOD("open", "file"), &XMLParser::open);
  300. ClassDB::bind_method(D_METHOD("open_buffer", "buffer"), &XMLParser::open_buffer);
  301. BIND_ENUM_CONSTANT(NODE_NONE);
  302. BIND_ENUM_CONSTANT(NODE_ELEMENT);
  303. BIND_ENUM_CONSTANT(NODE_ELEMENT_END);
  304. BIND_ENUM_CONSTANT(NODE_TEXT);
  305. BIND_ENUM_CONSTANT(NODE_COMMENT);
  306. BIND_ENUM_CONSTANT(NODE_CDATA);
  307. BIND_ENUM_CONSTANT(NODE_UNKNOWN);
  308. }
  309. Error XMLParser::read() {
  310. // if end not reached, parse the node
  311. if (P && (P - data) < (int64_t)length - 1 && *P != 0) {
  312. _parse_current_node();
  313. return OK;
  314. }
  315. return ERR_FILE_EOF;
  316. }
  317. XMLParser::NodeType XMLParser::get_node_type() {
  318. return node_type;
  319. }
  320. String XMLParser::get_node_data() const {
  321. ERR_FAIL_COND_V(node_type != NODE_TEXT, "");
  322. return node_name;
  323. }
  324. String XMLParser::get_node_name() const {
  325. ERR_FAIL_COND_V(node_type == NODE_TEXT, "");
  326. return node_name;
  327. }
  328. int XMLParser::get_attribute_count() const {
  329. return attributes.size();
  330. }
  331. String XMLParser::get_attribute_name(int p_idx) const {
  332. ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
  333. return attributes[p_idx].name;
  334. }
  335. String XMLParser::get_attribute_value(int p_idx) const {
  336. ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
  337. return attributes[p_idx].value;
  338. }
  339. bool XMLParser::has_attribute(const String &p_name) const {
  340. for (int i = 0; i < attributes.size(); i++) {
  341. if (attributes[i].name == p_name) {
  342. return true;
  343. }
  344. }
  345. return false;
  346. }
  347. String XMLParser::get_named_attribute_value(const String &p_name) const {
  348. int idx = -1;
  349. for (int i = 0; i < attributes.size(); i++) {
  350. if (attributes[i].name == p_name) {
  351. idx = i;
  352. break;
  353. }
  354. }
  355. ERR_FAIL_COND_V_MSG(idx < 0, "", vformat("Attribute not found: '%s'.", p_name));
  356. return attributes[idx].value;
  357. }
  358. String XMLParser::get_named_attribute_value_safe(const String &p_name) const {
  359. int idx = -1;
  360. for (int i = 0; i < attributes.size(); i++) {
  361. if (attributes[i].name == p_name) {
  362. idx = i;
  363. break;
  364. }
  365. }
  366. if (idx < 0) {
  367. return "";
  368. }
  369. return attributes[idx].value;
  370. }
  371. bool XMLParser::is_empty() const {
  372. return node_empty;
  373. }
  374. Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) {
  375. ERR_FAIL_COND_V(p_buffer.is_empty(), ERR_INVALID_DATA);
  376. if (data_copy) {
  377. memdelete_arr(data_copy);
  378. data_copy = nullptr;
  379. }
  380. length = p_buffer.size();
  381. data_copy = memnew_arr(char, length + 1);
  382. memcpy(data_copy, p_buffer.ptr(), length);
  383. data_copy[length] = 0;
  384. data = data_copy;
  385. P = data;
  386. current_line = 0;
  387. return OK;
  388. }
  389. Error XMLParser::_open_buffer(const uint8_t *p_buffer, size_t p_size) {
  390. ERR_FAIL_COND_V(p_size == 0, ERR_INVALID_DATA);
  391. ERR_FAIL_NULL_V(p_buffer, ERR_INVALID_DATA);
  392. if (data_copy) {
  393. memdelete_arr(data_copy);
  394. data_copy = nullptr;
  395. }
  396. length = p_size;
  397. data = (const char *)p_buffer;
  398. P = data;
  399. current_line = 0;
  400. return OK;
  401. }
  402. Error XMLParser::open(const String &p_path) {
  403. Error err;
  404. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::READ, &err);
  405. ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot open file '%s'.", p_path));
  406. length = file->get_length();
  407. ERR_FAIL_COND_V(length < 1, ERR_FILE_CORRUPT);
  408. if (data_copy) {
  409. memdelete_arr(data_copy);
  410. data_copy = nullptr;
  411. }
  412. data_copy = memnew_arr(char, length + 1);
  413. file->get_buffer((uint8_t *)data_copy, length);
  414. data_copy[length] = 0;
  415. data = data_copy;
  416. P = data;
  417. current_line = 0;
  418. return OK;
  419. }
  420. void XMLParser::skip_section() {
  421. // skip if this element is empty anyway.
  422. if (is_empty()) {
  423. return;
  424. }
  425. // read until we've reached the last element in this section
  426. int tagcount = 1;
  427. while (tagcount && read() == OK) {
  428. if (get_node_type() == XMLParser::NODE_ELEMENT &&
  429. !is_empty()) {
  430. ++tagcount;
  431. } else if (get_node_type() == XMLParser::NODE_ELEMENT_END) {
  432. --tagcount;
  433. }
  434. }
  435. }
  436. void XMLParser::close() {
  437. if (data_copy) {
  438. memdelete_arr(data);
  439. data_copy = nullptr;
  440. }
  441. data = nullptr;
  442. length = 0;
  443. P = nullptr;
  444. node_empty = false;
  445. node_type = NODE_NONE;
  446. node_offset = 0;
  447. }
  448. int XMLParser::get_current_line() const {
  449. return current_line;
  450. }
  451. XMLParser::~XMLParser() {
  452. if (data_copy) {
  453. memdelete_arr(data_copy);
  454. data_copy = nullptr;
  455. }
  456. }