bes  Updated for version 3.17.0
BESTextInfo.cc
1 // BESTextInfo.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <sstream>
34 #include <iostream>
35 
36 using std::ostringstream ;
37 
38 #include "BESTextInfo.h"
39 #include "BESUtil.h"
40 
51  : BESInfo( ),
52  _ishttp( ishttp ),
53  _header( false )
54 {
55 }
56 
76 BESTextInfo::BESTextInfo( const string &key, ostream *strm,
77  bool strm_owned, bool ishttp )
78  : BESInfo( key, strm, strm_owned ),
79  _ishttp( ishttp ),
80  _header( false )
81 {
82 }
83 
84 BESTextInfo::~BESTextInfo()
85 {
86 }
87 
95 void
96 BESTextInfo::begin_response( const string &response_name,
98 {
99  BESInfo::begin_response( response_name, dhi ) ;
100 }
101 
108 void
109 BESTextInfo::add_tag( const string &tag_name,
110  const string &tag_data,
111  map<string,string> *attrs )
112 {
113  add_data( _indent + tag_name + ": " + tag_data + "\n" ) ;
114  if( attrs )
115  {
116  map<string,string>::const_iterator i = attrs->begin() ;
117  map<string,string>::const_iterator e = attrs->end() ;
118  for( ; i != e; i++ )
119  {
120  string name = (*i).first ;
121  string val = (*i).second ;
122  add_data( _indent + " " + name + ": " + val + "\n" ) ;
123  }
124  }
125 }
126 
132 void
133 BESTextInfo::begin_tag( const string &tag_name,
134  map<string,string> *attrs )
135 {
136  BESInfo::begin_tag( tag_name ) ;
137  add_data( _indent + tag_name + "\n" ) ;
138  _indent += " " ;
139  if( attrs )
140  {
141  map<string,string>::const_iterator i = attrs->begin() ;
142  map<string,string>::const_iterator e = attrs->end() ;
143  for( ; i != e; i++ )
144  {
145  string name = (*i).first ;
146  string val = (*i).second ;
147  add_data( _indent + name + ": " + val + "\n" ) ;
148  }
149  }
150 }
151 
158 void
159 BESTextInfo::end_tag( const string &tag_name )
160 {
161  BESInfo::end_tag( tag_name ) ;
162  if( _indent.length() >= 4 )
163  _indent = _indent.substr( 0, _indent.length()-4 ) ;
164 }
165 
170 void
171 BESTextInfo::add_data( const string & s )
172 {
173  if( _ishttp && !_header && !_buffered )
174  {
175  BESUtil::set_mime_text( *_strm ) ;
176  _header = true ;
177  }
178  BESInfo::add_data( s ) ;
179 }
180 
185 void
186 BESTextInfo::add_space( unsigned long num_spaces )
187 {
188  string to_add ;
189  for( unsigned long i = 0; i < num_spaces; i++ )
190  {
191  to_add += " " ;
192  }
193  add_data( to_add ) ;
194 }
195 
200 void
201 BESTextInfo::add_break( unsigned long num_breaks )
202 {
203  string to_add ;
204  for( unsigned long i = 0; i < num_breaks; i++ )
205  {
206  to_add += "\n" ;
207  }
208  add_data( to_add ) ;
209 }
210 
219 void
220 BESTextInfo::add_data_from_file( const string &key, const string &name )
221 {
222  string newkey = key + ".TXT" ;
223  BESInfo::add_data_from_file( newkey, name ) ;
224 }
225 
234 void
237 {
238  transmitter->send_text( *this, dhi ) ;
239 }
240 
248 void
249 BESTextInfo::dump( ostream &strm ) const
250 {
251  strm << BESIndent::LMarg << "BESTextInfo::dump - ("
252  << (void *)this << ")" << endl ;
253  BESIndent::Indent() ;
254  strm << BESIndent::LMarg << "has header been added? " << _header << endl ;
255  strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl ;
256  strm << BESIndent::LMarg << "is http? " << _ishttp << endl ;
257  BESInfo::dump( strm ) ;
258  BESIndent::UnIndent() ;
259 }
260 
261 BESInfo *
262 BESTextInfo::BuildTextInfo( const string &/*info_type*/ )
263 {
264  return new BESTextInfo( ) ;
265 }
266 
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESTextInfo.cc:96
virtual void add_space(unsigned long num_spaces)
add a space to the informational response
Definition: BESTextInfo.cc:186
virtual void begin_tag(const string &tag_name, map< string, string > *attrs=0)
begin a tagged part of the information, information to follow
Definition: BESTextInfo.cc:133
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object.
Definition: BESInfo.cc:170
virtual void dump(ostream &strm) const
Displays debug information about this object.
Definition: BESInfo.cc:263
informational response object
Definition: BESInfo.h:68
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESTextInfo.cc:249
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object
Definition: BESTextInfo.cc:220
static void set_mime_text(ostream &strm)
Generate an HTTP 1.0 response header for a text document.
Definition: BESUtil.cc:67
virtual void add_tag(const string &tag_name, const string &tag_data, map< string, string > *attrs=0)
add tagged information to the inforamtional response
Definition: BESTextInfo.cc:109
BESTextInfo(bool ishttp=false)
constructs a basic text information response object.
Definition: BESTextInfo.cc:50
virtual void end_tag(const string &tag_name)
end a tagged part of the informational response
Definition: BESTextInfo.cc:159
virtual void add_break(unsigned long num_breaks)
add a line break to the information
Definition: BESTextInfo.cc:201
Structure storing information used by the BES to handle the request.
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:112
virtual void add_data(const string &s)
add data to this informational object. If buffering is not set then the information is output directl...
Definition: BESInfo.cc:148
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the text information as text
Definition: BESTextInfo.cc:235
virtual void add_data(const string &s)
add string data to the informational response
Definition: BESTextInfo.cc:171