XmlReader.h
1/*
2 * Copyright (C) 2009-2012 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_Xml_XmlReader_h
30#define Pt_Xml_XmlReader_h
31
32#include <Pt/Xml/Api.h>
33#include <Pt/Xml/Node.h>
34#include <Pt/String.h>
35#include <Pt/NonCopyable.h>
36#include <iosfwd>
37
38namespace Pt {
39
40namespace Xml {
41
42class Node;
44class InputSource;
45class XmlResolver;
46class InputIterator;
47
79class PT_XML_API XmlReader : private NonCopyable
80{
81 public:
85
88 explicit XmlReader(InputSource& is);
89
93
97
101
105
113
120 void reset();
121
129
135 void setChunkSize(std::size_t n);
136
139 void setMaxInputDepth(std::size_t n);
140
143 void setMaxSize(std::size_t n);
144
147 std::size_t maxSize() const;
148
151 std::size_t usedSize() const;
152
155 void reportStartDocument(bool value);
156
159 void reportDocType(bool value);
160
164
167 void reportCData(bool value);
168
171 void reportComments(bool value);
172
175 void reportEntityReferences(bool value);
176
180
183 const DocTypeDefinition& dtd() const;
184
187 std::size_t depth() const;
188
191 std::size_t line() const;
192
196
199 InputIterator end() const;
200
204
208
212
213 private:
214 class XmlReaderImpl* _impl;
215
216 public:
217 XmlReaderImpl* impl()
218 { return _impl; }
219};
220
224{
225 public:
229 : _stream(0)
230 , _node(0)
231 { }
232
236 : _stream(&xis)
237 , _node(0)
238 { _node = &_stream->get(); }
239
243 : _stream(it._stream), _node(it._node)
244 { }
245
249 { }
250
254 {
255 _stream = it._stream;
256 _node = it._node;
257 return *this;
258 }
259
262 inline Node& operator*()
263 { return *_node; }
264
267 inline Node* operator->()
268 { return _node; }
269
273 {
274 if(_node->type() == Node::EndDocument)
275 _node = 0;
276 else
277 _node = &_stream->next();
278
279 return *this;
280 }
281
284 inline bool operator==(const InputIterator& it) const
285 { return _node == it._node; }
286
289 inline bool operator!=(const InputIterator& it) const
290 { return _node != it._node; }
291
292 private:
293 XmlReader* _stream;
294 Node* _node;
295};
296
297
299{
300 return InputIterator(*this);
301}
302
303
305{
306 return InputIterator();
307}
308
309} // namespace Xml
310
311} // namespace Pt
312
313#endif // Pt_Xml_XmlReader_h
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
The DocTypeDefinition of an XML document.
Definition DocTypeDefinition.h:58
Input iterator to read XML nodes with an XmlReader.
Definition XmlReader.h:224
InputIterator(const InputIterator &it)
Copy constructor.
Definition XmlReader.h:242
bool operator==(const InputIterator &it) const
Returns true if both iterators point at the same node.
Definition XmlReader.h:284
~InputIterator()
Destructor.
Definition XmlReader.h:248
InputIterator & operator=(const InputIterator &it)
Assignment operator.
Definition XmlReader.h:253
InputIterator(XmlReader &xis)
Construct iterator to point to current document position.
Definition XmlReader.h:235
Node * operator->()
Derefences the iterator.
Definition XmlReader.h:267
Node & operator*()
Derefences the iterator.
Definition XmlReader.h:262
InputIterator & operator++()
Increments the iterator position.
Definition XmlReader.h:272
InputIterator()
Default Constructor.
Definition XmlReader.h:228
bool operator!=(const InputIterator &it) const
Returns true if iterators point to different nodes.
Definition XmlReader.h:289
Input source for the XML reader.
Definition InputSource.h:61
XML document node.
Definition Node.h:51
Reads XML as a Stream of XML Nodes.
Definition XmlReader.h:80
Node * advance()
Process availabe data from underlying input source.
InputIterator end() const
Returns an iterator to the end of the document.
Definition XmlReader.h:304
void reset(InputSource &is)
Starts parsing with an input source.
void setMaxInputDepth(std::size_t n)
Sets the max expansion depth of input sources.
Node & next()
Get next node.
std::size_t maxSize() const
Returns the number of characters the parser may allocate.
void addInput(InputSource &in)
Adds an external input source.
XmlReader(InputSource &is)
Construct with input source.
void reportEntityReferences(bool value)
Configures the parser to report entity references.
void reportProcessingInstructions(bool value)
Configures the parser to report processing instructions.
const DocTypeDefinition & dtd() const
Returns current DTD of the document.
std::size_t line() const
Returns the current line of the primary input source.
InputIterator current()
Returns an iterator to the current node.
Definition XmlReader.h:298
void reportDocType(bool value)
Configures the parser to report DOCTYPEs.
InputSource * input()
Returns the current input source or nullptr if none is set.
void reportComments(bool value)
Configures the parser to report comments.
XmlReader(XmlResolver &r, InputSource &is)
Construct with resolver and input source.
void reportStartDocument(bool value)
Configures the parser to report the start of the document.
std::size_t depth() const
Returns the XML tree depth.
void setMaxSize(std::size_t n)
Sets the max number of characters the parser may allocate.
~XmlReader()
Destructor.
void reset()
Clears the reader state and input.
void setChunkSize(std::size_t n)
Sets the max size of a characters block.
XmlReader()
Default Constructor.
Node & get()
Get current node.
DocTypeDefinition & dtd()
Returns current DTD of the document.
XmlResolver * resolver() const
Returns the resolver or nullptr is none was set.
void reportCData(bool value)
Configures the parser to report CDATA sections.
std::size_t usedSize() const
Returns the number of characters the parser has allocated.
Resolves external entities and DTDs.
Definition XmlResolver.h:54
Read and write XML ducuments.
Core module.
Definition Allocator.h:33