xmldeserializer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 by 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 xis_cxxtools_XmlDeserializer_h
29 #define xis_cxxtools_XmlDeserializer_h
30 
31 #include <cxxtools/string.h>
32 #include <cxxtools/deserializer.h>
33 #include "cxxtools/xml/xmlreader.h"
35 #include <sstream>
36 
37 namespace cxxtools
38 {
39 
40 namespace xml
41 {
42 
43  class XmlReader;
44 
51  {
52  public:
58  : _readAttributes(readAttributes),
59  _attributePrefix(attributePrefix)
60  { }
61 
64  explicit XmlDeserializer(XmlReader& reader, bool readAttributes = false, const String& attributePrefix = cxxtools::String());
65 
68  explicit XmlDeserializer(std::istream& is, bool readAttributes = false, const String& attributePrefix = cxxtools::String());
69 
72  void parse(XmlReader& reader);
73 
76  void parse(std::istream& is);
77 
80  void parse(std::basic_istream<Char>& is);
81 
88  { _readAttributes = readAttributes; }
89 
92  bool readAttributes() const
93  { return _readAttributes; }
94 
100  { _attributePrefix = attributePrefix; }
101 
106  const String& attributePrefix() const
107  { return _attributePrefix; }
108 
109  template <typename T>
110  static void toObject(const std::string& str, T& type, bool readAttributes = false)
111  {
112  std::istringstream in(str);
114  d.deserialize(type);
115  }
116 
117  template <typename T>
118  static void toObject(XmlReader& in, T& type, bool readAttributes = false)
119  {
121  d.deserialize(type);
122  }
123 
124  template <typename T>
125  static void toObject(std::istream& in, T& type, bool readAttributes = false)
126  {
128  d.deserialize(type);
129  }
130 
131  private:
132 
134  void beginDocument(XmlReader& reader);
135 
137  void onRootElement(XmlReader& reader);
138 
140  void onStartElement(XmlReader& reader);
141 
143  void onWhitespace(XmlReader& reader);
144 
146  void onContent(XmlReader& reader);
147 
149  void onEndElement(XmlReader& reader);
150 
152  typedef void (XmlDeserializer::*ProcessNode)(XmlReader&);
153 
155  ProcessNode _processNode;
156 
157  size_t _startDepth;
158 
159  bool _readAttributes;
160 
162  String _nodeName;
163 
164  String _nodeId;
165 
166  String _nodeType;
167 
168  String _nodeCategory;
169 
170  Attributes _attributes;
171 
172  String _attributePrefix;
173 
174  SerializationInfo::Category nodeCategory() const;
175 
176  void processAttributes(const Attributes& attributes);
177 
178  };
179 
180 } // namespace xml
181 
182 } // namespace cxxtools
183 
184 #endif