33 #include <libxml/encoding.h>
34 #include <libxml/xmlwriter.h>
41 using namespace libdap;
50 throw InternalErr(__FILE__, __LINE__,
"Error allocating the xml buffer");
52 xmlBufferSetAllocationScheme(d_doc_buf, XML_BUFFER_ALLOC_DOUBLEIT);
56 if (!(d_writer = xmlNewTextWriterMemory(d_doc_buf, 0)))
57 throw InternalErr(__FILE__, __LINE__,
"Error allocating memory for xml writer");
59 if (xmlTextWriterSetIndent(d_writer, pad.length()) < 0)
60 throw InternalErr(__FILE__, __LINE__,
"Error starting indentation for response document ");
62 if (xmlTextWriterSetIndentString(d_writer, (
const xmlChar*)pad.c_str()) < 0)
63 throw InternalErr(__FILE__, __LINE__,
"Error setting indentation for response document ");
71 if (xmlTextWriterStartDocument(d_writer, NULL,
ENCODING, NULL) < 0)
72 throw InternalErr(__FILE__, __LINE__,
"Error starting xml response document");
85 void XMLWriter::m_cleanup() {
88 xmlFreeTextWriter(d_writer);
95 xmlBufferFree(d_doc_buf);
104 if (d_writer && d_started) {
105 if (xmlTextWriterEndDocument(d_writer) < 0)
106 throw InternalErr(__FILE__, __LINE__,
"Error ending the document");
112 xmlFreeTextWriter(d_writer);
116 if (!d_doc_buf->content)
117 throw InternalErr(__FILE__, __LINE__,
"Error retrieving response document as string");
120 if (xmlTextWriterFlush(d_writer) < 0)
121 throw InternalErr(__FILE__, __LINE__,
"Error flushing the xml writer buffer");
124 return (
const char *)d_doc_buf->content;
128 if (d_writer && d_started) {
129 if (xmlTextWriterEndDocument(d_writer) < 0)
130 throw InternalErr(__FILE__, __LINE__,
"Error ending the document");
136 xmlFreeTextWriter(d_writer);
140 if (!d_doc_buf->content)
141 throw InternalErr(__FILE__, __LINE__,
"Error retrieving response document as string");
144 if (xmlTextWriterFlush(d_writer) < 0)
145 throw InternalErr(__FILE__, __LINE__,
"Error flushing the xml writer buffer");
149 return d_doc_buf->use;