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 
38 namespace Pt {
39 
40 namespace Xml {
41 
42 class Node;
43 class DocTypeDefinition;
44 class InputSource;
45 class XmlResolver;
46 class InputIterator;
47 
79 class 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 
128  void reset(InputSource& is);
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 
195  InputIterator current();
196 
199  InputIterator end() const;
200 
203  Node& get();
204 
207  Node& next();
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 
235  explicit InputIterator(XmlReader& xis)
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
Core module.
Definition: Allocator.h:33
Resolves external entities and DTDs.
Definition: XmlResolver.h:54
~XmlReader()
Destructor.
bool operator!=(const InputIterator &it) const
Returns true if iterators point to different nodes.
Definition: XmlReader.h:289
InputSource * input()
Returns the current input source or nullptr if none is set.
Node * advance()
Process availabe data from underlying input source.
Reads XML as a Stream of XML Nodes.
Definition: XmlReader.h:80
InputIterator(XmlReader &xis)
Construct iterator to point to current document position.
Definition: XmlReader.h:235
InputIterator()
Default Constructor.
Definition: XmlReader.h:228
XmlReader()
Default Constructor.
XmlResolver * resolver() const
Returns the resolver or nullptr is none was set.
Node & get()
Get current node.
void reportStartDocument(bool value)
Configures the parser to report the start of the document.
InputIterator end() const
Returns an iterator to the end of the document.
Definition: XmlReader.h:304
Node * operator->()
Derefences the iterator.
Definition: XmlReader.h:267
InputIterator & operator++()
Increments the iterator position.
Definition: XmlReader.h:272
void reset(InputSource &is)
Starts parsing with an input source.
Node & next()
Get next node.
Input source for the XML reader.
Definition: InputSource.h:61
XmlReader(XmlResolver &r, InputSource &is)
Construct with resolver and input source.
Input iterator to read XML nodes with an XmlReader.
Definition: XmlReader.h:224
std::size_t depth() const
Returns the XML tree depth.
InputIterator current()
Returns an iterator to the current node.
Definition: XmlReader.h:298
~InputIterator()
Destructor.
Definition: XmlReader.h:248
void reset()
Clears the reader state and input.
Node & operator*()
Derefences the iterator.
Definition: XmlReader.h:262
XML document node.
Definition: Node.h:51
Type type() const
Returns the type of the node.
Definition: Node.h:74
void setMaxSize(std::size_t n)
Sets the max number of characters the parser may allocate.
void setMaxInputDepth(std::size_t n)
Sets the max expansion depth of input sources.
void setChunkSize(std::size_t n)
Sets the max size of a characters block.
std::size_t line() const
Returns the current line of the primary input source.
DocTypeDefinition & dtd()
Returns current DTD of the document.
void addInput(InputSource &in)
Adds an external input source.
InputIterator & operator=(const InputIterator &it)
Assignment operator.
Definition: XmlReader.h:253
bool operator==(const InputIterator &it) const
Returns true if both iterators point at the same node.
Definition: XmlReader.h:284
std::size_t maxSize() const
Returns the number of characters the parser may allocate.
void reportComments(bool value)
Configures the parser to report comments.
const DocTypeDefinition & dtd() const
Returns current DTD of the document.
Protects derived classes from being copied.
Definition: NonCopyable.h:54
The DocTypeDefinition of an XML document.
Definition: DocTypeDefinition.h:58
XmlReader(InputSource &is)
Construct with input source.
void reportEntityReferences(bool value)
Configures the parser to report entity references.
std::size_t usedSize() const
Returns the number of characters the parser has allocated.
InputIterator(const InputIterator &it)
Copy constructor.
Definition: XmlReader.h:242
void reportProcessingInstructions(bool value)
Configures the parser to report processing instructions.
void reportCData(bool value)
Configures the parser to report CDATA sections.
void reportDocType(bool value)
Configures the parser to report DOCTYPEs.