35 using std::ostringstream;
37 #include "BESXMLInfo.h"
39 #include "BESDataNames.h"
41 #define MY_ENCODING "ISO-8859-1"
42 #define BES_SCHEMA "http://xml.opendap.org/ns/bes/1.0#"
50 BESInfo(), _writer(0), _doc_buf(0), _started(false), _ended(false)
54 BESXMLInfo::~BESXMLInfo()
59 void BESXMLInfo::cleanup()
63 xmlFreeTextWriter(_writer);
69 xmlBufferFree(_doc_buf);
79 ((ostringstream *) _strm)->str(
"");
93 map<string, string> empty_attrs;
109 _response_name = response_name;
119 _doc_buf = xmlBufferCreate();
120 if (_doc_buf == NULL) {
122 string err = (string)
"Error creating the xml buffer for response " + _response_name;
128 _writer = xmlNewTextWriterMemory(_doc_buf, 0);
129 if (_writer == NULL) {
131 string err = (string)
"Error creating the xml writer for response " + _response_name;
135 rc = xmlTextWriterSetIndent(_writer, 4);
138 string err = (string)
"Error starting indentation for response document " + _response_name;
142 rc = xmlTextWriterSetIndentString( _writer, BAD_CAST
" " );
145 string err = (string)
"Error setting indentation for response document " + _response_name;
154 rc = xmlTextWriterStartDocument(_writer, NULL, MY_ENCODING, NULL);
157 string err = (string)
"Error starting xml response document for " + _response_name;
163 rc = xmlTextWriterStartElementNS(_writer, NULL, BAD_CAST
"response", BAD_CAST BES_SCHEMA);
166 string err = (string)
"Error starting the response element for response " + _response_name;
171 string reqid = dhi.
data[REQUEST_ID];
172 if (!reqid.empty()) {
173 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST REQUEST_ID,
174 BAD_CAST reqid.c_str() );
177 string err = (string)
"Error adding attribute " + REQUEST_ID +
" for response " + _response_name;
183 rc = xmlTextWriterStartElement( _writer, BAD_CAST _response_name.c_str() );
186 string err = (string)
"Error creating root element for response " + _response_name;
190 map<string, string>::iterator it;
191 for ( it = attrs->begin(); it != attrs->end(); it++ )
193 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST it->first.c_str(), BAD_CAST it->second.c_str());
196 string err = (string)
"Error creating root element for response " + _response_name;
212 BESInfo::end_response();
217 rc = xmlTextWriterEndElement(_writer);
220 string err = (string)
"Error ending response element for response " + _response_name;
225 rc = xmlTextWriterEndElement(_writer);
228 string err = (string)
"Error ending specific response element " +
"for response " + _response_name;
232 rc = xmlTextWriterEndDocument(_writer);
235 string err = (string)
"Error ending the response document for response " + _response_name;
240 xmlFreeTextWriter(_writer);
244 if (!_doc_buf->content) {
246 string err = (string)
"Error retrieving response document as string " +
"for response " + _response_name;
250 _doc = (
char *) _doc_buf->content;
267 int rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str() );
270 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
275 map<string, string>::const_iterator i = attrs->begin();
276 map<string, string>::const_iterator e = attrs->end();
277 for (; i != e; i++) {
278 string name = (*i).first;
279 string val = (*i).second;
283 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
284 BAD_CAST val.c_str() );
287 string err = (string)
"Error adding attribute " + name +
" for response " + _response_name;
294 if (!tag_data.empty()) {
295 rc = xmlTextWriterWriteString( _writer, BAD_CAST tag_data.c_str() );
298 string err = (string)
"Error writing the value for element " + tag_name +
" for response " + _response_name;
304 rc = xmlTextWriterEndElement(_writer);
307 string err = (string)
"Error ending element " + tag_name +
" for response " + _response_name;
331 BESInfo::begin_tag(tag_name);
335 if (ns.empty() && uri.empty()) {
336 rc = xmlTextWriterStartElement( _writer, BAD_CAST tag_name.c_str());
339 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
344 const char *cns = NULL;
345 if (!ns.empty()) cns = ns.c_str();
346 rc = xmlTextWriterStartElementNS( _writer,
348 BAD_CAST tag_name.c_str(),
349 BAD_CAST uri.c_str() );
352 string err = (string)
"Error starting element " + tag_name +
" for response " + _response_name;
358 map<string, string>::const_iterator i = attrs->begin();
359 map<string, string>::const_iterator e = attrs->end();
360 for (; i != e; i++) {
361 string name = (*i).first;
362 string val = (*i).second;
365 rc = xmlTextWriterWriteAttribute( _writer, BAD_CAST name.c_str(),
366 BAD_CAST val.c_str() );
369 string err = (string)
"Error adding attribute " + name +
" for response " + _response_name;
384 BESInfo::end_tag(tag_name);
388 string s = ((ostringstream *) _strm)->str();
391 rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() );
394 string err = (string)
"Error writing the value for element " + tag_name +
" for response " + _response_name;
398 ((ostringstream *) _strm)->str(
"");
402 rc = xmlTextWriterEndElement(_writer);
405 string err = (string)
"Error ending element " + tag_name +
" for response " + _response_name;
417 for (
unsigned long i = 0; i < num_spaces; i++) {
430 for (
unsigned long i = 0; i < num_breaks; i++) {
441 int rc = xmlTextWriterWriteString( _writer, BAD_CAST s.c_str() );
444 throw BESInternalError(
string(
"Error writing String data for response ") + _response_name, __FILE__, __LINE__);
461 begin_tag(
"html",
"",
"http://www.w3.org/1999/xhtml");
463 string newkey = key +
".HTML";
479 if (_started && !_ended) {
482 transmitter->send_text(*
this, dhi);
492 if (_started && !_ended) {
507 strm << BESIndent::LMarg <<
"BESXMLInfo::dump - (" << (
void *)
this <<
")" << endl;
510 BESIndent::UnIndent();
514 BESXMLInfo::BuildXMLInfo(
const string &)