2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* xml_parser.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2019-01-01 12:53:14 +01:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "xml_parser.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
|
|
|
#include "core/print_string.h"
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//#define DEBUG_XML
|
|
|
|
|
2014-12-15 19:42:58 +01:00
|
|
|
VARIANT_ENUM_CAST(XMLParser::NodeType);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static bool _equalsn(const CharType *str1, const CharType *str2, int len) {
|
2014-02-10 02:10:30 +01:00
|
|
|
int i;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (i = 0; i < len && str1[i] && str2[i]; ++i)
|
|
|
|
if (str1[i] != str2[i])
|
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// if one (or both) of the strings was smaller then they
|
2017-03-24 21:45:31 +01:00
|
|
|
// are only equal if they have the same length
|
2014-02-10 02:10:30 +01:00
|
|
|
return (i == len) || (str1[i] == 0 && str2[i] == 0);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String XMLParser::_replace_special_characters(const String &origstr) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
int pos = origstr.find("&");
|
|
|
|
int oldPos = 0;
|
|
|
|
|
|
|
|
if (pos == -1)
|
|
|
|
return origstr;
|
|
|
|
|
|
|
|
String newstr;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (pos != -1 && pos < origstr.length() - 2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// check if it is one of the special characters
|
|
|
|
|
|
|
|
int specialChar = -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < (int)special_characters.size(); ++i) {
|
|
|
|
const CharType *p = &origstr[pos] + 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (_equalsn(&special_characters[i][1], p, special_characters[i].length() - 1)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
specialChar = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (specialChar != -1) {
|
|
|
|
newstr += (origstr.substr(oldPos, pos - oldPos));
|
|
|
|
newstr += (special_characters[specialChar][0]);
|
2014-02-10 02:10:30 +01:00
|
|
|
pos += special_characters[specialChar].length();
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
|
|
|
newstr += (origstr.substr(oldPos, pos - oldPos + 1));
|
2014-02-10 02:10:30 +01:00
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find next &
|
|
|
|
oldPos = pos;
|
|
|
|
pos = origstr.find("&", pos);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (oldPos < origstr.length() - 1)
|
|
|
|
newstr += (origstr.substr(oldPos, origstr.length() - oldPos));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
static inline bool _is_white_space(char c) {
|
|
|
|
return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//! sets the state that text was found. Returns true if set should be set
|
2017-03-05 16:44:50 +01:00
|
|
|
bool XMLParser::_set_text(char *start, char *end) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// check if text is more than 2 characters, and if not, check if there is
|
|
|
|
// only white space, so that this text won't be reported
|
2017-03-05 16:44:50 +01:00
|
|
|
if (end - start < 3) {
|
|
|
|
char *p = start;
|
|
|
|
for (; p != end; ++p)
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!_is_white_space(*p))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (p == end)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set current text to the parsed text, and replace xml special characters
|
|
|
|
String s = String::utf8(start, (int)(end - start));
|
|
|
|
node_name = _replace_special_characters(s);
|
|
|
|
|
|
|
|
// current XML node type is text
|
|
|
|
node_type = NODE_TEXT;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::_parse_closing_xml_element() {
|
|
|
|
node_type = NODE_ELEMENT_END;
|
|
|
|
node_empty = false;
|
|
|
|
attributes.clear();
|
|
|
|
|
|
|
|
++P;
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *pBeginClose = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P != '>')
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
|
|
|
node_name = String::utf8(pBeginClose, (int)(P - pBeginClose));
|
|
|
|
#ifdef DEBUG_XML
|
2017-03-05 16:44:50 +01:00
|
|
|
print_line("XML CLOSE: " + node_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::_ignore_definition() {
|
|
|
|
node_type = NODE_UNKNOWN;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char *F = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
// move until end marked with '>' reached
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P != '>')
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
2017-03-05 16:44:50 +01:00
|
|
|
node_name.parse_utf8(F, P - F);
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XMLParser::_parse_cdata() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (*(P + 1) != '[')
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
node_type = NODE_CDATA;
|
|
|
|
|
|
|
|
// skip '<![CDATA['
|
2017-03-05 16:44:50 +01:00
|
|
|
int count = 0;
|
|
|
|
while (*P && count < 8) {
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*P)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
char *cDataBegin = P;
|
|
|
|
char *cDataEnd = 0;
|
|
|
|
|
|
|
|
// find end of CDATA
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P && !cDataEnd) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (*P == '>' &&
|
2017-03-05 16:44:50 +01:00
|
|
|
(*(P - 1) == ']') &&
|
|
|
|
(*(P - 2) == ']')) {
|
2014-02-10 02:10:30 +01:00
|
|
|
cDataEnd = P - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (cDataEnd)
|
2014-02-10 02:10:30 +01:00
|
|
|
node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin));
|
|
|
|
else
|
|
|
|
node_name = "";
|
|
|
|
#ifdef DEBUG_XML
|
2017-03-05 16:44:50 +01:00
|
|
|
print_line("XML CDATA: " + node_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::_parse_comment() {
|
|
|
|
|
|
|
|
node_type = NODE_COMMENT;
|
|
|
|
P += 1;
|
|
|
|
|
|
|
|
char *pCommentBegin = P;
|
|
|
|
|
|
|
|
int count = 1;
|
|
|
|
|
|
|
|
// move until end of comment reached
|
2017-03-05 16:44:50 +01:00
|
|
|
while (count) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (*P == '>')
|
|
|
|
--count;
|
2017-03-05 16:44:50 +01:00
|
|
|
else if (*P == '<')
|
2014-02-10 02:10:30 +01:00
|
|
|
++count;
|
|
|
|
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
|
|
|
P -= 3;
|
2017-03-05 16:44:50 +01:00
|
|
|
node_name = String::utf8(pCommentBegin + 2, (int)(P - pCommentBegin - 2));
|
2014-02-10 02:10:30 +01:00
|
|
|
P += 3;
|
|
|
|
#ifdef DEBUG_XML
|
2017-03-05 16:44:50 +01:00
|
|
|
print_line("XML COMMENT: " + node_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::_parse_opening_xml_element() {
|
|
|
|
|
|
|
|
node_type = NODE_ELEMENT;
|
|
|
|
node_empty = false;
|
|
|
|
attributes.clear();
|
|
|
|
|
|
|
|
// find name
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *startName = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// find end of element
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P != '>' && !_is_white_space(*P))
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *endName = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// find attributes
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P != '>') {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (_is_white_space(*P))
|
|
|
|
++P;
|
2017-03-05 16:44:50 +01:00
|
|
|
else {
|
|
|
|
if (*P != '/') {
|
2014-02-10 02:10:30 +01:00
|
|
|
// we've got an attribute
|
|
|
|
|
|
|
|
// read the attribute names
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *attributeNameBegin = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (!_is_white_space(*P) && *P != '=')
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *attributeNameEnd = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
|
|
|
// read the attribute value
|
|
|
|
// check for quotes and single quotes, thx to murphy
|
2017-03-05 16:44:50 +01:00
|
|
|
while ((*P != '\"') && (*P != '\'') && *P)
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
|
|
|
if (!*P) // malformatted xml file
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char attributeQuoteChar = *P;
|
|
|
|
|
|
|
|
++P;
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *attributeValueBegin = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P != attributeQuoteChar && *P)
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
|
|
|
if (!*P) // malformatted xml file
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const char *attributeValueEnd = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
|
|
|
Attribute attr;
|
|
|
|
attr.name = String::utf8(attributeNameBegin,
|
2017-03-05 16:44:50 +01:00
|
|
|
(int)(attributeNameEnd - attributeNameBegin));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String s = String::utf8(attributeValueBegin,
|
|
|
|
(int)(attributeValueEnd - attributeValueBegin));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
attr.value = _replace_special_characters(s);
|
|
|
|
attributes.push_back(attr);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
// tag is closed directly
|
|
|
|
++P;
|
|
|
|
node_empty = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if this tag is closing directly
|
2017-03-05 16:44:50 +01:00
|
|
|
if (endName > startName && *(endName - 1) == '/') {
|
2014-02-10 02:10:30 +01:00
|
|
|
// directly closing tag
|
|
|
|
node_empty = true;
|
|
|
|
endName--;
|
|
|
|
}
|
|
|
|
|
|
|
|
node_name = String::utf8(startName, (int)(endName - startName));
|
|
|
|
#ifdef DEBUG_XML
|
2017-03-05 16:44:50 +01:00
|
|
|
print_line("XML OPEN: " + node_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::_parse_current_node() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
char *start = P;
|
2014-02-10 02:10:30 +01:00
|
|
|
node_offset = P - data;
|
|
|
|
|
|
|
|
// more forward until '<' found
|
2017-03-05 16:44:50 +01:00
|
|
|
while (*P != '<' && *P)
|
2014-02-10 02:10:30 +01:00
|
|
|
++P;
|
|
|
|
|
|
|
|
if (!*P)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (P - start > 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
// we found some text, store it
|
|
|
|
if (_set_text(start, P))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
++P;
|
|
|
|
|
|
|
|
// based on current token, parse and report next element
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (*P) {
|
|
|
|
case '/':
|
|
|
|
_parse_closing_xml_element();
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
_ignore_definition();
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
if (!_parse_cdata())
|
|
|
|
_parse_comment();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_parse_opening_xml_element();
|
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t XMLParser::get_node_offset() const {
|
|
|
|
|
|
|
|
return node_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
Error XMLParser::seek(uint64_t p_pos) {
|
|
|
|
|
2019-06-11 14:49:34 +02:00
|
|
|
ERR_FAIL_COND_V(!data, ERR_FILE_EOF);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND_V(p_pos >= length, ERR_FILE_EOF);
|
|
|
|
|
|
|
|
P = data + p_pos;
|
|
|
|
|
|
|
|
return read();
|
|
|
|
};
|
|
|
|
|
|
|
|
void XMLParser::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("read"), &XMLParser::read);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_type"), &XMLParser::get_node_type);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_name"), &XMLParser::get_node_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_data"), &XMLParser::get_node_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_offset"), &XMLParser::get_node_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_attribute_count"), &XMLParser::get_attribute_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_attribute_name", "idx"), &XMLParser::get_attribute_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_attribute_value", "idx"), (String(XMLParser::*)(int) const) & XMLParser::get_attribute_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_attribute", "name"), &XMLParser::has_attribute);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_named_attribute_value", "name"), (String(XMLParser::*)(const String &) const) & XMLParser::get_attribute_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_named_attribute_value_safe", "name"), &XMLParser::get_attribute_value_safe);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_empty"), &XMLParser::is_empty);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_current_line"), &XMLParser::get_current_line);
|
|
|
|
ClassDB::bind_method(D_METHOD("skip_section"), &XMLParser::skip_section);
|
2017-09-10 15:37:49 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("seek", "position"), &XMLParser::seek);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("open", "file"), &XMLParser::open);
|
|
|
|
ClassDB::bind_method(D_METHOD("open_buffer", "buffer"), &XMLParser::open_buffer);
|
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(NODE_NONE);
|
|
|
|
BIND_ENUM_CONSTANT(NODE_ELEMENT);
|
|
|
|
BIND_ENUM_CONSTANT(NODE_ELEMENT_END);
|
|
|
|
BIND_ENUM_CONSTANT(NODE_TEXT);
|
|
|
|
BIND_ENUM_CONSTANT(NODE_COMMENT);
|
|
|
|
BIND_ENUM_CONSTANT(NODE_CDATA);
|
|
|
|
BIND_ENUM_CONSTANT(NODE_UNKNOWN);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Error XMLParser::read() {
|
|
|
|
|
|
|
|
// if not end reached, parse the node
|
2017-08-31 23:30:35 +02:00
|
|
|
if (P && (P - data) < (int64_t)length - 1 && *P != 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
_parse_current_node();
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_FILE_EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLParser::NodeType XMLParser::get_node_type() {
|
|
|
|
|
|
|
|
return node_type;
|
|
|
|
}
|
|
|
|
String XMLParser::get_node_data() const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(node_type != NODE_TEXT, "");
|
2014-02-10 02:10:30 +01:00
|
|
|
return node_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
String XMLParser::get_node_name() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(node_type == NODE_TEXT, "");
|
2014-02-10 02:10:30 +01:00
|
|
|
return node_name;
|
|
|
|
}
|
|
|
|
int XMLParser::get_attribute_count() const {
|
|
|
|
|
|
|
|
return attributes.size();
|
|
|
|
}
|
|
|
|
String XMLParser::get_attribute_name(int p_idx) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
|
2014-02-10 02:10:30 +01:00
|
|
|
return attributes[p_idx].name;
|
|
|
|
}
|
|
|
|
String XMLParser::get_attribute_value(int p_idx) const {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, attributes.size(), "");
|
2014-02-10 02:10:30 +01:00
|
|
|
return attributes[p_idx].value;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
bool XMLParser::has_attribute(const String &p_name) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < attributes.size(); i++) {
|
|
|
|
if (attributes[i].name == p_name)
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
String XMLParser::get_attribute_value(const String &p_name) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = -1;
|
|
|
|
for (int i = 0; i < attributes.size(); i++) {
|
|
|
|
if (attributes[i].name == p_name) {
|
|
|
|
idx = i;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (idx < 0) {
|
|
|
|
ERR_EXPLAIN("Attribute not found: " + p_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(idx < 0, "");
|
2014-02-10 02:10:30 +01:00
|
|
|
return attributes[idx].value;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String XMLParser::get_attribute_value_safe(const String &p_name) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int idx = -1;
|
|
|
|
for (int i = 0; i < attributes.size(); i++) {
|
|
|
|
if (attributes[i].name == p_name) {
|
|
|
|
idx = i;
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (idx < 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return "";
|
|
|
|
return attributes[idx].value;
|
|
|
|
}
|
|
|
|
bool XMLParser::is_empty() const {
|
|
|
|
|
|
|
|
return node_empty;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) {
|
2014-02-16 01:16:33 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(p_buffer.size() == 0, ERR_INVALID_DATA);
|
2014-02-16 01:16:33 +01:00
|
|
|
|
|
|
|
length = p_buffer.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
data = memnew_arr(char, length + 1);
|
|
|
|
copymem(data, p_buffer.ptr(), length);
|
|
|
|
data[length] = 0;
|
|
|
|
P = data;
|
2014-02-16 01:16:33 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error XMLParser::open(const String &p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Error err;
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (err) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
length = file->get_len();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(length < 1, ERR_FILE_CORRUPT);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data = memnew_arr(char, length + 1);
|
|
|
|
file->get_buffer((uint8_t *)data, length);
|
|
|
|
data[length] = 0;
|
|
|
|
P = data;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
memdelete(file);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::skip_section() {
|
|
|
|
|
|
|
|
// skip if this element is empty anyway.
|
|
|
|
if (is_empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// read until we've reached the last element in this section
|
|
|
|
int tagcount = 1;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
while (tagcount && read() == OK) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (get_node_type() == XMLParser::NODE_ELEMENT &&
|
2017-03-05 16:44:50 +01:00
|
|
|
!is_empty()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
++tagcount;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (get_node_type() == XMLParser::NODE_ELEMENT_END)
|
2014-02-10 02:10:30 +01:00
|
|
|
--tagcount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XMLParser::close() {
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
memdelete_arr(data);
|
2017-03-05 16:44:50 +01:00
|
|
|
data = NULL;
|
|
|
|
length = 0;
|
|
|
|
P = NULL;
|
|
|
|
node_empty = false;
|
|
|
|
node_type = NODE_NONE;
|
2014-02-10 02:10:30 +01:00
|
|
|
node_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int XMLParser::get_current_line() const {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLParser::XMLParser() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
close();
|
|
|
|
special_characters.push_back("&");
|
|
|
|
special_characters.push_back("<lt;");
|
|
|
|
special_characters.push_back(">gt;");
|
|
|
|
special_characters.push_back("\"quot;");
|
|
|
|
special_characters.push_back("'apos;");
|
|
|
|
}
|
|
|
|
XMLParser::~XMLParser() {
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
memdelete_arr(data);
|
|
|
|
}
|