BESXMLInfo.cc

Go to the documentation of this file.
00001 // BESXMLInfo.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmostpheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #ifdef __GNUG__
00034 #pragma implementation
00035 #endif
00036 
00037 #include <sstream>
00038 
00039 using std::ostringstream ;
00040 
00041 #include "BESXMLInfo.h"
00042 
00051 BESXMLInfo::BESXMLInfo( )
00052     : BESInfo( ),
00053       _do_indent( true )
00054 {
00055     //_buffered = false ;
00056 }
00057 
00058 BESXMLInfo::~BESXMLInfo()
00059 {
00060 }
00061 
00069 void
00070 BESXMLInfo::begin_response( const string &response_name )
00071 {
00072     BESInfo::begin_response( response_name ) ;
00073     add_data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ) ;
00074     _response_name = response_name ;
00075     add_data( (string)"<" + _response_name + ">\n" ) ;
00076     _indent += "    " ;
00077     add_data( "<response>\n" ) ;
00078     _indent += "    " ;
00079 }
00080 
00088 void
00089 BESXMLInfo::end_response()
00090 {
00091     BESInfo::end_response() ;
00092     if( _indent.length() >= 4 )
00093         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00094     add_data( "</response>\n" ) ;
00095     if( _indent.length() >= 4 )
00096         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00097     add_data( (string)"</" + _response_name + ">\n" ) ;
00098 }
00099 
00105 void
00106 BESXMLInfo::add_tag( const string &tag_name,
00107                      const string &tag_data,
00108                      map<string,string> *attrs )
00109 {
00110     add_data( (string)"<" + tag_name ) ;
00111     if( attrs )
00112     {
00113         map<string,string>::const_iterator i = attrs->begin() ;
00114         map<string,string>::const_iterator e = attrs->end() ;
00115         for( ; i != e; i++ )
00116         {
00117             string name = (*i).first ;
00118             string val = (*i).second ;
00119             _do_indent = false ;
00120             if( val != "" )
00121                 add_data( " " + name + "=" + val ) ;
00122             else
00123                 add_data( " " + name ) ;
00124         }
00125     }
00126     _do_indent = false ;
00127     add_data( ">" + tag_data + "</" + tag_name + ">\n" ) ;
00128 }
00129 
00134 void
00135 BESXMLInfo::begin_tag( const string &tag_name,
00136                        map<string,string> *attrs )
00137 {
00138     BESInfo::begin_tag( tag_name ) ;
00139     add_data( (string)"<" + tag_name ) ;
00140     if( attrs )
00141     {
00142         map<string,string>::const_iterator i = attrs->begin() ;
00143         map<string,string>::const_iterator e = attrs->end() ;
00144         for( ; i != e; i++ )
00145         {
00146             string name = (*i).first ;
00147             string val = (*i).second ;
00148             _do_indent = false ;
00149             if( val != "" )
00150                 add_data( " " + name + "=" + val ) ;
00151             else
00152                 add_data( " " + name ) ;
00153         }
00154     }
00155     _do_indent = false ;
00156     add_data( ">\n" ) ;
00157     _indent += "    " ;
00158 }
00159 
00166 void
00167 BESXMLInfo::end_tag( const string &tag_name )
00168 {
00169     BESInfo::end_tag( tag_name ) ;
00170     if( _indent.length() >= 4 )
00171         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00172     add_data( (string)"</" + tag_name + ">\n" ) ;
00173 }
00174 
00179 void
00180 BESXMLInfo::add_space( unsigned long num_spaces )
00181 {
00182     string to_add ;
00183     for( unsigned long i = 0; i < num_spaces; i++ )
00184     {
00185         to_add += " " ;
00186     }
00187     _do_indent = false ;
00188     add_data( to_add ) ;
00189 }
00190 
00195 void
00196 BESXMLInfo::add_break( unsigned long num_breaks )
00197 {
00198     string to_add ;
00199     for( unsigned long i = 0; i < num_breaks; i++ )
00200     {
00201         to_add += "\n" ;
00202     }
00203     _do_indent = false ;
00204     add_data( to_add ) ;
00205 }
00206 
00207 void
00208 BESXMLInfo::add_data( const string &s )
00209 {
00210     if( _do_indent )
00211         BESInfo::add_data( _indent + s ) ;
00212     else
00213         BESInfo::add_data( s ) ;
00214     _do_indent = true ;
00215 }
00216 
00225 void
00226 BESXMLInfo::add_data_from_file( const string &key, const string &name )
00227 {
00228     string newkey = key + ".XML" ;
00229     BESInfo::add_data_from_file( newkey, name ) ;
00230 }
00231 
00240 void
00241 BESXMLInfo::transmit( BESTransmitter *transmitter,
00242                       BESDataHandlerInterface &dhi )
00243 {
00244     transmitter->send_text( *this, dhi ) ;
00245 }
00246 
00252 void
00253 BESXMLInfo::print( FILE *out )
00254 {
00255     BESInfo::print( out ) ;
00256 }
00257 
00265 void
00266 BESXMLInfo::dump( ostream &strm ) const
00267 {
00268     strm << BESIndent::LMarg << "BESXMLInfo::dump - ("
00269                              << (void *)this << ")" << endl ;
00270     BESIndent::Indent() ;
00271     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00272     strm << BESIndent::LMarg << "do indent? " << _do_indent << endl ;
00273     BESInfo::dump( strm ) ;
00274     BESIndent::UnIndent() ;
00275 }
00276 
00277 BESInfo *
00278 BESXMLInfo::BuildXMLInfo( const string &info_type )
00279 {
00280     return new BESXMLInfo( ) ;
00281 }
00282 

Generated on Wed Aug 29 03:24:04 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2