libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
DDXParserSAX2.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2003 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#ifndef ddx_parser_h
27#define ddx_parser_h
28
29#include <string.h>
30#include <string>
31#include <map>
32#include <stack>
33
34#include <libxml/parserInternals.h>
35
36#ifndef ddx_exceptions_h
37#include "DDXExceptions.h"
38#endif
39
40#ifndef _dds_h
41#include "DDS.h"
42#endif
43
44#ifndef _basetype_h
45#include "BaseType.h"
46#endif
47
48#ifndef base_type_factory_h
49#include "BaseTypeFactory.h"
50#endif
51
52namespace libdap
53{
54
80class DDXParser
81{
82private:
85 enum ParseState {
86 parser_start,
87
88 inside_dataset,
89
90 inside_attribute_container,
91 inside_attribute,
92 inside_attribute_value,
93 inside_other_xml_attribute,
94
95 inside_alias,
96
97 // This covers Byte, ..., Url.
98 inside_simple_type,
99
100 inside_array,
101 inside_dimension,
102
103 inside_grid,
104 inside_map,
105
106 inside_structure,
107 inside_sequence,
108
109 inside_blob_href,
110
111 parser_unknown,
112 parser_error
113 };
114
115 xmlSAXHandler ddx_sax_parser;
116
117 BaseTypeFactory *d_factory;
118
119 // These stacks hold the state of the parse as it progresses.
120 stack<ParseState> s; // Current parse state
121 stack<BaseType*> bt_stack; // current variable(s)
122 stack<AttrTable*> at_stack; // current attribute table
123
124 // Accumulate stuff inside an 'OtherXML' DAP attribute here
125 string other_xml;
126
127 // When we're parsing unknown XML, how deeply is it nested? This is used
128 // for the OtherXML DAP attributes.
129 unsigned int other_xml_depth;
130 unsigned int unknown_depth;
131
132 // These are used for processing errors.
133 string error_msg; // Error message(s), if any.
134 xmlParserCtxtPtr ctxt; // used for error message line numbers
135
136 // The results of the parse operation are stored in these fields.
137 DDS *dds; // dump DDX here
138 string *blob_href; // put href to blob here
139
140 // These hold temporary values read during the parse.
141 string dods_attr_name; // DAP2 attributes, not XML attributes
142 string dods_attr_type; // ... not XML ...
143 string char_data; // char data in value elements; null after use
144 string root_ns; // What is the namespace of the root node (Dataset)
145
146 class XMLAttribute {
147 public:
148 string prefix;
149 string nsURI;
150 string value;
151
152 void clone(const XMLAttribute &src) {
153 prefix = src.prefix;
154 nsURI = src.nsURI;
155 value = src.value;
156 }
157
158 XMLAttribute() : prefix(""), nsURI(""), value("") {}
159 XMLAttribute(const string &p, const string &ns, const string &v)
160 : prefix(p), nsURI(ns), value(v) {}
161 // 'attributes' as passed from libxml2 is a five element array but this
162 // ctor gets the back four elements.
163 XMLAttribute(const xmlChar **attributes/*[4]*/) {
164 prefix = attributes[0] != 0 ? (const char *)attributes[0]: "";
165 nsURI = attributes[1] != 0 ? (const char *)attributes[1]: "";
166 value = string((const char *)attributes[2], (const char *)attributes[3]);
167 }
168 XMLAttribute(const XMLAttribute &rhs) {
169 clone(rhs);
170 }
171 ~XMLAttribute() {
172 }
173 XMLAttribute &operator=(const XMLAttribute &rhs) {
174 if (this == &rhs)
175 return *this;
176 clone(rhs);
177 return *this;
178 }
179 };
180
181 typedef map<string, XMLAttribute> XMLAttrMap;
182 XMLAttrMap attribute_table; // dump XML attributes here
183
184 XMLAttrMap::iterator attr_table_begin() {
185 return attribute_table.begin();
186 }
187
188 XMLAttrMap::iterator attr_table_end() {
189 return attribute_table.end();
190 }
191
192 map<string, string> namespace_table;
193
194 // These are kind of silly...
195 void set_state(DDXParser::ParseState state);
196 DDXParser::ParseState get_state() const;
197 void pop_state();
198
199 // Glue for the BaseTypeFactory class.
200 BaseType *factory(Type t, const string &name);
201
202 // Common cleanup code for intern() and intern_stream()
203#if 0
204 //void cleanup_parse(xmlParserCtxtPtr &context);
205#endif
206 void cleanup_parse();
207
214 void transfer_xml_attrs(const xmlChar **attrs, int nb_attributes);
215 void transfer_xml_ns(const xmlChar **namespaces, int nb_namespaces);
216 bool check_required_attribute(const string &attr);
217 bool check_attribute(const string & attr);
218
219 void process_attribute_element(const xmlChar **attrs, int nb_attrs);
220 void process_attribute_alias(const xmlChar **attrs, int nb_attrs);
221
222 void process_variable(Type t, ParseState s, const xmlChar **attrs,
223 int nb_attributes);
224
225 void process_dimension(const xmlChar **attrs, int nb_attrs);
226 void process_blob(const xmlChar **attrs, int nb_attrs);
227
228 bool is_attribute_or_alias(const char *name, const xmlChar **attrs,
229 int nb_attributes);
230 bool is_variable(const char *name, const xmlChar **attrs, int nb_attributes);
231
232 void finish_variable(const char *tag, Type t, const char *expected);
234
236 DDXParser();
237
238 friend class DDXParserTest;
239
240public:
241 DDXParser(BaseTypeFactory *factory)
242 : d_factory(factory),
243 other_xml(""), other_xml_depth(0), unknown_depth(0),
244 error_msg(""), ctxt(0), dds(0), blob_href(0),
245 dods_attr_name(""), dods_attr_type(""),
246 char_data(""), root_ns("")
247 {
248 memset( &ddx_sax_parser, 0, sizeof(xmlSAXHandler) );
249
250 ddx_sax_parser.getEntity = &DDXParser::ddx_get_entity;
251 ddx_sax_parser.startDocument = &DDXParser::ddx_start_document;
252 ddx_sax_parser.endDocument = &DDXParser::ddx_end_document;
253 ddx_sax_parser.characters = &DDXParser::ddx_get_characters;
254 ddx_sax_parser.ignorableWhitespace = &DDXParser::ddx_ignoreable_whitespace;
255 ddx_sax_parser.cdataBlock = &DDXParser::ddx_get_cdata;
256 ddx_sax_parser.warning = &DDXParser::ddx_fatal_error;
257 ddx_sax_parser.error = &DDXParser::ddx_fatal_error;
258 ddx_sax_parser.fatalError = &DDXParser::ddx_fatal_error;
259 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
260 ddx_sax_parser.startElementNs = &DDXParser::ddx_sax2_start_element;
261 ddx_sax_parser.endElementNs = &DDXParser::ddx_sax2_end_element;
262
263 }
264 void intern(const string &document, DDS *dest_dds, string &cid);
265 void intern_stream(FILE *in, DDS *dds, string &cid, const string &boundary = "");
266 void intern_stream(istream &in, DDS *dds, string &cid, const string &boundary = "");
267
268 static void ddx_start_document(void *parser);
269 static void ddx_end_document(void *parser);
270
271 static void ddx_sax2_start_element(void *parser,
272 const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
273 int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
274 int nb_defaulted, const xmlChar **attributes);
275 static void ddx_sax2_end_element(void *parser, const xmlChar *localname,
276 const xmlChar *prefix, const xmlChar *URI);
277
278 static void ddx_get_characters(void *parser, const xmlChar *ch, int len);
279 static void ddx_ignoreable_whitespace(void *parser,
280 const xmlChar * ch, int len);
281 static void ddx_get_cdata(void *parser, const xmlChar *value, int len);
282
283 static xmlEntityPtr ddx_get_entity(void *parser, const xmlChar *name);
284 static void ddx_fatal_error(void *parser, const char *msg, ...);
285};
286
287} // namespace libdap
288
289#endif // ddx_parser_h
The basic data type for the DODS DAP types.
Definition BaseType.h:120
static void ddx_fatal_error(void *parser, const char *msg,...)
static void ddx_ignoreable_whitespace(void *parser, const xmlChar *ch, int len)
static void ddx_get_characters(void *parser, const xmlChar *ch, int len)
static void ddx_start_document(void *parser)
void intern_stream(FILE *in, DDS *dds, string &cid, const string &boundary="")
Read the DDX from a stream instead of a file.
void intern(const string &document, DDS *dest_dds, string &cid)
static void ddx_end_document(void *parser)
static void ddx_get_cdata(void *parser, const xmlChar *value, int len)
static xmlEntityPtr ddx_get_entity(void *parser, const xmlChar *name)
top level DAP object to house generic methods
Definition AISConnect.cc:30
Type
Identifies the data type.
Definition Type.h:94