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:
84  XmlReader();
85 
88  explicit XmlReader(InputSource& is);
89 
93 
96  ~XmlReader();
97 
100  XmlResolver* resolver() const;
101 
104  InputSource* input();
105 
112  void addInput(InputSource& in);
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 
163  void reportProcessingInstructions(bool value);
164 
167  void reportCData(bool value);
168 
171  void reportComments(bool value);
172 
175  void reportEntityReferences(bool value);
176 
179  DocTypeDefinition& dtd();
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 
211  Node* advance();
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
InputIterator()
Default Constructor.
Definition: XmlReader.h:228
Node & operator*()
Derefences the iterator.
Definition: XmlReader.h:262
Input source for the XML reader.
Definition: InputSource.h:60
Protects derived classes from being copied.
Definition: NonCopyable.h:54
InputIterator(XmlReader &xis)
Construct iterator to point to current document position.
Definition: XmlReader.h:235
Reads XML as a Stream of XML Nodes.
Definition: XmlReader.h:79
bool operator!=(const InputIterator &it) const
Returns true if iterators point to different nodes.
Definition: XmlReader.h:289
InputIterator & operator=(const InputIterator &it)
Assignment operator.
Definition: XmlReader.h:253
Node & get()
Get current node.
Node & next()
Get next node.
Input iterator to read XML nodes with an XmlReader.
Definition: XmlReader.h:223
The DocTypeDefinition of an XML document.
Definition: DocTypeDefinition.h:57
~InputIterator()
Destructor.
Definition: XmlReader.h:248
Node * operator->()
Derefences the iterator.
Definition: XmlReader.h:267
InputIterator current()
Returns an iterator to the current node.
Definition: XmlReader.h:298
InputIterator end() const
Returns an iterator to the end of the document.
Definition: XmlReader.h:304
Resolves external entities and DTDs.
Definition: XmlResolver.h:53
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
Type type() const
Returns the type of the node.
Definition: Node.h:74
XML document node.
Definition: Node.h:50
InputIterator & operator++()
Increments the iterator position.
Definition: XmlReader.h:272