xml_parser.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*************************************************************************/
  2. /* xml_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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;
  62. char *P;
  63. uint64_t length;
  64. void unescape(String &p_str);
  65. Vector<String> special_characters;
  66. String node_name;
  67. bool node_empty;
  68. NodeType node_type;
  69. uint64_t node_offset;
  70. struct Attribute {
  71. String name;
  72. String value;
  73. };
  74. Vector<Attribute> attributes;
  75. String _replace_special_characters(const String &origstr);
  76. bool _set_text(char *start, char *end);
  77. void _parse_closing_xml_element();
  78. void _ignore_definition();
  79. bool _parse_cdata();
  80. void _parse_comment();
  81. void _parse_opening_xml_element();
  82. void _parse_current_node();
  83. static void _bind_methods();
  84. public:
  85. Error read();
  86. NodeType get_node_type();
  87. String get_node_name() const;
  88. String get_node_data() const;
  89. uint64_t get_node_offset() const;
  90. int get_attribute_count() const;
  91. String get_attribute_name(int p_idx) const;
  92. String get_attribute_value(int p_idx) const;
  93. bool has_attribute(const String &p_name) const;
  94. String get_attribute_value(const String &p_name) const;
  95. String get_attribute_value_safe(const String &p_name) const; // do not print error if doesn't exist
  96. bool is_empty() const;
  97. int get_current_line() const;
  98. void skip_section();
  99. Error seek(uint64_t p_pos);
  100. Error open(const String &p_path);
  101. Error open_buffer(const Vector<uint8_t> &p_buffer);
  102. void close();
  103. XMLParser();
  104. ~XMLParser();
  105. };
  106. #endif