33 #include <libxml/encoding.h> 34 #include <libxml/xmlwriter.h> 36 #include "XMLWriter.h" 37 #include "InternalErr.h" 39 const char *ENCODING =
"ISO-8859-1";
40 const int XML_BUF_SIZE = 2000000;
44 XMLWriter::XMLWriter(
const string &pad) {
50 if (!(d_doc_buf = xmlBufferCreateSize(XML_BUF_SIZE)))
51 throw InternalErr(__FILE__, __LINE__,
"Error allocating the xml buffer");
53 xmlBufferSetAllocationScheme(d_doc_buf, XML_BUFFER_ALLOC_DOUBLEIT);
57 if (!(d_writer = xmlNewTextWriterMemory(d_doc_buf, 0)))
58 throw InternalErr(__FILE__, __LINE__,
"Error allocating memory for xml writer");
60 if (xmlTextWriterSetIndent(d_writer, pad.length()) < 0)
61 throw InternalErr(__FILE__, __LINE__,
"Error starting indentation for response document ");
63 if (xmlTextWriterSetIndentString(d_writer, (
const xmlChar*)pad.c_str()) < 0)
64 throw InternalErr(__FILE__, __LINE__,
"Error setting indentation for response document ");
72 if (xmlTextWriterStartDocument(d_writer, NULL, ENCODING, NULL) < 0)
73 throw InternalErr(__FILE__, __LINE__,
"Error starting xml response document");
82 XMLWriter::~XMLWriter() {
86 void XMLWriter::m_cleanup() {
89 xmlFreeTextWriter(d_writer);
96 xmlBufferFree(d_doc_buf);
104 const 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;
123 unsigned 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;
A class for software fault reporting.