2 * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3 * are covered by the GNU Lesser General Public License, which should be
4 * included with libxml++ as the file COPYING.
7 #ifndef __LIBXMLPP_XMLREADER_H
8 #define __LIBXMLPP_XMLREADER_H
10 #include <libxml++/noncopyable.h>
11 #include <libxml++/nodes/node.h>
13 #include <glibmm/ustring.h>
19 struct _xmlTextReader;
25 /** A TextReader-style XML parser.
26 * A reader that provides fast, non-cached, forward-only access to XML data,
27 * in the style of .Net's <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx">XmlTextReader</a> class.
29 class TextReader: public NonCopyable
37 DocumentFragment = 11,
46 ProcessingInstruction = 7,
47 SignificantWhitespace = 14,
71 typedef unsigned int size_type;
75 * Wraps a TextReader object from an underlying libxml object. The TextReader
76 * takes ownership of cobj.
77 * @param cobj The underlying libxml xmlTextReader object.
79 TextReader(struct _xmlTextReader* cobj);
82 * Creates a new TextReader object to parse a file or URI.
83 * @param URI The URI to read.
84 * @throws xmlpp::internal_error If an xmlTextReader object cannot be created.
86 TextReader(const Glib::ustring& URI);
89 * Creates a new TextReader object which parses in memory data.
90 * @param data The data to parse.
91 * @param size The number of bytes in data.
92 * @param uri The base URI to use for the document.
93 * @throws xmlpp::internal_error If an xmlTextReader object cannot be created.
95 TextReader(const unsigned char* data, size_type size, const Glib::ustring& uri = Glib::ustring());
97 ~TextReader() override;
99 /** Moves the position of the current instance to the next node in the stream, exposing its properties.
100 * @return true if the node was read successfully, false if there are no more nodes to read.
101 * @throws xmlpp::parse_error
102 * @throws xmlpp::validity_error
106 /** Reads the contents of the current node, including child nodes and markup.
107 * @return A Glib::ustring containing the XML content, or an empty Glib::ustring if the current node is neither an element nor attribute, or has no child nodes.
108 * @throws xmlpp::parse_error
109 * @throws xmlpp::validity_error
111 Glib::ustring read_inner_xml();
113 /** Reads the current node and its contents, including child nodes and markup.
114 * @return A Glib::ustring containing the XML content, or an empty Glib::ustring if the current node is neither an element nor attribute.
115 * @throws xmlpp::parse_error
116 * @throws xmlpp::validity_error
118 Glib::ustring read_outer_xml();
120 /** Reads the contents of an element or a text node as a string.
121 * @return A Glib::ustring containing the contents of the Element or Text node, or an empty Glib::ustring if the reader is positioned on any other type of node.
122 * @throws xmlpp::parse_error
123 * @throws xmlpp::validity_error
125 Glib::ustring read_string();
127 /** Parses an attribute value into one or more Text and EntityReference nodes.
128 * @return A bool where true indicates the attribute value was parsed, and false indicates the reader was not positioned on an attribute node or all the attribute values have been read.
129 * @throws xmlpp::parse_error
130 * @throws xmlpp::validity_error
132 bool read_attribute_value();
134 /** Gets the number of attributes on the current node.
135 * @return The number of attributes on the current node, or zero if the current node
136 * does not support attributes, or -1 in case of error.
137 * @throws xmlpp::parse_error
138 * @throws xmlpp::validity_error
140 int get_attribute_count() const;
142 /** Gets the base Uniform Resource Identifier (URI) of the current node.
143 * @return The base URI of the current node or an empty Glib::ustring if not available.
145 Glib::ustring get_base_uri() const;
147 /** Gets the depth of the current node in the XML document.
148 * @return The depth of the current node in the XML document, or -1 in case of error.
150 int get_depth() const;
152 /** Gets a value indicating whether the current node has any attributes.
153 * @return true if the current has attributes, false otherwise.
155 bool has_attributes() const;
157 /** Whether the node can have a text value.
158 * @return true if the current node can have an associated text value, false otherwise.
160 bool has_value() const;
162 /** Whether an Attribute node was generated from the default value defined in the DTD or schema.
163 * @return true if defaulted, false otherwise.
165 bool is_default() const;
167 /** Check if the current node is empty
168 * @return true if empty, false otherwise.
170 bool is_empty_element() const;
172 Glib::ustring get_local_name() const;
173 Glib::ustring get_name() const;
174 Glib::ustring get_namespace_uri() const;
176 /** Get the node type of the current node.
177 * @returns The xmlpp::xmlNodeType of the current node, or -1 in case of error.
179 xmlNodeType get_node_type() const;
181 /** Get the namespace prefix associated with the current node.
182 * @returns The namespace prefix, or an empty string if not available.
184 Glib::ustring get_prefix() const;
186 /** Get the quotation mark character used to enclose the value of an attribute.
187 * @returns Returns " or ' and -1 in case of error.
189 char get_quote_char() const;
191 Glib::ustring get_value() const;
192 Glib::ustring get_xml_lang() const;
194 xmlReadState get_read_state() const;
198 Glib::ustring get_attribute(int number) const;
199 Glib::ustring get_attribute(const Glib::ustring& name) const;
200 Glib::ustring get_attribute(const Glib::ustring& local_name, const Glib::ustring& ns_uri) const;
202 // TODO InputBuffer GetRemainder;
204 Glib::ustring lookup_namespace(const Glib::ustring& prefix) const;
206 bool move_to_attribute(int number);
207 bool move_to_attribute(const Glib::ustring& name);
208 bool move_to_attribute(const Glib::ustring& local_name, const Glib::ustring& ns_uri);
209 bool move_to_first_attribute();
210 bool move_to_next_attribute();
211 bool move_to_element();
213 bool get_normalization() const;
214 void set_normalization(bool value);
216 bool get_parser_property(ParserProperties property) const;
217 void set_parser_property(ParserProperties property, bool value);
219 /** Get a pointer to the current node.
220 * @warning This is dangerous because the underlying node may be destroyed on the next read.
221 * The C++ wrapper is not deleted. Using this method causes memory leaks,
222 * unless you call xmlpp::Node::free_wrappers(), which is not intended to be
223 * called by the application.
224 * @returns A pointer to the current node, or <tt>nullptr</tt> in case of error.
226 Node* get_current_node();
228 /** Get a pointer to the current node.
229 * @warning See the non-const get_current_node().
230 * @returns A pointer to the current node, or <tt>nullptr</tt> in case of error.
232 const Node* get_current_node() const;
234 // Document* CurrentDocument();
236 /** Expand the current node.
237 * Reads the contents of the current node and the full subtree. It then makes
238 * the subtree available until the next call to read() or next().
239 * @warning The C++ wrappers are not deleted. Using this method causes memory leaks,
240 * unless you call xmlpp::Node::free_wrappers(), which is not intended to be
241 * called by the application.
242 * @returns A pointer to the current node, or <tt>nullptr</tt> in case of error.
243 * @throws xmlpp::parse_error
244 * @throws xmlpp::validity_error
249 bool is_valid() const;
252 class PropertyReader;
253 friend class PropertyReader;
255 void setup_exceptions();
256 static void on_libxml_error(void * arg, const char *msg, int severity,
258 void check_for_exceptions() const;
260 std::unique_ptr<PropertyReader> propertyreader;
261 _xmlTextReader* impl_;
263 Glib::ustring error_;