xml_parser.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/os/file_access.h"
  33. #include "core/reference.h"
  34. #include "core/ustring.h"
  35. #include "core/vector.h"
  36. /*
  37. Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader.
  38. */
  39. class XMLParser : public Reference {
  40. GDCLASS(XMLParser, Reference);
  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 = nullptr;
  62. char *P = nullptr;
  63. uint64_t length = 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(char *start, 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. static void _bind_methods();
  81. public:
  82. Error read();
  83. NodeType get_node_type();
  84. String get_node_name() const;
  85. String get_node_data() const;
  86. uint64_t get_node_offset() const;
  87. int get_attribute_count() const;
  88. String get_attribute_name(int p_idx) const;
  89. String get_attribute_value(int p_idx) const;
  90. bool has_attribute(const String &p_name) const;
  91. String get_attribute_value(const String &p_name) const;
  92. String get_attribute_value_safe(const String &p_name) const; // do not print error if doesn't exist
  93. bool is_empty() const;
  94. int get_current_line() const;
  95. void skip_section();
  96. Error seek(uint64_t p_pos);
  97. Error open(const String &p_path);
  98. Error open_buffer(const Vector<uint8_t> &p_buffer);
  99. void close();
  100. XMLParser();
  101. ~XMLParser();
  102. };
  103. #endif // XML_PARSER_H