xml_parser.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*************************************************************************/
  2. /* xml_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef XML_PARSER_H
  30. #define XML_PARSER_H
  31. #include "ustring.h"
  32. #include "vector.h"
  33. #include "os/file_access.h"
  34. #include "reference.h"
  35. /*
  36. Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader.
  37. */
  38. class XMLParser : public Reference {
  39. OBJ_TYPE( XMLParser, Reference );
  40. public:
  41. //! Enumeration of all supported source text file formats
  42. enum SourceFormat {
  43. SOURCE_ASCII,
  44. SOURCE_UTF8,
  45. SOURCE_UTF16_BE,
  46. SOURCE_UTF16_LE,
  47. SOURCE_UTF32_BE,
  48. SOURCE_UTF32_LE
  49. };
  50. enum NodeType {
  51. NODE_NONE,
  52. NODE_ELEMENT,
  53. NODE_ELEMENT_END,
  54. NODE_TEXT,
  55. NODE_COMMENT,
  56. NODE_CDATA,
  57. NODE_UNKNOWN
  58. };
  59. private:
  60. char *data;
  61. char *P;
  62. int length;
  63. void unescape(String& p_str);
  64. Vector<String> special_characters;
  65. String node_name;
  66. bool node_empty;
  67. NodeType node_type;
  68. uint64_t node_offset;
  69. struct Attribute {
  70. String name;
  71. String value;
  72. };
  73. Vector<Attribute> attributes;
  74. String _replace_special_characters(const String& origstr);
  75. bool _set_text(char* start, 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. static void _bind_methods();
  83. public:
  84. Error read();
  85. NodeType get_node_type();
  86. String get_node_name() const;
  87. String get_node_data() const;
  88. uint64_t get_node_offset() const;
  89. int get_attribute_count() const;
  90. String get_attribute_name(int p_idx) const;
  91. String get_attribute_value(int p_idx) const;
  92. bool has_attribute(const String& p_name) const;
  93. String get_attribute_value(const String& p_name) const;
  94. String get_attribute_value_safe(const String& p_name) const; // do not print error if doesn't exist
  95. bool is_empty() const;
  96. int get_current_line() const;
  97. void skip_section();
  98. Error seek(uint64_t p_pos);
  99. Error open(const String& p_path);
  100. Error open_buffer(const Vector<uint8_t>& p_buffer);
  101. void close();
  102. XMLParser();
  103. ~XMLParser();
  104. };
  105. #endif