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 
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 
00098 void
00099 BESTextInfo::begin_response( const string &response_name )
00100 {
00101     BESInfo::begin_response( response_name ) ;
00102 }
00103 
00110 void
00111 BESTextInfo::add_tag( const string &tag_name,
00112                       const string &tag_data,
00113                       map<string,string> *attrs )
00114 {
00115     add_data( _indent + tag_name + ": " + tag_data + "\n" ) ;
00116     if( attrs )
00117     {
00118         map<string,string>::const_iterator i = attrs->begin() ;
00119         map<string,string>::const_iterator e = attrs->end() ;
00120         for( ; i != e; i++ )
00121         {
00122             string name = (*i).first ;
00123             string val = (*i).second ;
00124             add_data( _indent + "    " + name + ": " + val + "\n" ) ;
00125         }
00126     }
00127 }
00128 
00134 void
00135 BESTextInfo::begin_tag( const string &tag_name,
00136                         map<string,string> *attrs )
00137 {
00138     BESInfo::begin_tag( tag_name ) ;
00139     add_data( _indent + tag_name + "\n" ) ;
00140     _indent += "    " ;
00141     if( attrs )
00142     {
00143         map<string,string>::const_iterator i = attrs->begin() ;
00144         map<string,string>::const_iterator e = attrs->end() ;
00145         for( ; i != e; i++ )
00146         {
00147             string name = (*i).first ;
00148             string val = (*i).second ;
00149             add_data( _indent + name + ": " + val + "\n" ) ;
00150         }
00151     }
00152 }
00153 
00160 void
00161 BESTextInfo::end_tag( const string &tag_name )
00162 {
00163     BESInfo::end_tag( tag_name ) ;
00164     if( _indent.length() >= 4 )
00165         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00166 }
00167 
00172 void
00173 BESTextInfo::add_data( const string & s )
00174 {
00175     if( _ishttp && !_header && !_buffered )
00176     {
00177         BESUtil::set_mime_text( *_strm ) ;
00178         _header = true ;
00179     }
00180     BESInfo::add_data( s ) ;
00181 }
00182 
00187 void
00188 BESTextInfo::add_space( unsigned long num_spaces )
00189 {
00190     string to_add ;
00191     for( unsigned long i = 0; i < num_spaces; i++ )
00192     {
00193         to_add += " " ;
00194     }
00195     add_data( to_add ) ;
00196 }
00197 
00202 void
00203 BESTextInfo::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 += "\n" ;
00209     }
00210     add_data( to_add ) ;
00211 }
00212 
00221 void
00222 BESTextInfo::add_data_from_file( const string &key, const string &name )
00223 {
00224     string newkey = key + ".TXT" ;
00225     BESInfo::add_data_from_file( newkey, name ) ;
00226 }
00227 
00236 void
00237 BESTextInfo::transmit( BESTransmitter *transmitter,
00238                        BESDataHandlerInterface &dhi )
00239 {
00240     transmitter->send_text( *this, dhi ) ;
00241 }
00242 
00250 void
00251 BESTextInfo::dump( ostream &strm ) const
00252 {
00253     strm << BESIndent::LMarg << "BESTextInfo::dump - ("
00254                              << (void *)this << ")" << endl ;
00255     BESIndent::Indent() ;
00256     strm << BESIndent::LMarg << "has header been added? " << _header << endl ;
00257     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00258     strm << BESIndent::LMarg << "is http? " << _ishttp << endl ;
00259     BESInfo::dump( strm ) ;
00260     BESIndent::UnIndent() ;
00261 }
00262 
00263 BESInfo *
00264 BESTextInfo::BuildTextInfo( const string &info_type )
00265 {
00266     return new BESTextInfo( ) ;
00267 }
00268 

Generated on Tue Mar 4 23:13:36 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.1