xml_parser.h 4.4 KB

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