xmlreader.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 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 #ifndef cxxtools_Xml_XmlReader_h
29 #define cxxtools_Xml_XmlReader_h
30 
31 #include <cxxtools/string.h>
32 #include <cxxtools/textstream.h>
33 #include <cxxtools/xml/api.h>
34 #include <cxxtools/xml/node.h>
36 #include <iosfwd>
37 
38 namespace cxxtools {
39 
40 namespace xml {
41 
42  class Node;
43  class StartElement;
44  class EntityResolver;
45 
82 {
83  public:
84  class Iterator
85  {
86  public:
88  : _stream(0)
89  , _node(0)
90  { }
91 
93  : _stream(&xis)
94  , _node(0)
95  { _node = &_stream->get(); }
96 
97  Iterator(const Iterator& it)
98  : _stream(it._stream), _node(it._node)
99  { }
100 
102  { }
103 
104  Iterator& operator=(const Iterator& it)
105  {
106  _stream = it._stream;
107  _node = it._node;
108  return *this;
109  }
110 
111  inline const Node& operator*() const
112  { return *_node; }
113 
114  inline const Node* operator->() const
115  { return _node; }
116 
117  Iterator& operator++()
118  {
119  if(_node->type() == Node::EndDocument)
120  _node = 0;
121  else
122  _node = &_stream->next();
123 
124  return *this;
125  }
126 
127  inline bool operator==(const Iterator& it) const
128  { return _node == it._node; }
129 
130  inline bool operator!=(const Iterator& it) const
131  { return _node != it._node; }
132 
133  private:
134  XmlReader* _stream;
135  const Node* _node;
136  };
137 
138  public:
139  /* TODO: Consider the following processing flags:
140  - ReportProcessingInstructions
141  - IgnoreWhitespace
142  - ReportComments
143  - ReportDocumentStart
144  */
145  XmlReader(std::istream& is, int flags = 0);
146 
147  XmlReader(std::basic_istream<Char>& is, int flags = 0);
148 
149  ~XmlReader();
150 
151  void reset(std::basic_istream<Char>& is, int flags = 0);
152 
153  void reset(std::istream& is, int flags = 0);
154 
155  const cxxtools::String& documentVersion() const;
156 
157  const cxxtools::String& documentEncoding() const;
158 
159  bool standaloneDocument() const;
160 
161  EntityResolver& entityResolver();
162 
163  const EntityResolver& entityResolver() const;
164 
165  size_t depth() const;
166 
167  Iterator current()
168  {
169  return Iterator(*this);
170  }
171 
172  Iterator end() const
173  {
174  return Iterator();
175  }
176 
178  const Node& get();
179 
181  const Node& next();
182 
183  bool advance();
184 
185  const StartElement& nextElement();
186 
187  const Node& nextTag();
188 
189  std::size_t line() const;
190 
191  private:
192  class XmlReaderImpl* _impl;
193 };
194 
195 }
196 
197 }
198 
199 #endif