libdap  Updated for version 3.20.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
XMLWriter.cc
1 
2 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
3 // Access Protocol.
4 
5 // Copyright (c) 2010 OPeNDAP, Inc.
6 // Author: James Gallagher <jgallagher@opendap.org>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 //
22 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23 
24 /*
25  * XMLWriter.cpp
26  *
27  * Created on: Jul 28, 2010
28  * Author: jimg
29  */
30 
31 #include "config.h"
32 
33 #include <libxml/encoding.h>
34 #include <libxml/xmlwriter.h>
35 
36 #include "XMLWriter.h"
37 #include "InternalErr.h"
38 
39 // TODO - Bite the bullet and make the encoding UTF-8 as required by dap4. This will break a lot of tests but the baselines could be amended using a bash script and sed.
40 const char *ENCODING = "ISO-8859-1";
41 const int XML_BUF_SIZE = 2000000;
42 
43 using namespace libdap;
44 
45 XMLWriter::XMLWriter(const string &pad) {
46  LIBXML_TEST_VERSION;
47 
48  /* Create a new XML buffer, to which the XML document will be
49  * written */
50  try {
51  if (!(d_doc_buf = xmlBufferCreateSize(XML_BUF_SIZE)))
52  throw InternalErr(__FILE__, __LINE__, "Error allocating the xml buffer");
53 
54  xmlBufferSetAllocationScheme(d_doc_buf, XML_BUFFER_ALLOC_DOUBLEIT);
55 
56  /* Create a new XmlWriter for memory, with no compression.
57  * Remark: there is no compression for this kind of xmlTextWriter */
58  if (!(d_writer = xmlNewTextWriterMemory(d_doc_buf, 0)))
59  throw InternalErr(__FILE__, __LINE__, "Error allocating memory for xml writer");
60 
61  if (xmlTextWriterSetIndent(d_writer, pad.length()) < 0)
62  throw InternalErr(__FILE__, __LINE__, "Error starting indentation for response document ");
63 
64  if (xmlTextWriterSetIndentString(d_writer, (const xmlChar*)pad.c_str()) < 0)
65  throw InternalErr(__FILE__, __LINE__, "Error setting indentation for response document ");
66 
67  d_started = true;
68  d_ended = false;
69 
70  /* Start the document with the xml default for the version,
71  * encoding ISO 8859-1 and the default for the standalone
72  * declaration. MY_ENCODING defined at top of this file*/
73  if (xmlTextWriterStartDocument(d_writer, NULL, ENCODING, NULL) < 0)
74  throw InternalErr(__FILE__, __LINE__, "Error starting xml response document");
75  }
76  catch (InternalErr &e) {
77  m_cleanup();
78  throw;
79  }
80 
81 }
82 
83 XMLWriter::~XMLWriter() {
84  m_cleanup();
85 }
86 
87 void XMLWriter::m_cleanup() {
88  // make sure the buffer and writer are all cleaned up
89  if (d_writer) {
90  xmlFreeTextWriter(d_writer); // This frees both d_writer and d_doc_buf
91  d_writer = 0;
92  // d_doc_buf = 0;
93  }
94 
95  // We could be here because of an exception and d_writer might be zero
96  if (d_doc_buf) {
97  xmlBufferFree(d_doc_buf);
98  d_doc_buf = 0;
99  }
100 
101  d_started = false;
102  d_ended = false;
103 }
104 
105 const char *XMLWriter::get_doc() {
106  if (d_writer && d_started) {
107  if (xmlTextWriterEndDocument(d_writer) < 0)
108  throw InternalErr(__FILE__, __LINE__, "Error ending the document");
109 
110  d_ended = true;
111 
112  // must call this before getting the buffer content. Odd, but appears to be true.
113  // jhrg
114  xmlFreeTextWriter(d_writer);
115  d_writer = 0;
116  }
117 
118  if (!d_doc_buf->content)
119  throw InternalErr(__FILE__, __LINE__, "Error retrieving response document as string");
120 
121  return (const char *)d_doc_buf->content;
122 }
123 
124 unsigned int XMLWriter::get_doc_size() {
125  if (d_writer && d_started) {
126  if (xmlTextWriterEndDocument(d_writer) < 0)
127  throw InternalErr(__FILE__, __LINE__, "Error ending the document");
128 
129  d_ended = true;
130 
131  // must call this before getting the buffer content. Odd, but appears to be true.
132  // jhrg
133  xmlFreeTextWriter(d_writer);
134  d_writer = 0;
135  }
136 
137  if (!d_doc_buf->content)
138  throw InternalErr(__FILE__, __LINE__, "Error retrieving response document as string");
139 
140  // how much of the buffer is in use?
141  return d_doc_buf->use;
142 }
top level DAP object to house generic methods
Definition: AlarmHandler.h:35
A class for software fault reporting.
Definition: InternalErr.h:64