| 1 | /**************************************************************************/ |
| 2 | /* xml_parser.h */ |
| 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 | #ifndef XML_PARSER_H |
| 32 | #define XML_PARSER_H |
| 33 | |
| 34 | #include "core/io/file_access.h" |
| 35 | #include "core/object/ref_counted.h" |
| 36 | #include "core/string/ustring.h" |
| 37 | #include "core/templates/vector.h" |
| 38 | |
| 39 | /* |
| 40 | Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader. |
| 41 | */ |
| 42 | |
| 43 | class XMLParser : public RefCounted { |
| 44 | GDCLASS(XMLParser, RefCounted); |
| 45 | |
| 46 | public: |
| 47 | //! Enumeration of all supported source text file formats |
| 48 | enum SourceFormat { |
| 49 | SOURCE_ASCII, |
| 50 | SOURCE_UTF8, |
| 51 | SOURCE_UTF16_BE, |
| 52 | SOURCE_UTF16_LE, |
| 53 | SOURCE_UTF32_BE, |
| 54 | SOURCE_UTF32_LE |
| 55 | }; |
| 56 | |
| 57 | enum NodeType { |
| 58 | NODE_NONE, |
| 59 | NODE_ELEMENT, |
| 60 | NODE_ELEMENT_END, |
| 61 | NODE_TEXT, |
| 62 | , |
| 63 | NODE_CDATA, |
| 64 | NODE_UNKNOWN |
| 65 | }; |
| 66 | |
| 67 | private: |
| 68 | char *data_copy = nullptr; |
| 69 | const char *data = nullptr; |
| 70 | const char *P = nullptr; |
| 71 | uint64_t length = 0; |
| 72 | uint64_t current_line = 0; |
| 73 | String node_name; |
| 74 | bool node_empty = false; |
| 75 | NodeType node_type = NODE_NONE; |
| 76 | uint64_t node_offset = 0; |
| 77 | |
| 78 | struct Attribute { |
| 79 | String name; |
| 80 | String value; |
| 81 | }; |
| 82 | |
| 83 | Vector<Attribute> attributes; |
| 84 | |
| 85 | bool _set_text(const char *start, const char *end); |
| 86 | void _parse_closing_xml_element(); |
| 87 | void _ignore_definition(); |
| 88 | bool _parse_cdata(); |
| 89 | void (); |
| 90 | void _parse_opening_xml_element(); |
| 91 | void _parse_current_node(); |
| 92 | |
| 93 | _FORCE_INLINE_ void next_char() { |
| 94 | if (*P == '\n') { |
| 95 | current_line++; |
| 96 | } |
| 97 | P++; |
| 98 | } |
| 99 | |
| 100 | static void _bind_methods(); |
| 101 | |
| 102 | public: |
| 103 | Error read(); |
| 104 | NodeType get_node_type(); |
| 105 | String get_node_name() const; |
| 106 | String get_node_data() const; |
| 107 | uint64_t get_node_offset() const; |
| 108 | int get_attribute_count() const; |
| 109 | String get_attribute_name(int p_idx) const; |
| 110 | String get_attribute_value(int p_idx) const; |
| 111 | bool has_attribute(const String &p_name) const; |
| 112 | String get_named_attribute_value(const String &p_name) const; |
| 113 | String get_named_attribute_value_safe(const String &p_name) const; // do not print error if doesn't exist |
| 114 | bool is_empty() const; |
| 115 | int get_current_line() const; |
| 116 | |
| 117 | void skip_section(); |
| 118 | Error seek(uint64_t p_pos); |
| 119 | |
| 120 | Error open(const String &p_path); |
| 121 | Error open_buffer(const Vector<uint8_t> &p_buffer); |
| 122 | Error _open_buffer(const uint8_t *p_buffer, size_t p_size); |
| 123 | |
| 124 | void close(); |
| 125 | |
| 126 | ~XMLParser(); |
| 127 | }; |
| 128 | |
| 129 | VARIANT_ENUM_CAST(XMLParser::NodeType); |
| 130 | |
| 131 | #endif // XML_PARSER_H |
| 132 | |