2
0

xml_parser.h 4.3 KB

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