1 // file : examples/cxx/tree/streaming/serializer.hxx
2 // author : Boris Kolpackov <boris@codesynthesis.com>
3 // copyright : not copyrighted - public domain
4
5 #ifndef SERIALIZER_HXX
6 #define SERIALIZER_HXX
7
8 #include <string>
9 #include <iosfwd>
10 #include <memory> // std::auto_ptr
11
12 #include <xercesc/dom/DOMElement.hpp>
13
14 #include <xsd/cxx/xml/dom/auto-ptr.hxx>
15 #include <xsd/cxx/xml/dom/serialization-header.hxx> // namespace_infomap
16
17 class serializer_impl;
18
19 class serializer
20 {
21 public:
22 typedef xsd::cxx::xml::dom::namespace_infomap<char> namespace_infomap;
23
24 ~serializer ();
25 serializer ();
26
27 // Start the serialization process.
28 //
29 void
30 start (std::ostream& is, const std::string& encoding = "UTF-8");
31
32 // Serialize next object model fragment into an element with the specified
33 // name.
34 //
35 template <typename T>
36 void
37 next (const std::string& name, const T& x);
38
39 // Serialize next object model fragment into an element with the specified
40 // name and namespace declarations.
41 //
42 template <typename T>
43 void
44 next (const std::string& name, const namespace_infomap&, const T& x);
45
46 // Serialize next object model fragment into an element with the specified
47 // namespace and qualified name.
48 //
49 template <typename T>
50 void
51 next (const std::string& ns, const std::string& name, const T& x);
52
53 // Serialize next object model fragment into an element with the specified
54 // namespace and qualified name as well as namespace declarations.
55 //
56 template <typename T>
57 void
58 next (const std::string& ns,
59 const std::string& name,
60 const namespace_infomap&,
61 const T& x);
62
63 // The next_open/close functions are like next() but split into two steps.
64 // next_open() serializes the object model fragment into an element leaving
65 // it open while next_close() closes the element.
66 //
67 template <typename T>
68 void
69 next_open (const std::string& name, const T& x);
70
71 template <typename T>
72 void
73 next_open (const std::string& name, const namespace_infomap&, const T& x);
74
75 template <typename T>
76 void
77 next_open (const std::string& ns, const std::string& name, const T& x);
78
79 template <typename T>
80 void
81 next_open (const std::string& ns,
82 const std::string& name,
83 const namespace_infomap&,
84 const T& x);
85
86 void
87 next_close (const std::string& name);
88
89 private:
90 serializer (const serializer&);
91
92 serializer&
93 operator= (const serializer&);
94
95 private:
96 xercesc::DOMElement*
97 create (const std::string& name, const namespace_infomap&);
98
99 xercesc::DOMElement*
100 create (const std::string& ns,
101 const std::string& name,
102 const namespace_infomap&);
103
104 void
105 serialize (xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement>);
106
107 void
108 serialize_open (xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement>);
109
110 void
111 serialize_close (const std::string& name);
112
113 private:
114 std::auto_ptr<serializer_impl> impl_;
115 };
116
117 template <typename T>
118 inline void serializer::
next(const std::string & name,const T & x)119 next (const std::string& name, const T& x)
120 {
121 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (
122 create (name, namespace_infomap ()));
123 *e << x;
124 serialize (e);
125 }
126
127 template <typename T>
128 inline void serializer::
next(const std::string & name,const namespace_infomap & map,const T & x)129 next (const std::string& name, const namespace_infomap& map, const T& x)
130 {
131 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (name, map));
132 *e << x;
133 serialize (e);
134 }
135
136 template <typename T>
137 inline void serializer::
next(const std::string & ns,const std::string & name,const T & x)138 next (const std::string& ns, const std::string& name, const T& x)
139 {
140 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (
141 create (ns, name, namespace_infomap ()));
142 *e << x;
143 serialize (e);
144 }
145
146 template <typename T>
147 inline void serializer::
next(const std::string & ns,const std::string & name,const namespace_infomap & map,const T & x)148 next (const std::string& ns,
149 const std::string& name,
150 const namespace_infomap& map,
151 const T& x)
152 {
153 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (ns, name, map));
154 *e << x;
155 serialize (e);
156 }
157
158 template <typename T>
159 inline void serializer::
next_open(const std::string & name,const T & x)160 next_open (const std::string& name, const T& x)
161 {
162 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (
163 create (name, namespace_infomap ()));
164 *e << x;
165 serialize_open (e);
166 }
167
168 template <typename T>
169 inline void serializer::
next_open(const std::string & name,const namespace_infomap & map,const T & x)170 next_open (const std::string& name, const namespace_infomap& map, const T& x)
171 {
172 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (name, map));
173 *e << x;
174 serialize_open (e);
175 }
176
177 template <typename T>
178 inline void serializer::
next_open(const std::string & ns,const std::string & name,const T & x)179 next_open (const std::string& ns, const std::string& name, const T& x)
180 {
181 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (
182 create (ns, name, namespace_infomap ()));
183 *e << x;
184 serialize_open (e);
185 }
186
187 template <typename T>
188 inline void serializer::
next_open(const std::string & ns,const std::string & name,const namespace_infomap & map,const T & x)189 next_open (const std::string& ns,
190 const std::string& name,
191 const namespace_infomap& map,
192 const T& x)
193 {
194 xsd::cxx::xml::dom::auto_ptr<xercesc::DOMElement> e (create (ns, name, map));
195 *e << x;
196 serialize_open (e);
197 }
198
199 inline void serializer::
next_close(const std::string & name)200 next_close (const std::string& name)
201 {
202 serialize_close (name);
203 }
204
205 #endif // SERIALIZER_HXX
206