libdap  Updated for version 3.18.3
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  not_dap4_element,
112 
113  parser_unknown,
114  parser_error,
115  parser_fatal_error,
116 
117  parser_end
118  };
119 
120  xmlSAXHandler ddx_sax_parser;
121 
122  // The results of the parse operation are stored in these fields.
123  // This is passed into the parser using the intern() methods.
124  DMR *d_dmr; // dump DMR here
125  DMR *dmr() const { return d_dmr; }
126 
127  // These stacks hold the state of the parse as it progresses.
128  stack<ParseState> s; // Current parse state
129  void push_state(D4ParserSax2::ParseState state) { s.push(state); }
130  D4ParserSax2::ParseState get_state() const { return s.top(); }
131  void pop_state() { s.pop(); }
132  bool empty_state() const { return s.empty(); }
133 
134  stack<BaseType*> btp_stack; // current variable(s)
135  void push_basetype(BaseType *btp) { btp_stack.push(btp); }
136  BaseType *top_basetype() const { return btp_stack.top(); }
137  void pop_basetype() { btp_stack.pop(); }
138  bool empty_basetype() const { return btp_stack.empty(); }
139 
140  stack<D4Group*> grp_stack; // current groups(s)
141  void push_group(D4Group *grp) { grp_stack.push(grp); }
142  D4Group *top_group() const { return grp_stack.top(); }
143  void pop_group() { grp_stack.pop(); }
144  bool empty_group() const { return grp_stack.empty(); }
145 
146  stack<D4Attributes*> d_attrs_stack; // DAP4 Attributes
147  void push_attributes(D4Attributes *attr) { d_attrs_stack.push(attr); }
148  D4Attributes *top_attributes() const { return d_attrs_stack.top(); }
149  void pop_attributes() { d_attrs_stack.pop(); }
150  bool empty_attributes() const { return d_attrs_stack.empty(); }
151 
152  D4EnumDef *d_enum_def;
153  D4EnumDef *enum_def();
154  void clear_enum_def() { d_enum_def = 0; }
155 
156  D4Dimension *d_dim_def;
157  D4Dimension *dim_def();
158  void clear_dim_def() { d_dim_def = 0; }
159 
160  // Accumulate stuff inside an 'OtherXML' DAP attribute here
161  string other_xml;
162 
163  // When we're parsing unknown XML, how deeply is it nested? This is used
164  // for the OtherXML DAP attributes.
165  unsigned int other_xml_depth;
166  unsigned int unknown_depth;
167 
168  // These are used for processing errors.
169  string error_msg; // Error message(s), if any.
170  xmlParserCtxtPtr context; // used for error message line numbers
171 
172  // These hold temporary values read during the parse.
173  string dods_attr_name; // DAP4 attributes, not XML attributes
174  string dods_attr_type; // ... not XML ...
175  string char_data; // char data in value elements; null after use
176  string root_ns; // What is the namespace of the root node (Group)
177 
178  bool d_debug;
179  bool debug() const { return d_debug; }
180 
181  bool d_strict;
182 
183  class XMLAttribute {
184  public:
185  string prefix;
186  string nsURI;
187  string value;
188 
189  void clone(const XMLAttribute &src) {
190  prefix = src.prefix;
191  nsURI = src.nsURI;
192  value = src.value;
193  }
194 
195  XMLAttribute() : prefix(""), nsURI(""), value("") {}
196  XMLAttribute(const string &p, const string &ns, const string &v)
197  : prefix(p), nsURI(ns), value(v) {}
198  // 'attributes' as passed from libxml2 is a five element array but this
199  // ctor gets the back four elements.
200  XMLAttribute(const xmlChar **attributes/*[4]*/) {
201  prefix = attributes[0] != 0 ? (const char *)attributes[0]: "";
202  nsURI = attributes[1] != 0 ? (const char *)attributes[1]: "";
203  value = string((const char *)attributes[2], (const char *)attributes[3]);
204  }
205  XMLAttribute(const XMLAttribute &rhs) {
206  clone(rhs);
207  }
208  XMLAttribute &operator=(const XMLAttribute &rhs) {
209  if (this == &rhs)
210  return *this;
211  clone(rhs);
212  return *this;
213  }
214  };
215 
216  typedef map<string, XMLAttribute> XMLAttrMap;
217  XMLAttrMap xml_attrs; // dump XML attributes here
218 
219  XMLAttrMap::iterator xml_attr_begin() { return xml_attrs.begin(); }
220 
221  XMLAttrMap::iterator xml_attr_end() { return xml_attrs.end(); }
222 
223  map<string, string> namespace_table;
224 
225  void cleanup_parse();
226 
233  void transfer_xml_attrs(const xmlChar **attrs, int nb_attributes);
234  void transfer_xml_ns(const xmlChar **namespaces, int nb_namespaces);
235  bool check_required_attribute(const string &attr);
236  bool check_attribute(const string & attr);
237  void process_variable_helper(Type t, ParseState s, const xmlChar **attrs, int nb_attributes);
238 
239  void process_enum_const_helper(const xmlChar **attrs, int nb_attributes);
240  void process_enum_def_helper(const xmlChar **attrs, int nb_attributes);
241 
242  bool process_dimension(const char *name, const xmlChar **attrs, int nb_attrs);
243  bool process_dimension_def(const char *name, const xmlChar **attrs, int nb_attrs);
244  bool process_map(const char *name, const xmlChar **attrs, int nb_attributes);
245  bool process_attribute(const char *name, const xmlChar **attrs, int nb_attributes);
246  bool process_variable(const char *name, const xmlChar **attrs, int nb_attributes);
247  bool process_group(const char *name, const xmlChar **attrs, int nb_attributes);
248  bool process_enum_def(const char *name, const xmlChar **attrs, int nb_attributes);
249  bool process_enum_const(const char *name, const xmlChar **attrs, int nb_attributes);
250 
251  void finish_variable(const char *tag, Type t, const char *expected);
253 
254  friend class D4ParserSax2Test;
255 
256 public:
257  D4ParserSax2() :
258  d_dmr(0), d_enum_def(0), d_dim_def(0),
259  other_xml(""), other_xml_depth(0), unknown_depth(0),
260  error_msg(""), context(0),
261  dods_attr_name(""), dods_attr_type(""),
262  char_data(""), root_ns(""), d_debug(false), d_strict(true)
263  {
264  //xmlSAXHandler ddx_sax_parser;
265  memset(&ddx_sax_parser, 0, sizeof(xmlSAXHandler));
266 
267  ddx_sax_parser.getEntity = &D4ParserSax2::dmr_get_entity;
268  ddx_sax_parser.startDocument = &D4ParserSax2::dmr_start_document;
269  ddx_sax_parser.endDocument = &D4ParserSax2::dmr_end_document;
270  ddx_sax_parser.characters = &D4ParserSax2::dmr_get_characters;
271  ddx_sax_parser.ignorableWhitespace = &D4ParserSax2::dmr_ignoreable_whitespace;
272  ddx_sax_parser.cdataBlock = &D4ParserSax2::dmr_get_cdata;
273  ddx_sax_parser.warning = &D4ParserSax2::dmr_error;
274  ddx_sax_parser.error = &D4ParserSax2::dmr_error;
275  ddx_sax_parser.fatalError = &D4ParserSax2::dmr_fatal_error;
276  ddx_sax_parser.initialized = XML_SAX2_MAGIC;
277  ddx_sax_parser.startElementNs = &D4ParserSax2::dmr_start_element;
278  ddx_sax_parser.endElementNs = &D4ParserSax2::dmr_end_element;
279  }
280 
281  void intern(istream &f, DMR *dest_dmr, bool debug = false);
282  void intern(const string &document, DMR *dest_dmr, bool debug = false);
283  void intern(const char *buffer, int size, DMR *dest_dmr, bool debug = false);
284 
297  void set_strict(bool s) { d_strict = s; }
301  bool get_strict() const { return d_strict; }
304  static void dmr_start_document(void *parser);
305  static void dmr_end_document(void *parser);
306 
307  static void dmr_start_element(void *parser,
308  const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
309  int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
310  int nb_defaulted, const xmlChar **attributes);
311  static void dmr_end_element(void *parser, const xmlChar *localname,
312  const xmlChar *prefix, const xmlChar *URI);
313 
314  static void dmr_get_characters(void *parser, const xmlChar *ch, int len);
315  static void dmr_ignoreable_whitespace(void *parser,
316  const xmlChar * ch, int len);
317  static void dmr_get_cdata(void *parser, const xmlChar *value, int len);
318 
319  static xmlEntityPtr dmr_get_entity(void *parser, const xmlChar *name);
320  static void dmr_fatal_error(void *parser, const char *msg, ...);
321  static void dmr_error(void *parser, const char *msg, ...);
322 };
323 
324 } // namespace libdap
325 
326 #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)
void set_strict(bool s)
Set the &#39;strict&#39; mode to true or false.
Definition: D4ParserSax2.h:297
Type
Identifies the data type.
Definition: Type.h:94
static void dmr_start_element(void *parser, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes)
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)
bool get_strict() const
Get the setting of the &#39;strict&#39; mode.
Definition: D4ParserSax2.h:301
The basic data type for the DODS DAP types.
Definition: BaseType.h:117
static void dmr_fatal_error(void *parser, const char *msg,...)