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,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 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 
00078 BESTextInfo::BESTextInfo( const string &key, ostream *strm,
00079                           bool strm_owned, bool ishttp )
00080     : BESInfo( key, strm, strm_owned ),
00081       _ishttp( ishttp ),
00082       _header( false )
00083 {
00084 }
00085 
00086 BESTextInfo::~BESTextInfo()
00087 {
00088 }
00089 
00096 void
00097 BESTextInfo::begin_response( const string &response_name )
00098 {
00099     BESInfo::begin_response( response_name ) ;
00100 }
00101 
00107 void
00108 BESTextInfo::add_tag( const string &tag_name,
00109                       const string &tag_data,
00110                       map<string,string> *attrs )
00111 {
00112     add_data( _indent + tag_name + ": " + tag_data + "\n" ) ;
00113     if( attrs )
00114     {
00115         map<string,string>::const_iterator i = attrs->begin() ;
00116         map<string,string>::const_iterator e = attrs->end() ;
00117         for( ; i != e; i++ )
00118         {
00119             string name = (*i).first ;
00120             string val = (*i).second ;
00121             add_data( _indent + "    " + name + ": " + val + "\n" ) ;
00122         }
00123     }
00124 }
00125 
00130 void
00131 BESTextInfo::begin_tag( const string &tag_name,
00132                         map<string,string> *attrs )
00133 {
00134     BESInfo::begin_tag( tag_name ) ;
00135     add_data( _indent + tag_name + "\n" ) ;
00136     _indent += "    " ;
00137     if( attrs )
00138     {
00139         map<string,string>::const_iterator i = attrs->begin() ;
00140         map<string,string>::const_iterator e = attrs->end() ;
00141         for( ; i != e; i++ )
00142         {
00143             string name = (*i).first ;
00144             string val = (*i).second ;
00145             add_data( _indent + name + ": " + val + "\n" ) ;
00146         }
00147     }
00148 }
00149 
00156 void
00157 BESTextInfo::end_tag( const string &tag_name )
00158 {
00159     BESInfo::end_tag( tag_name ) ;
00160     if( _indent.length() >= 4 )
00161         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00162 }
00163 
00168 void
00169 BESTextInfo::add_data( const string & s )
00170 {
00171     if( _ishttp && !_header && !_buffered )
00172     {
00173         BESUtil::set_mime_text( *_strm ) ;
00174         _header = true ;
00175     }
00176     BESInfo::add_data( s ) ;
00177 }
00178 
00183 void
00184 BESTextInfo::add_space( unsigned long num_spaces )
00185 {
00186     string to_add ;
00187     for( unsigned long i = 0; i < num_spaces; i++ )
00188     {
00189         to_add += " " ;
00190     }
00191     add_data( to_add ) ;
00192 }
00193 
00198 void
00199 BESTextInfo::add_break( unsigned long num_breaks )
00200 {
00201     string to_add ;
00202     for( unsigned long i = 0; i < num_breaks; i++ )
00203     {
00204         to_add += "\n" ;
00205     }
00206     add_data( to_add ) ;
00207 }
00208 
00217 void
00218 BESTextInfo::add_data_from_file( const string &key, const string &name )
00219 {
00220     string newkey = key + ".TXT" ;
00221     BESInfo::add_data_from_file( newkey, name ) ;
00222 }
00223 
00232 void
00233 BESTextInfo::transmit( BESTransmitter *transmitter,
00234                        BESDataHandlerInterface &dhi )
00235 {
00236     transmitter->send_text( *this, dhi ) ;
00237 }
00238 
00246 void
00247 BESTextInfo::dump( ostream &strm ) const
00248 {
00249     strm << BESIndent::LMarg << "BESTextInfo::dump - ("
00250                              << (void *)this << ")" << endl ;
00251     BESIndent::Indent() ;
00252     strm << BESIndent::LMarg << "has header been added? " << _header << endl ;
00253     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00254     strm << BESIndent::LMarg << "is http? " << _ishttp << endl ;
00255     BESInfo::dump( strm ) ;
00256     BESIndent::UnIndent() ;
00257 }
00258 
00259 BESInfo *
00260 BESTextInfo::BuildTextInfo( const string &info_type )
00261 {
00262     return new BESTextInfo( ) ;
00263 }
00264 

Generated on Wed Jan 2 06:01:19 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4