xml.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Tommi Maekitalo
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 CXXTOOLS_XML_XML_H
30 #define CXXTOOLS_XML_XML_H
31 
34 #include <iostream>
35 
36 namespace cxxtools
37 {
38 namespace xml
39 {
57  template <typename ObjectType>
58  class XmlOObject
59  {
60  const ObjectType& _object;
61  std::string _name;
62  bool _beautify;
63  bool _useAttributes;
64 
65  public:
68  XmlOObject(const ObjectType& object, const std::string& name)
69  : _object(object),
70  _name(name),
71  _beautify(false),
72  _useAttributes(true)
73  { }
74 
75  XmlOObject& beautify(bool sw)
76  { _beautify = sw; return *this; }
77 
78  bool beautify() const
79  { return _beautify; }
80 
82  { _useAttributes = sw; return *this; }
83 
84  bool useAttributes() const
85  { return _useAttributes; }
86 
87  const ObjectType& object() const
88  { return _object; }
89 
90  const std::string& name() const
91  { return _name; }
92  };
93 
95  template <typename CharType, typename ObjectType>
96  std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& out, const XmlOObject<ObjectType>& object)
97  {
98  try
99  {
100  XmlSerializer serializer(out);
101 
102  serializer.useIndent(object.beautify());
103  serializer.useEndl(object.beautify());
104  serializer.useAttributes(object.useAttributes());
105 
106  serializer.serialize(object.object(), object.name());
107  }
108  catch (const std::exception&)
109  {
110  out.setstate(std::ios::failbit);
111  throw;
112  }
113 
114  return out;
115  }
116 
120  template <typename ObjectType>
121  XmlOObject<ObjectType> Xml(const ObjectType& object, const std::string& name, bool beautify = false, bool useAttributes = true)
122  {
123  return XmlOObject<ObjectType>(object, name)
124  .beautify(beautify)
125  .useAttributes(useAttributes);
126  }
127 
151  template <typename ObjectType>
153  {
154  ObjectType& _object;
155  bool _readAttributes;
156  String _attributePrefix;
157 
158  public:
159  explicit XmlIObject(ObjectType& object)
160  : _object(object),
161  _readAttributes(false)
162  { }
163 
164  ObjectType& object()
165  { return _object; }
166 
168  { _readAttributes = sw; return *this; }
169 
171  { return _readAttributes; }
172 
174  { _attributePrefix = p; return *this; }
175 
177  { return _attributePrefix; }
178 
179  };
180 
183  template <typename CharType, typename ObjectType>
184  std::basic_istream<CharType>& operator>> (std::basic_istream<CharType>& in, XmlIObject<ObjectType> object)
185  {
186  try
187  {
188  XmlDeserializer deserializer;
189  deserializer.readAttributes(object.readAttributes());
190  deserializer.attributePrefix(object.attributePrefix());
191  deserializer.parse(in);
192  deserializer.deserialize(object.object());
193  }
194  catch (const std::exception&)
195  {
196  in.setstate(std::ios::failbit);
197  throw;
198  }
199  return in;
200  }
201 
204  template <typename ObjectType>
205  XmlIObject<ObjectType> Xml(ObjectType& object)
206  {
207  return XmlIObject<ObjectType>(object);
208  }
209 
210 }
211 }
212 
213 #endif // CXXTOOLS_XML_XML_H
214