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-2009 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 Atmospheric 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 #include <iostream>
00039 
00040 using std::ostringstream ;
00041 
00042 #include "BESHTMLInfo.h"
00043 #include "BESUtil.h"
00044 
00050 BESHTMLInfo::BESHTMLInfo( )
00051     : BESInfo( ),
00052       _header( false ),
00053       _do_indent( true )
00054 {
00055 }
00056 
00065 BESHTMLInfo::BESHTMLInfo( const string &key, ostream *strm, bool strm_owned )
00066     : BESInfo( key, strm, strm_owned ),
00067       _header( false ),
00068       _do_indent( true )
00069 {
00070 }
00071 
00072 BESHTMLInfo::~BESHTMLInfo()
00073 {
00074 }
00075 
00083 void
00084 BESHTMLInfo::begin_response( const string &response_name,
00085                              BESDataHandlerInterface &dhi )
00086 {
00087     BESInfo::begin_response( response_name, dhi ) ;
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 
00124 void
00125 BESHTMLInfo::add_tag( const string &tag_name,
00126                       const string &tag_data,
00127                       map<string,string> *attrs )
00128 {
00129     string to_add = tag_name + ": " + tag_data + "<BR />\n" ;
00130     add_data( to_add ) ;
00131     if( attrs )
00132     {
00133         map<string,string>::const_iterator i = attrs->begin() ;
00134         map<string,string>::const_iterator e = attrs->end() ;
00135         for( ; i != e; i++ )
00136         {
00137             string name = (*i).first ;
00138             string val = (*i).second ;
00139             BESInfo::add_data( _indent + "    " + name + ": " + val + "<BR />\n" ) ;
00140         }
00141     }
00142 }
00143 
00149 void
00150 BESHTMLInfo::begin_tag( const string &tag_name,
00151                         map<string,string> *attrs )
00152 {
00153     BESInfo::begin_tag( tag_name ) ;
00154     string to_add = tag_name + "<BR />\n" ;
00155     add_data( to_add ) ;
00156     _indent += "    " ;
00157     if( attrs )
00158     {
00159         map<string,string>::const_iterator i = attrs->begin() ;
00160         map<string,string>::const_iterator e = attrs->end() ;
00161         for( ; i != e; i++ )
00162         {
00163             string name = (*i).first ;
00164             string val = (*i).second ;
00165             BESInfo::add_data( _indent + name + ": " + val + "<BR />\n" ) ;
00166         }
00167     }
00168 }
00169 
00176 void
00177 BESHTMLInfo::end_tag( const string &tag_name )
00178 {
00179     BESInfo::end_tag( tag_name ) ;
00180     if( _indent.length() >= 4 )
00181         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00182 }
00183 
00188 void
00189 BESHTMLInfo::add_space( unsigned long num_spaces )
00190 {
00191     string to_add ;
00192     for( unsigned long i = 0; i < num_spaces; i++ )
00193     {
00194         to_add += "&nbsp;" ;
00195     }
00196     _do_indent = false ;
00197     add_data( to_add ) ;
00198 }
00199 
00204 void
00205 BESHTMLInfo::add_break( unsigned long num_breaks )
00206 {
00207     string to_add ;
00208     for( unsigned long i = 0; i < num_breaks; i++ )
00209     {
00210         to_add += "<BR />" ;
00211     }
00212     to_add += "\n" ;
00213     _do_indent = false ;
00214     add_data( to_add ) ;
00215 }
00216 
00226 void
00227 BESHTMLInfo::add_data( const string &s )
00228 {
00229     if( !_header && !_buffered )
00230     {
00231         BESUtil::set_mime_html( *_strm ) ;
00232         _header = true ;
00233     }
00234     if( _do_indent )
00235         BESInfo::add_data( _indent + s ) ;
00236     else
00237         BESInfo::add_data( s ) ;
00238     _do_indent = true ;
00239 }
00240 
00249 void
00250 BESHTMLInfo::add_data_from_file( const string &key, const string &name )
00251 {
00252     string newkey = key + ".HTML" ;
00253     BESInfo::add_data_from_file( newkey, name ) ;
00254 }
00255 
00264 void
00265 BESHTMLInfo::transmit( BESTransmitter *transmitter,
00266                        BESDataHandlerInterface &dhi )
00267 {
00268     transmitter->send_html( *this, dhi ) ;
00269 }
00270 
00278 void
00279 BESHTMLInfo::dump( ostream &strm ) const
00280 {
00281     strm << BESIndent::LMarg << "BESHTMLInfo::dump - ("
00282                              << (void *)this << ")" << endl ;
00283     BESIndent::Indent() ;
00284     strm << BESIndent::LMarg << "has header been added? " << _header << endl ;
00285     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00286     strm << BESIndent::LMarg << "do indent? " << _do_indent << endl ;
00287     BESInfo::dump( strm ) ;
00288     BESIndent::UnIndent() ;
00289 }
00290 
00291 BESInfo *
00292 BESHTMLInfo::BuildHTMLInfo( const string &info_type )
00293 {
00294     return new BESHTMLInfo( ) ;
00295 }
00296 

Generated on Thu Sep 16 15:20:30 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7