bes  Updated for version 3.20.5
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 
36 using std::ostringstream;
37 
38 #include "BESHTMLInfo.h"
39 #include "BESUtil.h"
40 
47  BESInfo(), _header(false), _do_indent(true)
48 {
49 }
50 
59 BESHTMLInfo::BESHTMLInfo(const string &key, ostream *strm, bool strm_owned) :
60  BESInfo(key, strm, strm_owned), _header(false), _do_indent(true)
61 {
62 }
63 
64 BESHTMLInfo::~BESHTMLInfo()
65 {
66 }
67 
75 void BESHTMLInfo::begin_response(const string &response_name, BESDataHandlerInterface &dhi)
76 {
77  BESInfo::begin_response(response_name, dhi);
78  add_data("<HTML>\n");
79  _indent += " ";
80  add_data("<HEAD>\n");
81  _indent += " ";
82  add_data((string) "<TITLE>" + response_name + "</TITLE>\n");
83  if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
84  add_data("</HEAD>\n");
85  add_data("<BODY>\n");
86  _indent += " ";
87 }
88 
97 {
98  if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
99  add_data("</BODY>\n");
100  if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
101  add_data("</HTML>\n");
102 }
103 
110 void BESHTMLInfo::add_tag(const string &tag_name, const string &tag_data, map<string, string> *attrs)
111 {
112  string to_add = tag_name + ": " + tag_data + "<BR />\n";
113  add_data(to_add);
114  if (attrs) {
115  map<string, string>::const_iterator i = attrs->begin();
116  map<string, string>::const_iterator e = attrs->end();
117  for (; i != e; i++) {
118  string name = (*i).first;
119  string val = (*i).second;
120  BESInfo::add_data(_indent + " " + name + ": " + val + "<BR />\n");
121  }
122  }
123 }
124 
130 void BESHTMLInfo::begin_tag(const string &tag_name, map<string, string> *attrs)
131 {
132  BESInfo::begin_tag(tag_name);
133  string to_add = tag_name + "<BR />\n";
134  add_data(to_add);
135  _indent += " ";
136  if (attrs) {
137  map<string, string>::const_iterator i = attrs->begin();
138  map<string, string>::const_iterator e = attrs->end();
139  for (; i != e; i++) {
140  string name = (*i).first;
141  string val = (*i).second;
142  BESInfo::add_data(_indent + name + ": " + val + "<BR />\n");
143  }
144  }
145 }
146 
153 void BESHTMLInfo::end_tag(const string &tag_name)
154 {
155  BESInfo::end_tag(tag_name);
156  if (_indent.length() >= 4) _indent = _indent.substr(0, _indent.length() - 4);
157 }
158 
163 void BESHTMLInfo::add_space(unsigned long num_spaces)
164 {
165  string to_add;
166  for (unsigned long i = 0; i < num_spaces; i++) {
167  to_add += "&nbsp;";
168  }
169  _do_indent = false;
170  add_data(to_add);
171 }
172 
177 void BESHTMLInfo::add_break(unsigned long num_breaks)
178 {
179  string to_add;
180  for (unsigned long i = 0; i < num_breaks; i++) {
181  to_add += "<BR />";
182  }
183  to_add += "\n";
184  _do_indent = false;
185  add_data(to_add);
186 }
187 
197 void BESHTMLInfo::add_data(const string &s)
198 {
199  if (!_header && !_buffered) {
200  BESUtil::set_mime_html(*_strm);
201  _header = true;
202  }
203  if (_do_indent)
204  BESInfo::add_data(_indent + s);
205  else
207  _do_indent = true;
208 }
209 
218 void BESHTMLInfo::add_data_from_file(const string &key, const string &name)
219 {
220  string newkey = key + ".HTML";
221  BESInfo::add_data_from_file(newkey, name);
222 }
223 
233 {
234  transmitter->send_html(*this, dhi);
235 }
236 
244 void BESHTMLInfo::dump(ostream &strm) const
245 {
246  strm << BESIndent::LMarg << "BESHTMLInfo::dump - (" << (void *) this << ")" << endl;
247  BESIndent::Indent();
248  strm << BESIndent::LMarg << "has header been added? " << _header << endl;
249  strm << BESIndent::LMarg << "indentation \"" << _indent << "\"" << endl;
250  strm << BESIndent::LMarg << "do indent? " << _do_indent << endl;
251  BESInfo::dump(strm);
252  BESIndent::UnIndent();
253 }
254 
255 BESInfo *
256 BESHTMLInfo::BuildHTMLInfo(const string &/*info_type*/)
257 {
258  return new BESHTMLInfo();
259 }
260 
virtual void dump(ostream &strm) const
dumps information about this object
Definition: BESHTMLInfo.cc:244
virtual void dump(ostream &strm) const
Displays debug information about this object.
Definition: BESInfo.cc:275
BESHTMLInfo()
constructs an html formatted information response object.
Definition: BESHTMLInfo.cc:46
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 end_response()
end the response
Definition: BESHTMLInfo.cc:96
virtual void add_space(unsigned long num_spaces)
add a space to the informational response
Definition: BESHTMLInfo.cc:163
informational response object
Definition: BESInfo.h:68
virtual void begin_tag(const string &tag_name, map< string, string > *attrs=0)
begin a tagged part of the information, information to follow
Definition: BESHTMLInfo.cc:130
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: BESHTMLInfo.cc:110
static void set_mime_html(ostream &strm)
Generate an HTTP 1.0 response header for a html document.
Definition: BESUtil.cc:99
virtual void add_data(const string &s)
add data to this informational object.
Definition: BESHTMLInfo.cc:197
virtual void add_break(unsigned long num_breaks)
add a line break to the information
Definition: BESHTMLInfo.cc:177
virtual void end_tag(const string &tag_name)
end a tagged part of the informational response
Definition: BESHTMLInfo.cc:153
Structure storing information used by the BES to handle the request.
virtual void add_data_from_file(const string &key, const string &name)
add data from a file to the informational object
Definition: BESHTMLInfo.cc:218
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: BESHTMLInfo.cc:232
virtual void begin_response(const string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
Definition: BESHTMLInfo.cc:75