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, std::less<>> 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_KEY];
177 if (!reqid.empty()) {
178 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST REQUEST_ID_KEY,
179 BAD_CAST reqid.c_str() );
182 string err = (string)
"Error adding attribute " + REQUEST_ID_KEY +
" for response " + _response_name;
188 string req_uuid = dhi.
data[REQUEST_UUID_KEY];
189 if (!req_uuid.empty()) {
190 rc = xmlTextWriterWriteAttribute( _writer, (
const xmlChar *)( REQUEST_UUID_KEY),
191 (
const xmlChar *)(req_uuid.c_str()) );
194 string err = (string)
"Error adding attribute " + REQUEST_UUID_KEY +
" for response " + _response_name;
200 rc = xmlTextWriterStartElement( _writer, BAD_CAST _response_name.c_str() );
203 string err = (string)
"Error creating root element for response " + _response_name;
208 for ( it = attrs->begin(); it != attrs->end(); it++ )
210 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST it->first.c_str(), BAD_CAST it->second.c_str());
213 string err = (string)
"Error creating root element for response " + _response_name;
229 BESInfo::end_response();
234 rc = xmlTextWriterEndElement(_writer);
237 string err = (string)
"Error ending response element for response " + _response_name;
242 rc = xmlTextWriterEndElement(_writer);
245 string err = (string)
"Error ending specific response element " +
"for response " + _response_name;
249 rc = xmlTextWriterEndDocument(_writer);
252 string err = (string)
"Error ending the response document for response " + _response_name;
257 xmlFreeTextWriter(_writer);
261 if (!_doc_buf->content) {
263 string err = (string)
"Error retrieving response document as string " +
"for response " + _response_name;
267 _doc = (
char *) _doc_buf->content;
281void BESXMLInfo::add_tag(
const string &tag_name,
const string &tag_data, map<
string,
string, std::less<>> *attrs)
284 int rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str() );
287 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
294 for (; i != e; i++) {
295 string name = (*i).first;
296 string val = (*i).second;
300 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
301 BAD_CAST val.c_str() );
304 string err = (string)
"Error adding attribute " + name +
" for response " + _response_name;
311 if (!tag_data.empty()) {
312 rc = xmlTextWriterWriteString( _writer, BAD_CAST tag_data.c_str() );
315 string err = (string)
"Error writing the value for element " + tag_name +
" for response " + _response_name;
321 rc = xmlTextWriterEndElement(_writer);
324 string err = (string)
"Error ending element " + tag_name +
" for response " + _response_name;
349 map<
string,
string, std::less<>> *attrs =
nullptr)
351 BESInfo::begin_tag(tag_name);
355 if (ns.empty() && uri.empty()) {
356 rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str());
359 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
364 const char *cns =
nullptr;
365 if (!ns.empty()) cns = ns.c_str();
366 rc = xmlTextWriterStartElementNS( _writer,
368 BAD_CAST tag_name.c_str(),
369 BAD_CAST uri.c_str() );
372 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
380 for (; i != e; i++) {
381 string name = (*i).first;
382 string val = (*i).second;
385 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
386 BAD_CAST val.c_str() );
389 string err = (string)
"Error adding attribute " + name +
" for response " + _response_name;
404 BESInfo::end_tag(tag_name);
408 string s = ((ostringstream *) _strm)->str();
411 rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() );
414 string err = (string)
"Error writing the value for element " + tag_name +
" for response " + _response_name;
418 ((ostringstream *) _strm)->str(
"");
422 rc = xmlTextWriterEndElement(_writer);
425 string err = (string)
"Error ending element " + tag_name +
" for response " + _response_name;
437 for (
unsigned long i = 0; i < num_spaces; i++) {
450 for (
unsigned long i = 0; i < num_breaks; i++) {
461 int rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() );
464 throw BESInternalError(
string(
"Error writing String data for response ") + _response_name, __FILE__, __LINE__);
481 begin_tag(
"html",
"",
"http://www.w3.org/1999/xhtml");
483 string newkey = key +
".HTML";
499 if (_started && !_ended) {
502 transmitter->send_text(*
this, dhi);
512 if (_started && !_ended) {
527 strm << BESIndent::LMarg <<
"BESXMLInfo::dump - (" << (
void *)
this <<
")" << endl;
530 BESIndent::UnIndent();
534BESXMLInfo::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...
BESInfo()
constructs a BESInfo object
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
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 informational response
void dump(std::ostream &strm) const override
dumps information about this object
void add_data(const std::string &s) override
add data to this informational object. If buffering is not set then the information is output directl...
void add_break(unsigned long num_breaks) override
add a line break to the information
void add_data_from_file(const std::string &key, const std::string &name) override
add data from a file to the informational object
void end_tag(const std::string &tag_name) override
end a tagged part of the informational response
void begin_response(const std::string &response_name, BESDataHandlerInterface &dhi) override
begin the informational response
void transmit(BESTransmitter *transmitter, BESDataHandlerInterface &dhi) override
transmit the text information as text
void add_space(unsigned long num_spaces) override
add a space to the informational response
BESXMLInfo()
constructs an informational response object as an xml document
void print(std::ostream &strm) override
print the information from this informational object to the specified stream
void begin_tag(const std::string &tag_name, const std::string &ns, const std::string &uri, std::map< std::string, std::string, std::less<> > *attrs)
begin a tagged part of the information, information to follow
void end_response() override
end the response