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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "CPFMarshaller.hpp"
00040 #include "../rtt-config.h"
00041
00042 namespace RTT {
00043 using namespace detail;
00044 template<class T>
00045 void CPFMarshaller<std::ostream>::doWrite( const Property<T> &v, const std::string& type )
00046 {
00047 *(this->s) <<indent << "<simple ";
00048 if ( !v.getName().empty() )
00049 *(this->s) <<"name=\"" << this->escape( v.getName() ) <<"\" ";
00050 *(this->s) << "type=\""<< type <<"\">";
00051 if ( !v.getDescription().empty() )
00052 *(this->s) << "<description>"<< this->escape( v.getDescription() ) << "</description>";
00053 *(this->s) << "<value>" << v.get() << "</value></simple>\n";
00054 }
00055
00056 void CPFMarshaller<std::ostream>::doWrite( const Property<std::string> &v, const std::string& type )
00057 {
00058 *(this->s) <<indent << "<simple ";
00059 if ( !v.getName().empty() )
00060 *(this->s) <<"name=\"" << this->escape( v.getName() ) <<"\" ";
00061 *(this->s) << "type=\""<< type <<"\">";
00062 if ( !v.getDescription().empty() )
00063 *(this->s) << "<description>"<< this->escape( v.getDescription() ) << "</description>";
00064 *(this->s) << "<value>" << this->escape( v.get() ) << "</value></simple>\n";
00065 }
00066
00067
00068 void CPFMarshaller<std::ostream>::doWrite( const Property<char> &v, const std::string& type )
00069 {
00070 *(this->s) <<indent << "<simple ";
00071 if ( !v.getName().empty() )
00072 *(this->s) <<"name=\"" << this->escape( v.getName() ) <<"\" ";
00073 *(this->s) << "type=\""<< type <<"\">";
00074 if ( !v.getDescription().empty() )
00075 *(this->s) << "<description>"<< this->escape( v.getDescription() ) << "</description>";
00076 if ( v.get() == '\0' )
00077 *(this->s)<< "<value></value></simple>\n";
00078 else {
00079 std::string toescape(1, v.get());
00080 *(this->s) << "<value>" << this->escape( toescape ) << "</value></simple>\n";
00081 }
00082 }
00083
00084
00085 std::string CPFMarshaller<std::ostream>::escape(std::string s)
00086 {
00087 std::string::size_type n=0;
00088
00089 while ((n = s.find("&",n)) != s.npos) {
00090 s.replace(n, 1, std::string("&"));
00091 n += 5;
00092 }
00093
00094 n=0;
00095 while ((n = s.find("<",n)) != s.npos) {
00096 s.replace(n, 1, std::string("<"));
00097 n += 4;
00098 }
00099
00100 n=0;
00101 while ((n = s.find(">",n)) != s.npos) {
00102 s.replace(n, 1, std::string(">"));
00103 n += 4;
00104 }
00105
00106
00107 return s;
00108 }
00109
00110
00111 void CPFMarshaller<std::ostream>::introspect(PropertyBase* pb)
00112 {
00113 PropertyIntrospection::introspect( pb );
00114 }
00115
00116
00117 void CPFMarshaller<std::ostream>::introspect(Property<bool> &v)
00118 {
00119 doWrite( v, "boolean");
00120 }
00121
00122
00123 void CPFMarshaller<std::ostream>::introspect(Property<char> &v)
00124 {
00125 doWrite( v, "char");
00126 }
00127
00128
00129 void CPFMarshaller<std::ostream>::introspect(Property<int> &v)
00130 {
00131 doWrite( v, "long");
00132 }
00133
00134
00135 void CPFMarshaller<std::ostream>::introspect(Property<unsigned int> &v)
00136 {
00137 doWrite( v, "ulong");
00138 }
00139
00140
00141 void CPFMarshaller<std::ostream>::introspect(Property<double> &v)
00142 {
00143 (this->s)->precision(25);
00144 doWrite( v, "double");
00145 }
00146
00147
00148 void CPFMarshaller<std::ostream>::introspect(Property<std::string> &v)
00149 {
00150 doWrite( v, "string");
00151 }
00152
00153
00154 void CPFMarshaller<std::ostream>::introspect(Property<PropertyBag> &b)
00155 {
00156 PropertyBag v = b.get();
00157 *(this->s) <<indent<<"<struct name=\""<<escape(b.getName())<<"\" type=\""<< escape(v.getType())<< "\">\n";
00158 indent +=" ";
00159 if ( !b.getDescription().empty() )
00160 *(this->s) <<indent<<"<description>" <<escape(b.getDescription()) << "</description>\n";
00161
00162 b.value().identify(this);
00163
00164 indent = indent.substr(0, indent.length()-3);
00165 *(this->s) <<indent<<"</struct>\n";
00166 }
00167
00168 CPFMarshaller<std::ostream>::CPFMarshaller(std::ostream &os)
00169 : StreamProcessor<std::ostream>(os), indent(" ")
00170 {
00171 }
00172
00173 CPFMarshaller<std::ostream>::CPFMarshaller(const std::string& filename)
00174 : StreamProcessor<std::ostream>(mfile),
00175 mfile(filename.c_str(), std::fstream::out),
00176 indent(" ")
00177 {
00178 if ( !mfile ) {
00179 s = 0;
00180 log(Error) << "Could not open file for writing: "<<filename <<endlog();
00181 }
00182 }
00183
00184
00185 void CPFMarshaller<std::ostream>::serialize(PropertyBase* v)
00186 {
00187 if (s)
00188 v->identify( this );
00189 }
00190
00191
00192 void CPFMarshaller<std::ostream>::serialize(const PropertyBag &v)
00193 {
00194 if ( !s )
00195 return;
00196 *(this->s) <<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
00197 <<"<!DOCTYPE properties SYSTEM \"cpf.dtd\">\n";
00198 *(this->s) <<"<properties>\n";
00199
00200 v.identify(this);
00201
00202 *(this->s) << "</properties>\n";
00203 }
00204
00205
00206 void CPFMarshaller<std::ostream>::flush()
00207 {
00208 if (s)
00209 this->s->flush();
00210 }
00211 }
00212