Debian Codegraph @

core/io/xml_parser.cpp

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