bes  Updated for version 3.20.5
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(), _ishttp(ishttp), _header(false)
52 {
53 }
54 
74 BESTextInfo::BESTextInfo(const string &key, ostream *strm, bool strm_owned, bool ishttp) :
75  BESInfo(key, strm, strm_owned), _ishttp(ishttp), _header(false)
76 {
77 }
78 
79 BESTextInfo::~BESTextInfo()
80 {
81 }
82 
90 void BESTextInfo::begin_response(const string &response_name, BESDataHandlerInterface &dhi)
91 {
92  BESInfo::begin_response(response_name, dhi);
93 }
94 
101 void BESTextInfo::add_tag(const string &tag_name, const string &tag_data, map<string, string> *attrs)
102 {
103  add_data(_indent + tag_name + ": " + tag_data + "\n");
104  if (attrs) {
105  map<string, string>::const_iterator i = attrs->begin();
106  map<string, string>::const_iterator e = attrs->end();
107  for (; i != e; i++) {
108  string name = (*i).first;
109  string val = (*i).second;
110  add_data(_indent + " " + name + ": " + val + "\n");
111  }
112  }
113 }
114 
120 void BESTextInfo::begin_tag(const string &tag_name, map<string, string> *attrs)
121 {
122  BESInfo::begin_tag(tag_name);
123  add_data(_indent + tag_name + "\n");
124  _indent += " ";
125  if (attrs) {
126  map<string, string>::const_iterator i = attrs->begin();
127  map<string, string>::const_iterator e = attrs->end();
128  for (; i != e; i++) {
129  string name = (*i).first;
130  string val = (*i).second;
131  add_data(_indent + name + ": " + val + "\n");
132  }
133  }
134 }
135 
142 void BESTextInfo::end_tag(const string &tag_name)
143 {
144  BESInfo::end_tag(tag_name);
145  if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
146 }
147 
152 void BESTextInfo::add_data(const string & s)
153 {
154  if (_ishttp && !_header && !_buffered) {
155  BESUtil::set_mime_text(*_strm);
156  _header = true;
157  }
159 }
160 
165 void BESTextInfo::add_space(unsigned long num_spaces)
166 {
167  string to_add;
168  for (unsigned long i = 0; i < num_spaces; i++) {
169  to_add += " ";
170  }
171  add_data(to_add);
172 }
173 
178 void BESTextInfo::add_break(unsigned long num_breaks)
179 {
180  string to_add;
181  for (unsigned long i = 0; i < num_breaks; i++) {
182  to_add += "\n";
183  }
184  add_data(to_add);
185 }
186 
195 void BESTextInfo::add_data_from_file(const string &key, const string &name)
196 {
197  string newkey = key + ".TXT";
198  BESInfo::add_data_from_file(newkey, name);
199 }
200 
210 {
211  transmitter->send_text(*this, dhi);
212 }
213 
221 void BESTextInfo::dump(ostream &strm) const
222 {
223  strm << BESIndent::LMarg << "BESTextInfo::dump - (" << (void *) this << ")" << endl;
224  BESIndent::Indent();
225  strm << BESIndent::LMarg << "has header been added? " << _header << endl;
226  strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl;
227  strm << BESIndent::LMarg << "is http? " << _ishttp << endl;
228  BESInfo::dump(strm);
229  BESIndent::UnIndent();
230 }
231 
232 BESInfo *
233 BESTextInfo::BuildTextInfo(const string &/*info_type*/)
234 {
235  return new BESTextInfo();
236 }
237 
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESTextInfo.cc:90
virtual void add_space(unsigned long num_spaces)
add a space to the informational response
Definition: BESTextInfo.cc:165
virtual void dump(ostream &strm) const
Displays debug information about this object.
Definition: BESInfo.cc:275
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:120
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object.
Definition: BESInfo.cc:182
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESTextInfo.cc:221
informational response object
Definition: BESInfo.h:68
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object
Definition: BESTextInfo.cc:195
static void set_mime_text(ostream &strm)
Generate an HTTP 1.0 response header for a text document.
Definition: BESUtil.cc:80
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:101
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:142
virtual void add_break(unsigned long num_breaks)
add a line break to the information
Definition: BESTextInfo.cc:178
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:124
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:160
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the text information as text
Definition: BESTextInfo.cc:209
virtual void add_data(const string &s)
add string data to the informational response
Definition: BESTextInfo.cc:152