2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* xml_parser.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
#ifndef XML_PARSER_H
|
|
|
|
#define XML_PARSER_H
|
|
|
|
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/file_access.h"
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/string/ustring.h"
|
|
|
|
#include "core/templates/vector.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader.
|
|
|
|
*/
|
|
|
|
|
2021-06-04 18:03:15 +02:00
|
|
|
class XMLParser : public RefCounted {
|
|
|
|
GDCLASS(XMLParser, RefCounted);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
|
|
|
//! Enumeration of all supported source text file formats
|
|
|
|
enum SourceFormat {
|
|
|
|
SOURCE_ASCII,
|
|
|
|
SOURCE_UTF8,
|
|
|
|
SOURCE_UTF16_BE,
|
|
|
|
SOURCE_UTF16_LE,
|
|
|
|
SOURCE_UTF32_BE,
|
|
|
|
SOURCE_UTF32_LE
|
|
|
|
};
|
|
|
|
|
|
|
|
enum NodeType {
|
|
|
|
NODE_NONE,
|
|
|
|
NODE_ELEMENT,
|
|
|
|
NODE_ELEMENT_END,
|
|
|
|
NODE_TEXT,
|
|
|
|
NODE_COMMENT,
|
|
|
|
NODE_CDATA,
|
|
|
|
NODE_UNKNOWN
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-06-14 09:29:58 +02:00
|
|
|
char *data_copy = nullptr;
|
|
|
|
const char *data = nullptr;
|
|
|
|
const char *P = nullptr;
|
2020-05-12 17:01:17 +02:00
|
|
|
uint64_t length = 0;
|
2022-07-05 01:23:04 +02:00
|
|
|
uint64_t current_line = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
String node_name;
|
2020-05-12 17:01:17 +02:00
|
|
|
bool node_empty = false;
|
|
|
|
NodeType node_type = NODE_NONE;
|
|
|
|
uint64_t node_offset = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct Attribute {
|
|
|
|
String name;
|
|
|
|
String value;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Attribute> attributes;
|
|
|
|
|
2022-06-14 09:29:58 +02:00
|
|
|
bool _set_text(const char *start, const char *end);
|
2014-02-10 02:10:30 +01:00
|
|
|
void _parse_closing_xml_element();
|
|
|
|
void _ignore_definition();
|
|
|
|
bool _parse_cdata();
|
|
|
|
void _parse_comment();
|
|
|
|
void _parse_opening_xml_element();
|
|
|
|
void _parse_current_node();
|
|
|
|
|
2022-07-05 01:23:04 +02:00
|
|
|
_FORCE_INLINE_ void next_char() {
|
|
|
|
if (*P == '\n') {
|
|
|
|
current_line++;
|
|
|
|
}
|
|
|
|
P++;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
Error read();
|
|
|
|
NodeType get_node_type();
|
|
|
|
String get_node_name() const;
|
|
|
|
String get_node_data() const;
|
|
|
|
uint64_t get_node_offset() const;
|
|
|
|
int get_attribute_count() const;
|
|
|
|
String get_attribute_name(int p_idx) const;
|
|
|
|
String get_attribute_value(int p_idx) const;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool has_attribute(const String &p_name) const;
|
2022-11-28 10:00:48 +01:00
|
|
|
String get_named_attribute_value(const String &p_name) const;
|
|
|
|
String get_named_attribute_value_safe(const String &p_name) const; // do not print error if doesn't exist
|
2014-02-10 02:10:30 +01:00
|
|
|
bool is_empty() const;
|
|
|
|
int get_current_line() const;
|
|
|
|
|
|
|
|
void skip_section();
|
|
|
|
Error seek(uint64_t p_pos);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error open(const String &p_path);
|
|
|
|
Error open_buffer(const Vector<uint8_t> &p_buffer);
|
2022-06-14 09:29:58 +02:00
|
|
|
Error _open_buffer(const uint8_t *p_buffer, size_t p_size);
|
2014-02-16 01:16:33 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void close();
|
|
|
|
|
|
|
|
~XMLParser();
|
|
|
|
};
|
|
|
|
|
2023-06-15 16:06:22 +02:00
|
|
|
VARIANT_ENUM_CAST(XMLParser::NodeType);
|
|
|
|
|
2020-03-25 11:10:34 +01:00
|
|
|
#endif // XML_PARSER_H
|