00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef XMLWRITER_H
00010 #define XMLWRITER_H
00011
00012 #include <core/cio.hpp>
00013
00014 #include <boost/shared_ptr.hpp>
00015
00016
00017 typedef struct _xmlDoc xmlDoc;
00018 typedef xmlDoc * xmlDocPtr;
00019
00020 typedef struct _xmlTextWriter xmlTextWriter;
00021 typedef xmlTextWriter * xmlTextWriterPtr;
00022
00023 namespace imaging
00024 {
00025 template<class data_t>
00026 class xml_handler;
00027
00090 class XmlWriter
00091 {
00093 class stream_command
00094 {
00095 friend class XmlWriter;
00096 size_t _cmd_id;
00097
00098 protected:
00099 enum commands { ELEMENT = 0, END_ELEMENT, ATTRIBUTE, COMMENT };
00100
00101 std::string _identifier;
00102
00103 public:
00104 stream_command() {}
00105 stream_command(size_t cmd_id, const std::string & identifier) : _cmd_id(cmd_id), _identifier(identifier) {}
00106 };
00107
00108 template<size_t N, class data_t>
00109 void fixed_vector_2_string(const ublas::fixed_vector<data_t, N> & v, std::string & str)
00110 {
00111 std::ostringstream out;
00112
00113 out << "(";
00114 for(std::size_t i = 0; i < N - 1; ++i)
00115 out << v(i) << ",";
00116
00117 out << v(N - 1) << ")";
00118
00119 str = out.str();
00120 }
00121
00122 template <class data_t>
00123 XmlWriter & write_integral_type_value(const data_t & value);
00124
00125 template <class data_t>
00126 XmlWriter & write_integral_type_vector(const std::vector<data_t> & vector);
00127
00130 public:
00132 class element : public stream_command
00133 {
00134 public:
00136 element(const std::string & identifier) : stream_command(ELEMENT, identifier) {}
00137 };
00139 class attribute : public stream_command
00140 {
00141 public:
00142 attribute(const std::string & identifier) : stream_command(ATTRIBUTE, identifier) {}
00143 };
00144
00146 class comment : public stream_command
00147 {
00148 public:
00150 comment(const std::string & identifier) : stream_command(COMMENT, identifier) {}
00151 };
00152
00153 private:
00154 xmlDocPtr _doc;
00155 xmlTextWriterPtr _writer;
00156
00157 bool _attribute_active;
00158 std::string _current_attribute;
00159 std::string _current_attribute_content;
00160
00161 bool _element_active;
00162 std::string _current_element;
00163
00164 void flush_element();
00165
00166 static const char ISO_8859_1 [];
00167
00168
00169 public:
00170
00172 static const stream_command end_element;
00173
00175 XmlWriter(const std::string & root_element, const std::string & style_file_name = "");
00176
00177 ~XmlWriter();
00178
00180 XmlWriter & operator<<(const stream_command & command);
00181
00183 XmlWriter & operator<<(const element & command);
00184
00186 XmlWriter & operator<<(const attribute & command);
00187
00189 XmlWriter & operator<<(const comment & command);
00190
00192 XmlWriter & operator<<(const std::string & string);
00193
00195 XmlWriter & operator<<(const char* str);
00196
00198 XmlWriter & operator<<(float_t value);
00199
00201 XmlWriter & operator<<(size_t value);
00202
00204 template<size_t N, class data_t>
00205 XmlWriter & operator<<(const ublas::fixed_vector<data_t, N> & value)
00206 {
00207 std::string temp;
00208 fixed_vector_2_string(value, temp);
00209
00210 *this << temp;
00211
00212 return *this;
00213 }
00214
00216 template<class data_t>
00217 XmlWriter & operator<<(const data_t & object)
00218 {
00219 xml_handler<data_t> handler;
00220
00221 *this << element(handler.element_name);
00222 handler.write_object(object, *this);
00223 *this << end_element;
00224
00225 return *this;
00226 }
00227
00229 template<class data_t>
00230 XmlWriter & operator<<(const std::vector<data_t> & vector)
00231 {
00232 for(size_t i = 0; i < vector.size(); ++i)
00233 *this << vector[i];
00234
00235 return *this;
00236 }
00237
00239 XmlWriter & operator<<(const std::vector<float_t> & vector);
00240
00242 template<class data_t>
00243 XmlWriter & operator<<(const std::vector< boost::shared_ptr<data_t> > & vector)
00244 {
00245 for(size_t i = 0; i < vector.size(); ++i)
00246 *this << *vector[i];
00247
00248 return *this;
00249 }
00250
00252 void write_file(const std::string & file_name);
00253 };
00254 }
00255
00256 #endif