bes Updated for version 3.20.13
BESHTMLInfo.cc
1// BESHTMLInfo.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#include <map>
36
37using std::ostringstream;
38using std::endl;
39using std::map;
40using std::string;
41using std::ostream;
42
43#include "BESHTMLInfo.h"
44#include "BESUtil.h"
45
52 BESInfo(), _header(false), _do_indent(true)
53{
54}
55
64BESHTMLInfo::BESHTMLInfo(const string &key, ostream *strm, bool strm_owned) :
65 BESInfo(key, strm, strm_owned), _header(false), _do_indent(true)
66{
67}
68
69BESHTMLInfo::~BESHTMLInfo()
70{
71}
72
80void BESHTMLInfo::begin_response(const string &response_name, BESDataHandlerInterface &dhi)
81{
82 BESInfo::begin_response(response_name, dhi);
83 add_data("<HTML>\n");
84 _indent += " ";
85 add_data("<HEAD>\n");
86 _indent += " ";
87 add_data((string) "<TITLE>" + response_name + "</TITLE>\n");
88 if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
89 add_data("</HEAD>\n");
90 add_data("<BODY>\n");
91 _indent += " ";
92}
93
102{
103 if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
104 add_data("</BODY>\n");
105 if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
106 add_data("</HTML>\n");
107}
108
115void BESHTMLInfo::add_tag(const string &tag_name, const string &tag_data, map<string, string> *attrs)
116{
117 string to_add = tag_name + ": " + tag_data + "<BR />\n";
118 add_data(to_add);
119 if (attrs) {
120 map<string, string>::const_iterator i = attrs->begin();
121 map<string, string>::const_iterator e = attrs->end();
122 for (; i != e; i++) {
123 string name = (*i).first;
124 string val = (*i).second;
125 BESInfo::add_data(_indent + " " + name + ": " + val + "<BR />\n");
126 }
127 }
128}
129
135void BESHTMLInfo::begin_tag(const string &tag_name, map<string, string> *attrs)
136{
137 BESInfo::begin_tag(tag_name);
138 string to_add = tag_name + "<BR />\n";
139 add_data(to_add);
140 _indent += " ";
141 if (attrs) {
142 map<string, string>::const_iterator i = attrs->begin();
143 map<string, string>::const_iterator e = attrs->end();
144 for (; i != e; i++) {
145 string name = (*i).first;
146 string val = (*i).second;
147 BESInfo::add_data(_indent + name + ": " + val + "<BR />\n");
148 }
149 }
150}
151
158void BESHTMLInfo::end_tag(const string &tag_name)
159{
160 BESInfo::end_tag(tag_name);
161 if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
162}
163
168void BESHTMLInfo::add_space(unsigned long num_spaces)
169{
170 string to_add;
171 for (unsigned long i = 0; i < num_spaces; i++) {
172 to_add += "&nbsp;";
173 }
174 _do_indent = false;
175 add_data(to_add);
176}
177
182void BESHTMLInfo::add_break(unsigned long num_breaks)
183{
184 string to_add;
185 for (unsigned long i = 0; i < num_breaks; i++) {
186 to_add += "<BR />";
187 }
188 to_add += "\n";
189 _do_indent = false;
190 add_data(to_add);
191}
192
202void BESHTMLInfo::add_data(const string &s)
203{
204 if (!_header && !_buffered) {
206 _header = true;
207 }
208 if (_do_indent)
209 BESInfo::add_data(_indent + s);
210 else
212 _do_indent = true;
213}
214
223void BESHTMLInfo::add_data_from_file(const string &key, const string &name)
224{
225 string newkey = key + ".HTML";
226 BESInfo::add_data_from_file(newkey, name);
227}
228
238{
239 transmitter->send_html(*this, dhi);
240}
241
249void BESHTMLInfo::dump(ostream &strm) const
250{
251 strm << BESIndent::LMarg << "BESHTMLInfo::dump - (" << (void *) this << ")" << endl;
252 BESIndent::Indent();
253 strm << BESIndent::LMarg << "has header been added? " << _header << endl;
254 strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl;
255 strm << BESIndent::LMarg << "do indent? " << _do_indent << endl;
256 BESInfo::dump(strm);
257 BESIndent::UnIndent();
258}
259
260BESInfo *
261BESHTMLInfo::BuildHTMLInfo(const string &/*info_type*/)
262{
263 return new BESHTMLInfo();
264}
265
Structure storing information used by the BES to handle the request.
virtual void add_data(const std::string &s)
add data to this informational object.
Definition: BESHTMLInfo.cc:202
virtual void begin_tag(const std::string &tag_name, std::map< std::string, std::string > *attrs=0)
begin a tagged part of the information, information to follow
Definition: BESHTMLInfo.cc:135
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the text information as text
Definition: BESHTMLInfo.cc:237
virtual void add_tag(const std::string &tag_name, const std::string &tag_data, std::map< std::string, std::string > *attrs=0)
add tagged information to the inforamtional response
Definition: BESHTMLInfo.cc:115
virtual void end_tag(const std::string &tag_name)
end a tagged part of the informational response
Definition: BESHTMLInfo.cc:158
virtual void end_response()
end the response
Definition: BESHTMLInfo.cc:101
virtual void add_break(unsigned long num_breaks)
add a line break to the information
Definition: BESHTMLInfo.cc:182
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESHTMLInfo.cc:80
virtual void add_space(unsigned long num_spaces)
add a space to the informational response
Definition: BESHTMLInfo.cc:168
BESHTMLInfo()
constructs an html formatted information response object.
Definition: BESHTMLInfo.cc:51
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: BESHTMLInfo.cc:249
virtual void add_data_from_file(const std::string &key, const std::string &name)
add data from a file to the informational object
Definition: BESHTMLInfo.cc:223
informational response object
Definition: BESInfo.h:63
virtual void add_data(const std::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 begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESInfo.cc:124
virtual void dump(std::ostream &strm) const
Displays debug information about this object.
Definition: BESInfo.cc:275
virtual void add_data_from_file(const std::string &key, const std::string &name)
add data from a file to the informational object.
Definition: BESInfo.cc:182
static void set_mime_html(std::ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition: BESUtil.cc:155