libdap  Updated for version 3.17.2
D4ParserSax2.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) 2012 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 d4_parser_sax2_h
27 #define d4_parser_sax2_h
28 
29 #define ATTR 1
30 
31 #include <string.h>
32 
33 #include <string>
34 #include <iostream>
35 #include <map>
36 #include <stack>
37 
38 #include <libxml/parserInternals.h>
39 
40 #define CRLF "\r\n"
41 
42 namespace libdap
43 {
44 
45 class DMR;
46 class BaseType;
47 class D4BaseTypeFactory;
48 class D4Group;
49 class D4Attributes;
50 class D4EnumDef;
51 class D4Dimension;
52 
77 {
78 private:
81  enum ParseState {
82  parser_start,
83 
84  inside_dataset,
85 
86  // inside_group is the state just after parsing the start of a Group
87  // element.
88  inside_group,
89 
90  inside_attribute_container,
91  inside_attribute,
92  inside_attribute_value,
93  inside_other_xml_attribute,
94 
95  inside_enum_def,
96  inside_enum_const,
97 
98  inside_dim_def,
99 
100  // This covers Byte, ..., Url, Opaque
101  inside_simple_type,
102 
103  // inside_array,
104  inside_dim,
105  inside_map,
106 
107  inside_constructor,
108 
109  // inside_sequence, Removed from merged code jhrg 5/2/14
110 
111  parser_unknown,
112  parser_error,
113  parser_fatal_error,
114 
115  parser_end
116  };
117 
118  xmlSAXHandler ddx_sax_parser;
119 
120  // The results of the parse operation are stored in these fields.
121  // This is passed into the parser using the intern() methods.
122  DMR *d_dmr; // dump DMR here
123  DMR *dmr() const { return d_dmr; }
124 
125  // These stacks hold the state of the parse as it progresses.
126  stack<ParseState> s; // Current parse state
127  void push_state(D4ParserSax2::ParseState state) { s.push(state); }
128  D4ParserSax2::ParseState get_state() const { return s.top(); }
129  void pop_state() { s.pop(); }
130  bool empty_state() const { return s.empty(); }
131 
132  stack<BaseType*> btp_stack; // current variable(s)
133  void push_basetype(BaseType *btp) { btp_stack.push(btp); }
134  BaseType *top_basetype() const { return btp_stack.top(); }
135  void pop_basetype() { btp_stack.pop(); }
136  bool empty_basetype() const { return btp_stack.empty(); }
137 
138  stack<D4Group*> grp_stack; // current groups(s)
139  void push_group(D4Group *grp) { grp_stack.push(grp); }
140  D4Group *top_group() const { return grp_stack.top(); }
141  void pop_group() { grp_stack.pop(); }
142  bool empty_group() const { return grp_stack.empty(); }
143 
144  stack<D4Attributes*> d_attrs_stack; // DAP4 Attributes
145  void push_attributes(D4Attributes *attr) { d_attrs_stack.push(attr); }
146  D4Attributes *top_attributes() const { return d_attrs_stack.top(); }
147  void pop_attributes() { d_attrs_stack.pop(); }
148  bool empty_attributes() const { return d_attrs_stack.empty(); }
149 
150  D4EnumDef *d_enum_def;
151  D4EnumDef *enum_def();
152 #if 0
153  {
154  if (!d_enum_def) d_enum_def = new D4EnumDef;
155  return d_enum_def;
156  }
157 #endif
158  void clear_enum_def() { d_enum_def = 0; }
159 
160  D4Dimension *d_dim_def;
161  D4Dimension *dim_def();
162 #if 0
163  {
164  if (!d_dim_def) d_dim_def = new D4Dimension;
165  return d_dim_def;
166  }
167 #endif
168  void clear_dim_def() { d_dim_def = 0; }
169 
170  // Accumulate stuff inside an 'OtherXML' DAP attribute here
171  string other_xml;
172 
173  // When we're parsing unknown XML, how deeply is it nested? This is used
174  // for the OtherXML DAP attributes.
175  unsigned int other_xml_depth;
176  unsigned int unknown_depth;
177 
178  // These are used for processing errors.
179  string error_msg; // Error message(s), if any.
180  xmlParserCtxtPtr context; // used for error message line numbers
181 
182  // These hold temporary values read during the parse.
183  string dods_attr_name; // DAP4 attributes, not XML attributes
184  string dods_attr_type; // ... not XML ...
185  string char_data; // char data in value elements; null after use
186  string root_ns; // What is the namespace of the root node (Group)
187 
188  bool d_debug;
189  bool debug() const { return d_debug; }
190 
191  class XMLAttribute {
192  public:
193  string prefix;
194  string nsURI;
195  string value;
196 
197  void clone(const XMLAttribute &src) {
198  prefix = src.prefix;
199  nsURI = src.nsURI;
200  value = src.value;
201  }
202 
203  XMLAttribute() : prefix(""), nsURI(""), value("") {}
204  XMLAttribute(const string &p, const string &ns, const string &v)
205  : prefix(p), nsURI(ns), value(v) {}
206  // 'attributes' as passed from libxml2 is a five element array but this
207  // ctor gets the back four elements.
208  XMLAttribute(const xmlChar **attributes/*[4]*/) {
209  prefix = attributes[0] != 0 ? (const char *)attributes[0]: "";
210  nsURI = attributes[1] != 0 ? (const char *)attributes[1]: "";
211  value = string((const char *)attributes[2], (const char *)attributes[3]);
212  }
213  XMLAttribute(const XMLAttribute &rhs) {
214  clone(rhs);
215  }
216  XMLAttribute &operator=(const XMLAttribute &rhs) {
217  if (this == &rhs)
218  return *this;
219  clone(rhs);
220  return *this;
221  }
222  };
223 
224  typedef map<string, XMLAttribute> XMLAttrMap;
225  XMLAttrMap xml_attrs; // dump XML attributes here
226 
227  XMLAttrMap::iterator xml_attr_begin() { return xml_attrs.begin(); }
228 
229  XMLAttrMap::iterator xml_attr_end() { return xml_attrs.end(); }
230 
231  map<string, string> namespace_table;
232 
233  void cleanup_parse();
234 
241  void transfer_xml_attrs(const xmlChar **attrs, int nb_attributes);
242  void transfer_xml_ns(const xmlChar **namespaces, int nb_namespaces);
243  bool check_required_attribute(const string &attr);
244  bool check_attribute(const string & attr);
245  void process_variable_helper(Type t, ParseState s, const xmlChar **attrs, int nb_attributes);
246 
247  void process_enum_const_helper(const xmlChar **attrs, int nb_attributes);
248  void process_enum_def_helper(const xmlChar **attrs, int nb_attributes);
249 
250  bool process_dimension(const char *name, const xmlChar **attrs, int nb_attrs);
251  bool process_dimension_def(const char *name, const xmlChar **attrs, int nb_attrs);
252  bool process_map(const char *name, const xmlChar **attrs, int nb_attributes);
253  bool process_attribute(const char *name, const xmlChar **attrs, int nb_attributes);
254  bool process_variable(const char *name, const xmlChar **attrs, int nb_attributes);
255  bool process_group(const char *name, const xmlChar **attrs, int nb_attributes);
256  bool process_enum_def(const char *name, const xmlChar **attrs, int nb_attributes);
257  bool process_enum_const(const char *name, const xmlChar **attrs, int nb_attributes);
258 
259  void finish_variable(const char *tag, Type t, const char *expected);
261 
262  friend class D4ParserSax2Test;
263 
264 public:
265  D4ParserSax2() :
266  d_dmr(0), d_enum_def(0), d_dim_def(0),
267  other_xml(""), other_xml_depth(0), unknown_depth(0),
268  error_msg(""), context(0),
269  dods_attr_name(""), dods_attr_type(""),
270  char_data(""), root_ns(""), d_debug(false)
271  {
272  //xmlSAXHandler ddx_sax_parser;
273  memset(&ddx_sax_parser, 0, sizeof(xmlSAXHandler));
274 
275  ddx_sax_parser.getEntity = &D4ParserSax2::dmr_get_entity;
276  ddx_sax_parser.startDocument = &D4ParserSax2::dmr_start_document;
277  ddx_sax_parser.endDocument = &D4ParserSax2::dmr_end_document;
278  ddx_sax_parser.characters = &D4ParserSax2::dmr_get_characters;
279  ddx_sax_parser.ignorableWhitespace = &D4ParserSax2::dmr_ignoreable_whitespace;
280  ddx_sax_parser.cdataBlock = &D4ParserSax2::dmr_get_cdata;
281  ddx_sax_parser.warning = &D4ParserSax2::dmr_error;
282  ddx_sax_parser.error = &D4ParserSax2::dmr_error;
283  ddx_sax_parser.fatalError = &D4ParserSax2::dmr_fatal_error;
284  ddx_sax_parser.initialized = XML_SAX2_MAGIC;
285  ddx_sax_parser.startElementNs = &D4ParserSax2::dmr_start_element;
286  ddx_sax_parser.endElementNs = &D4ParserSax2::dmr_end_element;
287  }
288 
289  void intern(istream &f, DMR *dest_dmr, bool debug = false);
290  void intern(const string &document, DMR *dest_dmr, bool debug = false);
291  void intern(const char *buffer, int size, DMR *dest_dmr, bool debug = false);
292 
293  static void dmr_start_document(void *parser);
294  static void dmr_end_document(void *parser);
295 
296  static void dmr_start_element(void *parser,
297  const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
298  int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
299  int nb_defaulted, const xmlChar **attributes);
300  static void dmr_end_element(void *parser, const xmlChar *localname,
301  const xmlChar *prefix, const xmlChar *URI);
302 
303  static void dmr_get_characters(void *parser, const xmlChar *ch, int len);
304  static void dmr_ignoreable_whitespace(void *parser,
305  const xmlChar * ch, int len);
306  static void dmr_get_cdata(void *parser, const xmlChar *value, int len);
307 
308  static xmlEntityPtr dmr_get_entity(void *parser, const xmlChar *name);
309  static void dmr_fatal_error(void *parser, const char *msg, ...);
310  static void dmr_error(void *parser, const char *msg, ...);
311 };
312 
313 } // namespace libdap
314 
315 #endif // d4_parser_sax2_h
static void dmr_end_document(void *parser)
static void dmr_start_document(void *parser)
static xmlEntityPtr dmr_get_entity(void *parser, const xmlChar *name)
Type
Identifies the data type.
Definition: Type.h:94
static void dmr_ignoreable_whitespace(void *parser, const xmlChar *ch, int len)
static void dmr_get_cdata(void *parser, const xmlChar *value, int len)
static void dmr_get_characters(void *parser, const xmlChar *ch, int len)
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
static void dmr_fatal_error(void *parser, const char *msg,...)