xml_parser.cpp 13 KB

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