bes  Updated for version 3.20.6
get_xml_data.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // Copyright (c) 2006 OPeNDAP, Inc.
4 // Author: James Gallagher <jgallagher@opendap.org>
5 //
6 // This is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your
9 // option) any later version.
10 //
11 // This is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
21 
22 // This file holds the interface for the 'get data as ascii' function of the
23 // OPeNDAP/HAO data server. This function is called by the BES when it loads
24 // this as a module. The functions in the file ascii_val.cc also use this, so
25 // the same basic processing software can be used both by Hyrax and tie older
26 // Server3.
27 
28 #include <stdio.h>
29 
30 #include <iostream>
31 
32 using std::cerr;
33 using std::endl;
34 
35 #include <DDS.h>
36 
37 #include <BESDebug.h>
38 
39 #include "get_xml_data.h"
40 #include "XDOutput.h"
41 
42 #include "XDByte.h"
43 #include "XDInt16.h"
44 #include "XDUInt16.h"
45 #include "XDInt32.h"
46 #include "XDUInt32.h"
47 #include "XDFloat32.h"
48 #include "XDFloat64.h"
49 #include "XDStr.h"
50 #include "XDUrl.h"
51 #include "XDArray.h"
52 #include "XDStructure.h"
53 #include "XDSequence.h"
54 #include "XDGrid.h"
55 
56 const char *DAP_SCHEMA = "http://xml.opendap.org/ns/dap/3.3#";
57 
58 using namespace libdap;
59 
60 namespace xml_data {
61 
69 void get_data_values_as_xml(DDS *dds, XMLWriter *writer)
70 {
71  try {
72  /* Start an element named "Dataset". Since this is the first element,
73  * this will be the root element of the document */
74  if (xmlTextWriterStartElementNS(writer->get_writer(), NULL, (const xmlChar*)"Dataset", (const xmlChar*)DAP_SCHEMA) < 0)
75  throw InternalErr(__FILE__, __LINE__, "Error starting the Dataset element for response ");
76 
77  DDS::Vars_iter i = dds->var_begin();
78  while (i != dds->var_end()) {
79  if ((*i)->send_p()) {
80  BESDEBUG("xd", "Printing the values for " << (*i)->name() << " (" << (*i)->type_name() << ")" << endl);
81  dynamic_cast<XDOutput &>(**i).print_xml_data(writer, true);
82  }
83  ++i;
84  }
85 
86  if (xmlTextWriterEndElement(writer->get_writer()) < 0)
87  throw InternalErr(__FILE__, __LINE__, "Error ending Dataset element.");
88 
89  }
90  catch (InternalErr &e) {
91  xmlErrorPtr error = xmlGetLastError();
92  if (error && error->message)
93  throw InternalErr(e.get_error_message() + "; libxml: " + error->message);
94  else
95  throw InternalErr(e.get_error_message() + "; libxml: no message");
96  }
97 }
98 
99 DDS *dds_to_xd_dds(DDS * dds)
100 {
101  BESDEBUG("xd", "In datadds_to_xd_datadds" << endl);
102  // Should the following use XDOutputFactory instead of the source DDS'
103  // factory class? It doesn't matter for the following since the function
104  // basetype_to_asciitype() doesn't use the factory. So long as no other
105  // code uses the DDS' factory, this is fine. jhrg 9/5/06
106  DDS *xd_dds = new DDS(dds->get_factory(), dds->get_dataset_name());
107 
108  DDS::Vars_iter i = dds->var_begin();
109  while (i != dds->var_end()) {
110  BaseType *abt = basetype_to_xd(*i);
111  xd_dds->add_var(abt);
112  // add_var makes a copy of the base type passed to it, so delete
113  // it here
114  delete abt;
115  ++i;
116  }
117 
118  // Calling tag_nested_sequences() makes it easier to figure out if a
119  // sequence has parent or child sequences or if it is a 'flat' sequence.
120  xd_dds->tag_nested_sequences();
121 
122  return xd_dds;
123 }
124 
125 BaseType *
126 basetype_to_xd(BaseType *bt)
127 {
128  if (!bt)
129  throw InternalErr(__FILE__, __LINE__, "Null BaseType to XD factory");
130 
131  switch (bt->type()) {
132  case dods_byte_c:
133  return new XDByte(dynamic_cast<Byte *>(bt));
134 
135  case dods_int16_c:
136  return new XDInt16(dynamic_cast<Int16 *>(bt));
137 
138  case dods_uint16_c:
139  return new XDUInt16(dynamic_cast<UInt16 *>(bt));
140 
141  case dods_int32_c:
142  return new XDInt32(dynamic_cast<Int32 *>(bt));
143 
144  case dods_uint32_c:
145  return new XDUInt32(dynamic_cast<UInt32 *>(bt));
146 
147  case dods_float32_c:
148  return new XDFloat32(dynamic_cast<Float32 *>(bt));
149 
150  case dods_float64_c:
151  return new XDFloat64(dynamic_cast<Float64 *>(bt));
152 
153  case dods_str_c:
154  return new XDStr(dynamic_cast<Str *>(bt));
155 
156  case dods_url_c:
157  return new XDUrl(dynamic_cast<Url *>(bt));
158 
159  case dods_array_c:
160  return new XDArray(dynamic_cast<Array *>(bt));
161 
162  case dods_structure_c:
163  return new XDStructure(dynamic_cast<Structure *>(bt));
164 
165  case dods_sequence_c:
166  return new XDSequence(dynamic_cast<Sequence *>(bt));
167 
168  case dods_grid_c:
169  return new XDGrid(dynamic_cast<Grid *>(bt));
170 
171  default:
172  throw InternalErr(__FILE__, __LINE__, "Unknown type");
173  }
174 }
175 
176 } // namespace xml_data
XDUInt16
Definition: XDUInt16.h:39
XDInt32
Definition: XDInt32.h:39
XDSequence
Definition: XDSequence.h:39
XDByte
Definition: XDByte.h:35
XDArray
Definition: XDArray.h:42
XDGrid
Definition: XDGrid.h:39
XDFloat32
Definition: XDFloat32.h:39
libdap
Definition: BESDapFunctionResponseCache.h:35
XDFloat64
Definition: XDFloat64.h:39
XDStr
Definition: XDStr.h:39
XDStructure
Definition: XDStructure.h:39
XDInt16
Definition: XDInt16.h:39
XDUInt32
Definition: XDUInt32.h:39
XDOutput
Definition: XDOutput.h:42
XDUrl
Definition: XDUrl.h:55