Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/xml/xmlserializer.h

00001 /*
00002  * Copyright (C) 2008 by Marc Boris Duerner
00003  * 
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  * 
00009  * As a special exception, you may use this file as part of a free
00010  * software library without restriction. Specifically, if other files
00011  * instantiate templates or use macros or inline functions from this
00012  * file, or you compile this file and link it with other files to
00013  * produce an executable, this file does not by itself cause the
00014  * resulting executable to be covered by the GNU General Public
00015  * License. This exception does not however invalidate any other
00016  * reasons why the executable file might be covered by the GNU Library
00017  * General Public License.
00018  * 
00019  * This library is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00022  * Lesser General Public License for more details.
00023  * 
00024  * You should have received a copy of the GNU Lesser General Public
00025  * License along with this library; if not, write to the Free Software
00026  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00027  */
00028 #ifndef cxxtools_Xml_XmlSerializer_h
00029 #define cxxtools_Xml_XmlSerializer_h
00030 
00031 #include <cxxtools/xml/xmlformatter.h>
00032 #include <cxxtools/decomposer.h>
00033 #include <sstream>
00034 
00035 namespace cxxtools
00036 {
00037 
00038 namespace xml
00039 {
00040 
00046 class XmlSerializer
00047 {
00048     public:
00055         XmlSerializer();
00056 
00062         XmlSerializer(std::ostream& os);
00063 
00070         XmlSerializer(cxxtools::xml::XmlWriter* writer);
00071 
00073         ~XmlSerializer();
00074 
00085         void attach(std::ostream& os);
00086 
00099         void attach(cxxtools::xml::XmlWriter& writer);
00100 
00106         void detach();
00107 
00108         void useXmlDeclaration(bool sw)
00109         { _formatter.useXmlDeclaration(sw); }
00110 
00111         bool useXmlDeclaration() const
00112         { return _formatter.useXmlDeclaration(); }
00113 
00114         void useIndent(bool sw)
00115         { _formatter.useIndent(sw); }
00116 
00117         bool useIndent() const
00118         { return _formatter.useIndent(); }
00119 
00120         void useEndl(bool sw)
00121         { _formatter.useEndl(sw); }
00122 
00123         bool useEndl() const
00124         { return _formatter.useEndl(); }
00125 
00126         void useAttributes(bool sw)
00127         { _formatter.useAttributes(sw); }
00128 
00129         bool useAttributes() const
00130         { return _formatter.useAttributes(); }
00131 
00139         template <typename T>
00140         void serialize(const T& type, const std::string& name)
00141         {
00142             Decomposer<T> decomposer;
00143             decomposer.begin(type);
00144             decomposer.setName(name);
00145             decomposer.format(_formatter);
00146             _formatter.finish();
00147             _formatter.flush();
00148         }
00149 
00150         void finish()
00151         {
00152         }
00153 
00154         template <typename T>
00155         static std::string toString(const T& type, const std::string& name, bool beautify = false)
00156         {
00157           std::ostringstream os;
00158           XmlWriter writer(os, beautify ? XmlWriter::UseXmlDeclaration | XmlWriter::UseIndent | XmlWriter::UseEndl
00159                                         : XmlWriter::UseXmlDeclaration);
00160           XmlSerializer s(&writer);
00161           s.serialize(type, name);
00162           s.finish();
00163           return os.str();
00164         }
00165 
00166     private:
00167         XmlFormatter _formatter;
00168 };
00169 
00170 } // namespace xml
00171 
00172 } // namespace cxxtools
00173 
00174 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6