33#include <libxml/encoding.h>
34#include <libxml/xmlwriter.h>
36#include "InternalErr.h"
41const char *ENCODING =
"ISO-8859-1";
42const int XML_BUF_SIZE = 2000000;
46XMLWriter::XMLWriter(
const string &pad) {
54 if (!(d_doc_buf = xmlBufferCreateSize(XML_BUF_SIZE)))
55 throw InternalErr(__FILE__, __LINE__,
"Error allocating the xml buffer");
57 xmlBufferSetAllocationScheme(d_doc_buf, XML_BUFFER_ALLOC_DOUBLEIT);
61 if (!(d_writer = xmlNewTextWriterMemory(d_doc_buf, 0)))
62 throw InternalErr(__FILE__, __LINE__,
"Error allocating memory for xml writer");
64 if (xmlTextWriterSetIndent(d_writer, pad.length()) < 0)
65 throw InternalErr(__FILE__, __LINE__,
"Error starting indentation for response document ");
67 if (xmlTextWriterSetIndentString(d_writer, (
const xmlChar *)pad.c_str()) < 0)
68 throw InternalErr(__FILE__, __LINE__,
"Error setting indentation for response document ");
76 if (xmlTextWriterStartDocument(d_writer, NULL, ENCODING, NULL) < 0)
77 throw InternalErr(__FILE__, __LINE__,
"Error starting xml response document");
78 }
catch (InternalErr &e) {
84XMLWriter::~XMLWriter() { m_cleanup(); }
86void XMLWriter::m_cleanup() {
89 xmlFreeTextWriter(d_writer);
96 xmlBufferFree(d_doc_buf);
104const char *XMLWriter::get_doc() {
105 if (d_writer && d_started) {
106 if (xmlTextWriterEndDocument(d_writer) < 0)
107 throw InternalErr(__FILE__, __LINE__,
"Error ending the document");
113 xmlFreeTextWriter(d_writer);
117 if (!d_doc_buf->content)
118 throw InternalErr(__FILE__, __LINE__,
"Error retrieving response document as string");
120 return (
const char *)d_doc_buf->content;
123unsigned int XMLWriter::get_doc_size() {
124 if (d_writer && d_started) {
125 if (xmlTextWriterEndDocument(d_writer) < 0)
126 throw InternalErr(__FILE__, __LINE__,
"Error ending the document");
132 xmlFreeTextWriter(d_writer);
136 if (!d_doc_buf->content)
137 throw InternalErr(__FILE__, __LINE__,
"Error retrieving response document as string");
140 return d_doc_buf->use;
top level DAP object to house generic methods