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