28#include "BESXMLWriter.h"
30#include <libxml/encoding.h>
31#include <libxml/xmlwriter.h>
33#include <BESInternalFatalError.h>
35static const char *ENCODING =
"ISO-8859-1";
37static const char *HAI_NS =
"https://xml.opendap.org/ns/bes/admin/1.0#";
38constexpr int XML_BUF_SIZE = 2000000;
40BESXMLWriter::BESXMLWriter()
47 if (!(d_doc_buf = xmlBufferCreateSize(XML_BUF_SIZE)))
48 throw BESInternalFatalError(
"Error allocating the xml buffer", __FILE__, __LINE__);
50 xmlBufferSetAllocationScheme(d_doc_buf, XML_BUFFER_ALLOC_DOUBLEIT);
54 if (!(d_writer = xmlNewTextWriterMemory(d_doc_buf, 0)))
55 throw BESInternalFatalError(
"Error allocating memory for xml writer", __FILE__, __LINE__);
57 if (xmlTextWriterSetIndent(d_writer, 4) < 0)
58 throw BESInternalFatalError(
"Error starting indentation for response document ", __FILE__, __LINE__);
60 if (xmlTextWriterSetIndentString(d_writer, (
const xmlChar*)
" ") < 0)
61 throw BESInternalFatalError(
"Error setting indentation for response document ", __FILE__, __LINE__);
69 if (xmlTextWriterStartDocument(d_writer, NULL, ENCODING, NULL) < 0)
70 throw BESInternalFatalError(
"Error starting xml response document", __FILE__, __LINE__);
74 if (xmlTextWriterStartElementNS(d_writer, (
const xmlChar*)
"hai", (
const xmlChar*)
"BesAdminCmd", (
const xmlChar*) HAI_NS) < 0)
75 throw BESInternalFatalError(
"Error starting the response element for response ", __FILE__, __LINE__);
77 catch (BESInternalFatalError &e) {
83BESXMLWriter::~BESXMLWriter()
88void BESXMLWriter::m_cleanup()
92 xmlFreeTextWriter(d_writer);
96 xmlBufferFree(d_doc_buf);
104const char *BESXMLWriter::get_doc()
106 if (d_writer && d_started) {
108 if (xmlTextWriterEndElement(d_writer) < 0)
109 throw BESInternalFatalError(
"Error ending Dataset element.", __FILE__, __LINE__);
111 if (xmlTextWriterEndDocument(d_writer) < 0)
112 throw BESInternalFatalError(
"Error ending the document", __FILE__, __LINE__);
118 xmlFreeTextWriter(d_writer);
123 if (!d_doc_buf->content)
124 throw BESInternalFatalError(
"Error retrieving response document as string", __FILE__, __LINE__);
126 return (
const char *) d_doc_buf->content;