OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESTextInfo.cc
Go to the documentation of this file.
00001 // BESTextInfo.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 "BESTextInfo.h"
00043 #include "BESUtil.h"
00044 
00054 BESTextInfo::BESTextInfo( bool ishttp )
00055     : BESInfo( ),
00056       _ishttp( ishttp ),
00057       _header( false )
00058 {
00059 }
00060 
00080 BESTextInfo::BESTextInfo( const string &key, ostream *strm,
00081                           bool strm_owned, bool ishttp )
00082     : BESInfo( key, strm, strm_owned ),
00083       _ishttp( ishttp ),
00084       _header( false )
00085 {
00086 }
00087 
00088 BESTextInfo::~BESTextInfo()
00089 {
00090 }
00091 
00099 void
00100 BESTextInfo::begin_response( const string &response_name,
00101                              BESDataHandlerInterface &dhi )
00102 {
00103     BESInfo::begin_response( response_name, dhi ) ;
00104 }
00105 
00112 void
00113 BESTextInfo::add_tag( const string &tag_name,
00114                       const string &tag_data,
00115                       map<string,string> *attrs )
00116 {
00117     add_data( _indent + tag_name + ": " + tag_data + "\n" ) ;
00118     if( attrs )
00119     {
00120         map<string,string>::const_iterator i = attrs->begin() ;
00121         map<string,string>::const_iterator e = attrs->end() ;
00122         for( ; i != e; i++ )
00123         {
00124             string name = (*i).first ;
00125             string val = (*i).second ;
00126             add_data( _indent + "    " + name + ": " + val + "\n" ) ;
00127         }
00128     }
00129 }
00130 
00136 void
00137 BESTextInfo::begin_tag( const string &tag_name,
00138                         map<string,string> *attrs )
00139 {
00140     BESInfo::begin_tag( tag_name ) ;
00141     add_data( _indent + tag_name + "\n" ) ;
00142     _indent += "    " ;
00143     if( attrs )
00144     {
00145         map<string,string>::const_iterator i = attrs->begin() ;
00146         map<string,string>::const_iterator e = attrs->end() ;
00147         for( ; i != e; i++ )
00148         {
00149             string name = (*i).first ;
00150             string val = (*i).second ;
00151             add_data( _indent + name + ": " + val + "\n" ) ;
00152         }
00153     }
00154 }
00155 
00162 void
00163 BESTextInfo::end_tag( const string &tag_name )
00164 {
00165     BESInfo::end_tag( tag_name ) ;
00166     if( _indent.length() >= 4 )
00167         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00168 }
00169 
00174 void
00175 BESTextInfo::add_data( const string & s )
00176 {
00177     if( _ishttp && !_header && !_buffered )
00178     {
00179         BESUtil::set_mime_text( *_strm ) ;
00180         _header = true ;
00181     }
00182     BESInfo::add_data( s ) ;
00183 }
00184 
00189 void
00190 BESTextInfo::add_space( unsigned long num_spaces )
00191 {
00192     string to_add ;
00193     for( unsigned long i = 0; i < num_spaces; i++ )
00194     {
00195         to_add += " " ;
00196     }
00197     add_data( to_add ) ;
00198 }
00199 
00204 void
00205 BESTextInfo::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 += "\n" ;
00211     }
00212     add_data( to_add ) ;
00213 }
00214 
00223 void
00224 BESTextInfo::add_data_from_file( const string &key, const string &name )
00225 {
00226     string newkey = key + ".TXT" ;
00227     BESInfo::add_data_from_file( newkey, name ) ;
00228 }
00229 
00238 void
00239 BESTextInfo::transmit( BESTransmitter *transmitter,
00240                        BESDataHandlerInterface &dhi )
00241 {
00242     transmitter->send_text( *this, dhi ) ;
00243 }
00244 
00252 void
00253 BESTextInfo::dump( ostream &strm ) const
00254 {
00255     strm << BESIndent::LMarg << "BESTextInfo::dump - ("
00256                              << (void *)this << ")" << endl ;
00257     BESIndent::Indent() ;
00258     strm << BESIndent::LMarg << "has header been added? " << _header << endl ;
00259     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00260     strm << BESIndent::LMarg << "is http? " << _ishttp << endl ;
00261     BESInfo::dump( strm ) ;
00262     BESIndent::UnIndent() ;
00263 }
00264 
00265 BESInfo *
00266 BESTextInfo::BuildTextInfo( const string &info_type )
00267 {
00268     return new BESTextInfo( ) ;
00269 }
00270