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 #ifdef __GNUG__
00034 #pragma implementation
00035 #endif
00036
00037 #include <sstream>
00038
00039 using std::ostringstream ;
00040
00041 #include "BESXMLInfo.h"
00042
00048 BESXMLInfo::BESXMLInfo( )
00049 : BESInfo( ),
00050 _do_indent( true )
00051 {
00052
00053 }
00054
00055 BESXMLInfo::~BESXMLInfo()
00056 {
00057 }
00058
00066 void
00067 BESXMLInfo::begin_response( const string &response_name )
00068 {
00069 BESInfo::begin_response( response_name ) ;
00070 add_data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ) ;
00071 _response_name = response_name ;
00072 add_data( (string)"<" + _response_name + ">\n" ) ;
00073 _indent += " " ;
00074 add_data( "<response>\n" ) ;
00075 _indent += " " ;
00076 }
00077
00085 void
00086 BESXMLInfo::end_response()
00087 {
00088 BESInfo::end_response() ;
00089 if( _indent.length() >= 4 )
00090 _indent = _indent.substr( 0, _indent.length()-4 ) ;
00091 add_data( "</response>\n" ) ;
00092 if( _indent.length() >= 4 )
00093 _indent = _indent.substr( 0, _indent.length()-4 ) ;
00094 add_data( (string)"</" + _response_name + ">\n" ) ;
00095 }
00096
00103 void
00104 BESXMLInfo::add_tag( const string &tag_name,
00105 const string &tag_data,
00106 map<string,string> *attrs )
00107 {
00108 add_data( (string)"<" + tag_name ) ;
00109 if( attrs )
00110 {
00111 map<string,string>::const_iterator i = attrs->begin() ;
00112 map<string,string>::const_iterator e = attrs->end() ;
00113 for( ; i != e; i++ )
00114 {
00115 string name = (*i).first ;
00116 string val = (*i).second ;
00117 _do_indent = false ;
00118 if( val != "" )
00119 add_data( " " + name + "=" + val ) ;
00120 else
00121 add_data( " " + name ) ;
00122 }
00123 }
00124 _do_indent = false ;
00125 add_data( ">" + id2xml( tag_data ) + "</" + tag_name + ">\n" ) ;
00126 }
00127
00133 void
00134 BESXMLInfo::begin_tag( const string &tag_name,
00135 map<string,string> *attrs )
00136 {
00137 BESInfo::begin_tag( tag_name ) ;
00138 add_data( (string)"<" + tag_name ) ;
00139 if( attrs )
00140 {
00141 map<string,string>::const_iterator i = attrs->begin() ;
00142 map<string,string>::const_iterator e = attrs->end() ;
00143 for( ; i != e; i++ )
00144 {
00145 string name = (*i).first ;
00146 string val = (*i).second ;
00147 _do_indent = false ;
00148 if( val != "" )
00149 add_data( " " + name + "=" + val ) ;
00150 else
00151 add_data( " " + name ) ;
00152 }
00153 }
00154 _do_indent = false ;
00155 add_data( ">\n" ) ;
00156 _indent += " " ;
00157 }
00158
00165 void
00166 BESXMLInfo::end_tag( const string &tag_name )
00167 {
00168 BESInfo::end_tag( tag_name ) ;
00169 if( _indent.length() >= 4 )
00170 _indent = _indent.substr( 0, _indent.length()-4 ) ;
00171 add_data( (string)"</" + tag_name + ">\n" ) ;
00172 }
00173
00178 void
00179 BESXMLInfo::add_space( unsigned long num_spaces )
00180 {
00181 string to_add ;
00182 for( unsigned long i = 0; i < num_spaces; i++ )
00183 {
00184 to_add += " " ;
00185 }
00186 _do_indent = false ;
00187 add_data( to_add ) ;
00188 }
00189
00194 void
00195 BESXMLInfo::add_break( unsigned long num_breaks )
00196 {
00197 string to_add ;
00198 for( unsigned long i = 0; i < num_breaks; i++ )
00199 {
00200 to_add += "\n" ;
00201 }
00202 _do_indent = false ;
00203 add_data( to_add ) ;
00204 }
00205
00206 void
00207 BESXMLInfo::add_data( const string &s )
00208 {
00209 if( _do_indent )
00210 BESInfo::add_data( _indent + s ) ;
00211 else
00212 BESInfo::add_data( s ) ;
00213 _do_indent = true ;
00214 }
00215
00216 string
00217 BESXMLInfo::entity( char c )
00218 {
00219 switch( c )
00220 {
00221 case '>': return ">";
00222 case '<': return "<";
00223 case '&': return "&";
00224 case '\'': return "'";
00225 case '\"': return """;
00226 }
00227 }
00228
00235 string
00236 BESXMLInfo::id2xml( string in, const string ¬_allowed )
00237 {
00238 string::size_type i = 0 ;
00239
00240 while( ( i = in.find_first_of( not_allowed, i ) ) != string::npos )
00241 {
00242 in.replace( i, 1, entity( in[i] ) ) ;
00243 i++ ;
00244 }
00245
00246 return in ;
00247 }
00248
00257 void
00258 BESXMLInfo::add_data_from_file( const string &key, const string &name )
00259 {
00260 string newkey = key + ".XML" ;
00261 BESInfo::add_data_from_file( newkey, name ) ;
00262 }
00263
00272 void
00273 BESXMLInfo::transmit( BESTransmitter *transmitter,
00274 BESDataHandlerInterface &dhi )
00275 {
00276 transmitter->send_text( *this, dhi ) ;
00277 }
00278
00284 void
00285 BESXMLInfo::print( ostream &strm )
00286 {
00287 BESInfo::print( strm ) ;
00288 }
00289
00297 void
00298 BESXMLInfo::dump( ostream &strm ) const
00299 {
00300 strm << BESIndent::LMarg << "BESXMLInfo::dump - ("
00301 << (void *)this << ")" << endl ;
00302 BESIndent::Indent() ;
00303 strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00304 strm << BESIndent::LMarg << "do indent? " << _do_indent << endl ;
00305 BESInfo::dump( strm ) ;
00306 BESIndent::UnIndent() ;
00307 }
00308
00309 BESInfo *
00310 BESXMLInfo::BuildXMLInfo( const string &info_type )
00311 {
00312 return new BESXMLInfo( ) ;
00313 }
00314