36using std::ostringstream;
42#include "BESXMLInfo.h"
44#include "BESDataNames.h"
46#define MY_ENCODING "ISO-8859-1"
47#define BES_SCHEMA "http://xml.opendap.org/ns/bes/1.0#"
55 BESInfo(), _writer(0), _doc_buf(0), _started(false), _ended(false)
59BESXMLInfo::~BESXMLInfo()
64void BESXMLInfo::cleanup()
68 xmlFreeTextWriter(_writer);
74 xmlBufferFree(_doc_buf);
84 ((ostringstream *) _strm)->str(
"");
98 map<string, string> empty_attrs;
114 _response_name = response_name;
124 _doc_buf = xmlBufferCreate();
125 if (_doc_buf == NULL) {
127 string err = (string)
"Error creating the xml buffer for response " + _response_name;
133 _writer = xmlNewTextWriterMemory(_doc_buf, 0);
134 if (_writer == NULL) {
136 string err = (string)
"Error creating the xml writer for response " + _response_name;
140 rc = xmlTextWriterSetIndent(_writer, 4);
143 string err = (string)
"Error starting indentation for response document " + _response_name;
147 rc = xmlTextWriterSetIndentString( _writer, BAD_CAST
" " );
150 string err = (string)
"Error setting indentation for response document " + _response_name;
159 rc = xmlTextWriterStartDocument(_writer, NULL, MY_ENCODING, NULL);
162 string err = (string)
"Error starting xml response document for " + _response_name;
168 rc = xmlTextWriterStartElementNS(_writer, NULL, BAD_CAST
"response", BAD_CAST BES_SCHEMA);
171 string err = (string)
"Error starting the response element for response " + _response_name;
176 string reqid = dhi.
data[REQUEST_ID];
177 if (!reqid.empty()) {
178 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST REQUEST_ID,
179 BAD_CAST reqid.c_str() );
182 string err = (string)
"Error adding attribute " + REQUEST_ID +
" for response " + _response_name;
188 rc = xmlTextWriterStartElement( _writer, BAD_CAST _response_name.c_str() );
191 string err = (string)
"Error creating root element for response " + _response_name;
195 map<string, string>::iterator it;
196 for ( it = attrs->begin(); it != attrs->end(); it++ )
198 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST it->first.c_str(), BAD_CAST it->second.c_str());
201 string err = (string)
"Error creating root element for response " + _response_name;
217 BESInfo::end_response();
222 rc = xmlTextWriterEndElement(_writer);
225 string err = (string)
"Error ending response element for response " + _response_name;
230 rc = xmlTextWriterEndElement(_writer);
233 string err = (string)
"Error ending specific response element " +
"for response " + _response_name;
237 rc = xmlTextWriterEndDocument(_writer);
240 string err = (string)
"Error ending the response document for response " + _response_name;
245 xmlFreeTextWriter(_writer);
249 if (!_doc_buf->content) {
251 string err = (string)
"Error retrieving response document as string " +
"for response " + _response_name;
255 _doc = (
char *) _doc_buf->content;
272 int rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str() );
275 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
280 map<string, string>::const_iterator i = attrs->begin();
281 map<string, string>::const_iterator e = attrs->end();
282 for (; i != e; i++) {
283 string name = (*i).first;
284 string val = (*i).second;
288 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
289 BAD_CAST val.c_str() );
292 string err = (string)
"Error adding attribute " + name +
" for response " + _response_name;
299 if (!tag_data.empty()) {
300 rc = xmlTextWriterWriteString( _writer, BAD_CAST tag_data.c_str() );
303 string err = (string)
"Error writing the value for element " + tag_name +
" for response " + _response_name;
309 rc = xmlTextWriterEndElement(_writer);
312 string err = (string)
"Error ending element " + tag_name +
" for response " + _response_name;
336 BESInfo::begin_tag(tag_name);
340 if (ns.empty() && uri.empty()) {
341 rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str());
344 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
349 const char *cns = NULL;
350 if (!ns.empty()) cns = ns.c_str();
351 rc = xmlTextWriterStartElementNS( _writer,
353 BAD_CAST tag_name.c_str(),
354 BAD_CAST uri.c_str() );
357 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
363 map<string, string>::const_iterator i = attrs->begin();
364 map<string, string>::const_iterator e = attrs->end();
365 for (; i != e; i++) {
366 string name = (*i).first;
367 string val = (*i).second;
370 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
371 BAD_CAST val.c_str() );
374 string err = (string)
"Error adding attribute " + name +
" for response " + _response_name;
389 BESInfo::end_tag(tag_name);
393 string s = ((ostringstream *) _strm)->str();
396 rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() );
399 string err = (string)
"Error writing the value for element " + tag_name +
" for response " + _response_name;
403 ((ostringstream *) _strm)->str(
"");
407 rc = xmlTextWriterEndElement(_writer);
410 string err = (string)
"Error ending element " + tag_name +
" for response " + _response_name;
422 for (
unsigned long i = 0; i < num_spaces; i++) {
435 for (
unsigned long i = 0; i < num_breaks; i++) {
446 int rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() );
449 throw BESInternalError(
string(
"Error writing String data for response ") + _response_name, __FILE__, __LINE__);
466 begin_tag(
"html",
"",
"http://www.w3.org/1999/xhtml");
468 string newkey = key +
".HTML";
484 if (_started && !_ended) {
487 transmitter->send_text(*
this, dhi);
497 if (_started && !_ended) {
512 strm << BESIndent::LMarg <<
"BESXMLInfo::dump - (" << (
void *)
this <<
")" << endl;
515 BESIndent::UnIndent();
519BESXMLInfo::BuildXMLInfo(
const string &)
Structure storing information used by the BES to handle the request.
std::map< std::string, std::string > data
the map of string data that will be required for the current request.
informational response object
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...
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
virtual void dump(std::ostream &strm) const
Displays debug information about this object.
virtual void add_data_from_file(const std::string &key, const std::string &name)
add data from a file to the informational object.
exception thrown if internal error encountered
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 informational response
virtual void end_tag(const std::string &tag_name)
end a tagged part of the informational response
virtual void add_data_from_file(const std::string &key, const std::string &name)
add data from a file to the informational object
virtual void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi)
transmit the text information as text
virtual void end_response()
end the response
virtual void add_space(unsigned long num_spaces)
add a space to the informational response
virtual void add_break(unsigned long num_breaks)
add a line break to the information
BESXMLInfo()
constructs an informational response object as an xml document
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...
virtual void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi)
begin the informational response
virtual void begin_tag(const std::string &tag_name, const std::string &ns, const std::string &uri, std::map< std::string, std::string > *attrs=0)
begin a tagged part of the information, information to follow
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void print(std::ostream &strm)
print the information from this informational object to the specified stream