bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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.size() >= 4) _indent = _indent.substr(0, _indent.size() - 4);
89 add_data("</HEAD>\n");
90 add_data("<BODY>\n");
91 _indent += " ";
92}
93
102{
103 if (_indent.size() >= 4) _indent = _indent.substr(0, _indent.size() - 4);
104 add_data("</BODY>\n");
105 if (_indent.size() >= 4) _indent = _indent.substr(0, _indent.size() - 4);
106 add_data("</HTML>\n");
107}
108
115void BESHTMLInfo::add_tag(const string &tag_name,
116 const string &tag_data,
117 map<string, string, std::less<>> *attrs){
118 string to_add = tag_name + ": " + tag_data + "<BR />\n";
119 add_data(to_add);
120 if (attrs) {
121 map<string, string>::const_iterator i = attrs->begin();
123 for (; i != e; i++) {
124 string name = (*i).first;
125 string val = (*i).second;
126 BESInfo::add_data(_indent + " " + name + ": " + val + "<BR />\n");
127 }
128 }
129}
130
136void BESHTMLInfo::begin_tag(const string &tag_name, map<string, string, std::less<>> *attrs)
137{
138 BESInfo::begin_tag(tag_name);
139 string to_add = tag_name + "<BR />\n";
140 add_data(to_add);
141 _indent += " ";
142 if (attrs) {
143 map<string, string>::const_iterator i = attrs->begin();
145 for (; i != e; i++) {
146 string name = (*i).first;
147 string val = (*i).second;
148 BESInfo::add_data(_indent + name + ": " + val + "<BR />\n");
149 }
150 }
151}
152
159void BESHTMLInfo::end_tag(const string &tag_name)
160{
161 BESInfo::end_tag(tag_name);
162 if (_indent.size() >= 4) _indent = _indent.substr(0, _indent.size() - 4);
163}
164
169void BESHTMLInfo::add_space(unsigned long num_spaces)
170{
171 string to_add;
172 for (unsigned long i = 0; i < num_spaces; i++) {
173 to_add += "&nbsp;";
174 }
175 _do_indent = false;
176 add_data(to_add);
177}
178
183void BESHTMLInfo::add_break(unsigned long num_breaks)
184{
185 string to_add;
186 for (unsigned long i = 0; i < num_breaks; i++) {
187 to_add += "<BR />";
188 }
189 to_add += "\n";
190 _do_indent = false;
191 add_data(to_add);
192}
193
203void BESHTMLInfo::add_data(const string &s)
204{
205 if (!_header && !_buffered) {
207 _header = true;
208 }
209 if (_do_indent)
210 BESInfo::add_data(_indent + s);
211 else
213 _do_indent = true;
214}
215
224void BESHTMLInfo::add_data_from_file(const string &key, const string &name)
225{
226 string newkey = key + ".HTML";
227 BESInfo::add_data_from_file(newkey, name);
228}
229
239{
240 transmitter->send_html(*this, dhi);
241}
242
250void BESHTMLInfo::dump(ostream &strm) const
251{
252 strm << BESIndent::LMarg << "BESHTMLInfo::dump - (" << (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 << "do indent? " << _do_indent << endl;
257 BESInfo::dump(strm);
258 BESIndent::UnIndent();
259}
260
261BESInfo *
262BESHTMLInfo::BuildHTMLInfo(const string &/*info_type*/)
263{
264 return new BESHTMLInfo();
265}
266
Structure storing information used by the BES to handle the request.
void add_data(const std::string &s) override
add data to this informational object.
void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi) override
transmit the text information as text
void begin_tag(const std::string &tag_name, std::map< std::string, std::string, std::less<> > *attrs=nullptr) override
begin a tagged part of the information, information to follow
void add_break(unsigned long num_breaks) override
add a line break to the information
void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi) override
begin the informational response
void end_tag(const std::string &tag_name) override
end a tagged part of the informational response
void dump(std::ostream &strm) const override
dumps information about this object
void add_data_from_file(const std::string &key, const std::string &name) override
add data from a file to the informational object
void add_tag(const std::string &tag_name, const std::string &tag_data, std::map< std::string, std::string, std::less<> > *attrs=nullptr) override
add tagged information to the inforamtional response
BESHTMLInfo()
constructs an html formatted information response object.
void add_space(unsigned long num_spaces) override
add a space to the informational response
void end_response() override
end the response
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:151
BESInfo()
constructs a BESInfo object
Definition BESInfo.cc:52
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition BESInfo.cc:120
virtual void dump(std::ostream &strm) const
Displays debug information about this object.
Definition BESInfo.cc:262
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:172
static void set_mime_html(std::ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition BESUtil.cc:158
STL iterator class.