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 #include <sstream>
00034 #include <iostream>
00035 #include <fstream>
00036
00037 using std::ostringstream ;
00038 using std::ifstream ;
00039
00040 #include "BESInfo.h"
00041 #include "TheBESKeys.h"
00042 #include "BESHandlerException.h"
00043
00044 #define BES_INFO_FILE_BUFFER_SIZE 4096
00045
00051 BESInfo::BESInfo( )
00052 : _strm( 0 ),
00053 _strm_owned( false ),
00054 _buffered( true )
00055 {
00056 _strm = new ostringstream ;
00057 _strm_owned = true ;
00058 }
00059
00073 BESInfo::BESInfo( const string &key, ostream *strm, bool strm_owned )
00074 : _strm( 0 ),
00075 _strm_owned( false ),
00076 _buffered( true )
00077 {
00078 bool found = false ;
00079 string b = TheBESKeys::TheKeys()->get_key( key, found ) ;
00080 if( b == "true" || b == "True" || b == "TRUE" ||
00081 b == "yes" || b == "Yes" || b == "YES" )
00082 {
00083 _strm = new ostringstream ;
00084 _strm_owned = true ;
00085 _buffered = true ;
00086 if( strm && strm_owned )
00087 delete strm ;
00088 }
00089 else
00090 {
00091 if( !strm )
00092 {
00093 string s = "Informational response not buffered but no stream passed" ;
00094 throw BESHandlerException( s, __FILE__, __LINE__ ) ;
00095 }
00096 _strm = strm ;
00097 _strm_owned = strm_owned ;
00098 _buffered = false ;
00099 }
00100 }
00101
00102 BESInfo::~BESInfo()
00103 {
00104 if( _strm && _strm_owned ) delete _strm ;
00105 }
00106
00107 void
00108 BESInfo::begin_response( const string &response_name )
00109 {
00110 _response_started = true ;
00111 _response_name = response_name ;
00112 }
00113
00114 void
00115 BESInfo::end_response( )
00116 {
00117 _response_started = false ;
00118 if( _tags.size() )
00119 {
00120 string s = "Not all tags were ended in info response" ;
00121 throw BESHandlerException( s, __FILE__, __LINE__ ) ;
00122 }
00123 }
00124
00125 void
00126 BESInfo::begin_tag( const string &tag_name,
00127 map<string,string> *attrs )
00128 {
00129 _tags.push( tag_name ) ;
00130 }
00131
00132 void
00133 BESInfo::end_tag( const string &tag_name )
00134 {
00135 if( _tags.size() == 0 || _tags.top() != tag_name )
00136 {
00137 string s = (string)"tag " + tag_name
00138 + " already ended or not started" ;
00139 throw BESHandlerException( s, __FILE__, __LINE__ ) ;
00140 }
00141 else
00142 {
00143 _tags.pop() ;
00144 }
00145 }
00146
00152 void
00153 BESInfo::add_data( const string &s )
00154 {
00155
00156
00157 (*_strm) << s ;
00158 }
00159
00175 void
00176 BESInfo::add_data_from_file( const string &key, const string &name )
00177 {
00178 bool found = false ;
00179 string file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00180 if( found == false )
00181 {
00182 add_data( name + " file key " + key + " not found, information not available\n" ) ;
00183 }
00184 else
00185 {
00186 ifstream ifs( file.c_str() ) ;
00187 if( !ifs )
00188 {
00189 add_data( name + " file " + file + " not found, information not available\n" ) ;
00190 }
00191 else
00192 {
00193 char line[BES_INFO_FILE_BUFFER_SIZE] ;
00194 while( !ifs.eof() )
00195 {
00196 ifs.getline( line, BES_INFO_FILE_BUFFER_SIZE ) ;
00197 if( !ifs.eof() )
00198 {
00199 add_data( line ) ;
00200 add_data( "\n" ) ;
00201 }
00202 }
00203 ifs.close() ;
00204 }
00205 }
00206 }
00207
00217 void
00218 BESInfo::add_exception( BESException &e )
00219 {
00220 begin_tag( "BESException" ) ;
00221 add_tag( "Type", e.get_context() ) ;
00222 add_tag( "Message", e.get_message() ) ;
00223 begin_tag( "Location" ) ;
00224 add_tag( "File", e.get_file() ) ;
00225 ostringstream sline ;
00226 sline << e.get_line() ;
00227 add_tag( "Line", sline.str() ) ;
00228 end_tag( "Location" ) ;
00229 end_tag( "BESException" ) ;
00230 }
00231
00240 void
00241 BESInfo::print( ostream &strm )
00242 {
00243 if( _buffered )
00244 {
00245 strm << ((ostringstream *)_strm)->str() ;
00246 }
00247 }
00248
00256 void
00257 BESInfo::dump( ostream &strm ) const
00258 {
00259 strm << BESIndent::LMarg << "BESInfo::dump - ("
00260 << (void *)this << ")" << endl ;
00261 BESIndent::Indent() ;
00262 strm << BESIndent::LMarg << "response name: " << _response_name << endl ;
00263 strm << BESIndent::LMarg << "is it buffered? " << _buffered << endl ;
00264 strm << BESIndent::LMarg << "has response been started? " << _response_started << endl ;
00265 if( _tags.size() )
00266 {
00267 strm << BESIndent::LMarg << "tags:" << endl ;
00268 BESIndent::Indent() ;
00269 stack<string> temp_tags = _tags ;
00270 while( !temp_tags.empty() )
00271 {
00272 string tag = temp_tags.top() ;
00273 strm << BESIndent::LMarg << tag << endl ;
00274 temp_tags.pop() ;
00275 }
00276 BESIndent::UnIndent() ;
00277 }
00278 else
00279 {
00280 strm << BESIndent::LMarg << "tags: empty" << endl ;
00281 }
00282 BESIndent::UnIndent() ;
00283 }
00284