BESXMLInfo.cc

Go to the documentation of this file.
00001 // BESXMLInfo.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 
00039 using std::ostringstream ;
00040 
00041 #include "BESXMLInfo.h"
00042 
00048 BESXMLInfo::BESXMLInfo( )
00049     : BESInfo( ),
00050       _do_indent( true )
00051 {
00052     //_buffered = false ;
00053 }
00054 
00055 BESXMLInfo::~BESXMLInfo()
00056 {
00057 }
00058 
00066 void
00067 BESXMLInfo::begin_response( const string &response_name )
00068 {
00069     BESInfo::begin_response( response_name ) ;
00070     add_data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ) ;
00071     _response_name = response_name ;
00072     add_data( (string)"<" + _response_name + ">\n" ) ;
00073     _indent += "    " ;
00074     add_data( "<response>\n" ) ;
00075     _indent += "    " ;
00076 }
00077 
00085 void
00086 BESXMLInfo::end_response()
00087 {
00088     BESInfo::end_response() ;
00089     if( _indent.length() >= 4 )
00090         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00091     add_data( "</response>\n" ) ;
00092     if( _indent.length() >= 4 )
00093         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00094     add_data( (string)"</" + _response_name + ">\n" ) ;
00095 }
00096 
00103 void
00104 BESXMLInfo::add_tag( const string &tag_name,
00105                      const string &tag_data,
00106                      map<string,string> *attrs )
00107 {
00108     add_data( (string)"<" + tag_name ) ;
00109     if( attrs )
00110     {
00111         map<string,string>::const_iterator i = attrs->begin() ;
00112         map<string,string>::const_iterator e = attrs->end() ;
00113         for( ; i != e; i++ )
00114         {
00115             string name = (*i).first ;
00116             string val = (*i).second ;
00117             _do_indent = false ;
00118             if( val != "" )
00119                 add_data( " " + name + "=" + val ) ;
00120             else
00121                 add_data( " " + name ) ;
00122         }
00123     }
00124     _do_indent = false ;
00125     add_data( ">" + id2xml( tag_data ) + "</" + tag_name + ">\n" ) ;
00126 }
00127 
00133 void
00134 BESXMLInfo::begin_tag( const string &tag_name,
00135                        map<string,string> *attrs )
00136 {
00137     BESInfo::begin_tag( tag_name ) ;
00138     add_data( (string)"<" + tag_name ) ;
00139     if( attrs )
00140     {
00141         map<string,string>::const_iterator i = attrs->begin() ;
00142         map<string,string>::const_iterator e = attrs->end() ;
00143         for( ; i != e; i++ )
00144         {
00145             string name = (*i).first ;
00146             string val = (*i).second ;
00147             _do_indent = false ;
00148             if( val != "" )
00149                 add_data( " " + name + "=" + val ) ;
00150             else
00151                 add_data( " " + name ) ;
00152         }
00153     }
00154     _do_indent = false ;
00155     add_data( ">\n" ) ;
00156     _indent += "    " ;
00157 }
00158 
00165 void
00166 BESXMLInfo::end_tag( const string &tag_name )
00167 {
00168     BESInfo::end_tag( tag_name ) ;
00169     if( _indent.length() >= 4 )
00170         _indent = _indent.substr( 0, _indent.length()-4 ) ;
00171     add_data( (string)"</" + tag_name + ">\n" ) ;
00172 }
00173 
00178 void
00179 BESXMLInfo::add_space( unsigned long num_spaces )
00180 {
00181     string to_add ;
00182     for( unsigned long i = 0; i < num_spaces; i++ )
00183     {
00184         to_add += " " ;
00185     }
00186     _do_indent = false ;
00187     add_data( to_add ) ;
00188 }
00189 
00194 void
00195 BESXMLInfo::add_break( unsigned long num_breaks )
00196 {
00197     string to_add ;
00198     for( unsigned long i = 0; i < num_breaks; i++ )
00199     {
00200         to_add += "\n" ;
00201     }
00202     _do_indent = false ;
00203     add_data( to_add ) ;
00204 }
00205 
00206 void
00207 BESXMLInfo::add_data( const string &s )
00208 {
00209     if( _do_indent )
00210         BESInfo::add_data( _indent + s ) ;
00211     else
00212         BESInfo::add_data( s ) ;
00213     _do_indent = true ;
00214 }
00215 
00216 string
00217 BESXMLInfo::entity( char c )
00218 {
00219     switch( c )
00220     {
00221         case '>': return "&gt;";
00222         case '<': return "&lt;";
00223         case '&': return "&amp;";
00224         case '\'': return "&apos;";
00225         case '\"': return "&quot;";
00226     }
00227 }
00228 
00235 string
00236 BESXMLInfo::id2xml( string in, const string &not_allowed )
00237 {
00238     string::size_type i = 0 ;
00239 
00240     while( ( i = in.find_first_of( not_allowed, i ) ) != string::npos )
00241     {
00242         in.replace( i, 1, entity( in[i] ) ) ;
00243         i++ ;
00244     }
00245 
00246     return in ;
00247 }
00248 
00257 void
00258 BESXMLInfo::add_data_from_file( const string &key, const string &name )
00259 {
00260     string newkey = key + ".XML" ;
00261     BESInfo::add_data_from_file( newkey, name ) ;
00262 }
00263 
00272 void
00273 BESXMLInfo::transmit( BESTransmitter *transmitter,
00274                       BESDataHandlerInterface &dhi )
00275 {
00276     transmitter->send_text( *this, dhi ) ;
00277 }
00278 
00284 void
00285 BESXMLInfo::print( ostream &strm )
00286 {
00287     BESInfo::print( strm ) ;
00288 }
00289 
00297 void
00298 BESXMLInfo::dump( ostream &strm ) const
00299 {
00300     strm << BESIndent::LMarg << "BESXMLInfo::dump - ("
00301                              << (void *)this << ")" << endl ;
00302     BESIndent::Indent() ;
00303     strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
00304     strm << BESIndent::LMarg << "do indent? " << _do_indent << endl ;
00305     BESInfo::dump( strm ) ;
00306     BESIndent::UnIndent() ;
00307 }
00308 
00309 BESInfo *
00310 BESXMLInfo::BuildXMLInfo( const string &info_type )
00311 {
00312     return new BESXMLInfo( ) ;
00313 }
00314 

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