BESHTMLInfo.cc

Go to the documentation of this file.
00001 // BESHTMLInfo.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 "BESHTMLInfo.h"
00042 #include "BESUtil.h"
00043 
00052 BESHTMLInfo::BESHTMLInfo( )
00053     : BESInfo( ),
00054       _header( false ),
00055       _do_indent( true )
00056 {
00057 }
00058 
00067 BESHTMLInfo::BESHTMLInfo( const string &key )
00068     : BESInfo( key ),
00069       _header( false ),
00070       _do_indent( true )
00071 {
00072 }
00073 
00074 BESHTMLInfo::~BESHTMLInfo()
00075 {
00076 }
00077 
00084 void
00085 BESHTMLInfo::begin_response( const string &response_name )
00086 {
00087     BESInfo::begin_response( response_name ) ;
00088     add_data( "<HTML>\n" ) ;
00089     _indent += "    " ;
00090     add_data( "<HEAD>\n" ) ;
00091     _indent += "    " ;
00092     add_data( (string)"<TITLE>" + response_name + "</TITLE>\n" ) ;
00093     if( _indent.length() >= 4 )
00094         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00095     add_data( "</HEAD>\n" ) ;
00096     add_data( "<BODY>\n" ) ;
00097     _indent += "    " ;
00098 }
00099 
00107 void
00108 BESHTMLInfo::end_response( )
00109 {
00110     if( _indent.length() >= 4 )
00111         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00112     add_data( "</BODY>\n" ) ;
00113     if( _indent.length() >= 4 )
00114         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00115     add_data( "</HTML>\n" ) ;
00116 }
00117 
00123 void
00124 BESHTMLInfo::add_tag( const string &tag_name,
00125                       const string &tag_data,
00126                       map<string,string> *attrs )
00127 {
00128     string to_add = tag_name + ": " + tag_data + "<BR />\n" ;
00129     add_data( to_add ) ;
00130     if( attrs )
00131     {
00132         map<string,string>::const_iterator i = attrs->begin() ;
00133         map<string,string>::const_iterator e = attrs->end() ;
00134         for( ; i != e; i++ )
00135         {
00136             string name = (*i).first ;
00137             string val = (*i).second ;
00138             BESInfo::add_data( _indent + "    " + name + ": " + val + "<BR />\n" ) ;
00139         }
00140     }
00141 }
00142 
00147 void
00148 BESHTMLInfo::begin_tag( const string &tag_name,
00149                         map<string,string> *attrs )
00150 {
00151     BESInfo::begin_tag( tag_name ) ;
00152     string to_add = tag_name + "<BR />\n" ;
00153     add_data( to_add ) ;
00154     _indent += "    " ;
00155     if( attrs )
00156     {
00157         map<string,string>::const_iterator i = attrs->begin() ;
00158         map<string,string>::const_iterator e = attrs->end() ;
00159         for( ; i != e; i++ )
00160         {
00161             string name = (*i).first ;
00162             string val = (*i).second ;
00163             BESInfo::add_data( _indent + name + ": " + val + "<BR />\n" ) ;
00164         }
00165     }
00166 }
00167 
00174 void
00175 BESHTMLInfo::end_tag( const string &tag_name )
00176 {
00177     BESInfo::end_tag( tag_name ) ;
00178     if( _indent.length() >= 4 )
00179         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00180 }
00181 
00186 void
00187 BESHTMLInfo::add_space( unsigned long num_spaces )
00188 {
00189     string to_add ;
00190     for( unsigned long i = 0; i < num_spaces; i++ )
00191     {
00192         to_add += "&nbsp;" ;
00193     }
00194     _do_indent = false ;
00195     add_data( to_add ) ;
00196 }
00197 
00202 void
00203 BESHTMLInfo::add_break( unsigned long num_breaks )
00204 {
00205     string to_add ;
00206     for( unsigned long i = 0; i < num_breaks; i++ )
00207     {
00208         to_add += "<BR />" ;
00209     }
00210     to_add += "\n" ;
00211     _do_indent = false ;
00212     add_data( to_add ) ;
00213 }
00214 
00224 void
00225 BESHTMLInfo::add_data( const string &s )
00226 {
00227     if( !_header && !_buffered )
00228     {
00229         BESUtil::set_mime_html( stdout ) ;
00230         _header = true ;
00231     }
00232     if( _do_indent )
00233         BESInfo::add_data( _indent + s ) ;
00234     else
00235         BESInfo::add_data( s ) ;
00236     _do_indent = true ;
00237 }
00238 
00247 void
00248 BESHTMLInfo::add_data_from_file( const string &key, const string &name )
00249 {
00250     string newkey = key + ".HTML" ;
00251     BESInfo::add_data_from_file( newkey, name ) ;
00252 }
00253 
00262 void
00263 BESHTMLInfo::transmit( BESTransmitter *transmitter,
00264                        BESDataHandlerInterface &dhi )
00265 {
00266     transmitter->send_html( *this, dhi ) ;
00267 }
00268 
00276 void
00277 BESHTMLInfo::dump( ostream &strm ) const
00278 {
00279     strm << BESIndent::LMarg << "BESHTMLInfo::dump - ("
00280                              << (void *)this << ")" << endl ;
00281     BESIndent::Indent() ;
00282     strm << BESIndent::LMarg << "has header been added? " << _header << endl ;
00283     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00284     strm << BESIndent::LMarg << "do indent? " << _do_indent << endl ;
00285     BESInfo::dump( strm ) ;
00286     BESIndent::UnIndent() ;
00287 }
00288 
00289 BESInfo *
00290 BESHTMLInfo::BuildHTMLInfo( const string &info_type )
00291 {
00292     return new BESHTMLInfo( ) ;
00293 }
00294 

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