00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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 }
00171
00172 }
00173
00174 #endif