bes  Updated for version 3.20.8
DmrppParserSax2.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 dmrpp_parser_sax2_h
27 #define dmrpp_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 <unordered_map>
37 #include <stack>
38 
39 #include <libxml/parserInternals.h>
40 
41 #include <Type.h> // from libdap
42 #include "BESRegex.h"
43 #include "EffectiveUrlCache.h"
44 
45 #define CRLF "\r\n"
46 #define D4_PARSE_BUFF_SIZE 1048576
47 
48 namespace libdap {
49 class DMR;
50 class BaseType;
51 class D4BaseTypeFactory;
52 class D4Group;
53 class D4Attributes;
54 class D4EnumDef;
55 class D4Dimension;
56 }
57 
58 namespace dmrpp {
65 {
66 private:
69  enum ParseState {
70  parser_start,
71 
72  inside_dataset,
73 
74  // inside_group is the state just after parsing the start of a Group
75  // element.
76  inside_group,
77 
78  inside_attribute_container,
79  inside_attribute,
80  inside_attribute_value,
81  inside_other_xml_attribute,
82 
83  inside_enum_def,
84  inside_enum_const,
85 
86  inside_dim_def,
87 
88  // This covers Byte, ..., Url, Opaque
89  inside_simple_type,
90 
91  // inside_array,
92  inside_dim,
93  inside_map,
94 
95  inside_constructor,
96 
97  // inside_sequence, Removed from merged code jhrg 5/2/14
98 
99  // FIXMEinside_dmrpp_byte_stream,
100  not_dap4_element,
101  inside_dmrpp_object,
102  inside_dmrpp_chunkDimensionSizes_element,
103  inside_dmrpp_compact_element,
104 
105  parser_unknown,
106  parser_error,
107  parser_fatal_error,
108 
109  parser_end
110  };
111 
112  char d_parse_buffer[D4_PARSE_BUFF_SIZE+1]; // Buff size plus one byte for NULL termination.
113 
114  xmlSAXHandler dmrpp_sax_parser;
115 
116  // The results of the parse operation are stored in these fields.
117  // This is passed into the parser using the intern() methods.
118  libdap::DMR *d_dmr; // dump DMR here
119  libdap::DMR *dmr() const { return d_dmr; }
120 
121 
122 
123 
124 
125  // These stacks hold the state of the parse as it progresses.
126  std::stack<ParseState> s; // Current parse state
127  void push_state(DmrppParserSax2::ParseState state) { s.push(state); }
128  DmrppParserSax2::ParseState get_state() const { return s.top(); }
129  void pop_state() { s.pop(); }
130  bool empty_state() const { return s.empty(); }
131 
132  std::stack<libdap::BaseType*> btp_stack; // current variable(s)
133  void push_basetype(libdap::BaseType *btp) { btp_stack.push(btp); }
134  libdap::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  std::stack<libdap::D4Group*> grp_stack; // current groups(s)
139  void push_group(libdap::D4Group *grp) { grp_stack.push(grp); }
140  libdap::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  std::stack<libdap::D4Attributes*> d_attrs_stack; // DAP4 Attributes
145  void push_attributes(libdap::D4Attributes *attr) { d_attrs_stack.push(attr); }
146  libdap::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  libdap::D4EnumDef *d_enum_def;
151  libdap::D4EnumDef *enum_def();
152  void clear_enum_def() { d_enum_def = 0; }
153 
154  libdap::D4Dimension *d_dim_def;
155  libdap::D4Dimension *dim_def();
156  void clear_dim_def() { d_dim_def = 0; }
157 
158  // Accumulate stuff inside an 'OtherXML' DAP attribute here
159  std::string other_xml;
160 
161  // When we're parsing unknown XML, how deeply is it nested? This is used
162  // for the OtherXML DAP attributes.
163  unsigned int other_xml_depth;
164  unsigned int unknown_depth;
165 
166  // These are used for processing errors.
167  std::string error_msg; // Error message(s), if any.
168  xmlParserCtxtPtr context; // used for error message line numbers
169 
170  // These hold temporary values read during the parse.
171  std::string dods_attr_name; // DAP4 attributes, not XML attributes
172  std::string dods_attr_type; // ... not XML ...
173  std::string char_data; // char data in value elements; null after use
174  std::string root_ns; // What is the namespace of the root node (Group)
175 
176 
177  bool d_strict;
178 
179  std::string dmrpp_dataset_href;
180 
181  class XMLAttribute {
182  public:
183  std::string prefix;
184  std::string nsURI;
185  std::string value;
186 
187  void clone(const XMLAttribute &src) {
188  prefix = src.prefix;
189  nsURI = src.nsURI;
190  value = src.value;
191  }
192 
193  XMLAttribute() : prefix(""), nsURI(""), value("") {}
194  XMLAttribute(const std::string &p, const std::string &ns, const std::string &v)
195  : prefix(p), nsURI(ns), value(v) {}
196  // 'attributes' as passed from libxml2 is a five element array but this
197  // ctor gets the back four elements.
198  XMLAttribute(const xmlChar **attributes/*[4]*/) {
199  prefix = attributes[0] != 0 ? (const char *)attributes[0]: "";
200  nsURI = attributes[1] != 0 ? (const char *)attributes[1]: "";
201  value = std::string((const char *)attributes[2], (const char *)attributes[3]);
202  }
203  XMLAttribute(const XMLAttribute &rhs) {
204  clone(rhs);
205  }
206  XMLAttribute &operator=(const XMLAttribute &rhs) {
207  if (this == &rhs)
208  return *this;
209  clone(rhs);
210  return *this;
211  }
212  };
213 
214  typedef std::unordered_map<std::string, XMLAttribute> XMLAttrMap;
215  XMLAttrMap xml_attrs; // dump XML attributes here
216 
217  XMLAttrMap::iterator xml_attr_begin() { return xml_attrs.begin(); }
218 
219  XMLAttrMap::iterator xml_attr_end() { return xml_attrs.end(); }
220 
221  std::map<std::string, std::string> namespace_table;
222 
223  void cleanup_parse();
224 
231 #if 0
232  void transfer_xml_attrs(const xmlChar **attrs, int nb_attributes);
233 #endif
234  std::string get_attribute_val(const std::string &name, const xmlChar **attributes, int num_attributes);
235  void transfer_xml_ns(const xmlChar **namespaces, int nb_namespaces);
236  bool check_required_attribute(const std::string &attr);
237  bool check_required_attribute(const std::string &attr, const xmlChar **attributes, int num_attributes);
238  bool check_attribute(const std::string & attr);
239  bool check_attribute(const std::string &name, const xmlChar **attributes, int num_attributes);
240  void process_variable_helper(libdap::Type t, ParseState s, const xmlChar **attrs, int nb_attributes);
241 
242  void process_enum_const_helper(const xmlChar **attrs, int nb_attributes);
243  void process_enum_def_helper(const xmlChar **attrs, int nb_attributes);
244 
245  bool process_dmrpp_compact_start(const char *name);
246  void process_dmrpp_compact_end(const char *localname);
247  bool process_dimension(const char *name, const xmlChar **attrs, int nb_attrs);
248  bool process_dimension_def(const char *name, const xmlChar **attrs, int nb_attrs);
249  bool process_map(const char *name, const xmlChar **attrs, int nb_attributes);
250  bool process_attribute(const char *name, const xmlChar **attrs, int nb_attributes);
251  bool process_variable(const char *name, const xmlChar **attrs, int nb_attributes);
252  bool process_group(const char *name, const xmlChar **attrs, int nb_attributes);
253  bool process_enum_def(const char *name, const xmlChar **attrs, int nb_attributes);
254  bool process_enum_const(const char *name, const xmlChar **attrs, int nb_attributes);
255  bool process_dmrpp_object(const char *name, const xmlChar **attrs, int nb_attributes);
256 
257  void finish_variable(const char *tag, libdap::Type t, const char *expected);
259 
260  friend class DmrppParserSax2Test;
261 
262 public:
263  DmrppParserSax2() :
264  d_dmr(0), d_enum_def(0), d_dim_def(0),
265  other_xml(""), other_xml_depth(0), unknown_depth(0),
266  error_msg(""), context(0),
267  dods_attr_name(""), dods_attr_type(""),
268  char_data(""), root_ns(""), d_strict(true),
269  dmrpp_dataset_href("")
270  {
271  //xmlSAXHandler ddx_sax_parser;
272  memset(&dmrpp_sax_parser, 0, sizeof(xmlSAXHandler));
273 
274  dmrpp_sax_parser.getEntity = &DmrppParserSax2::dmr_get_entity;
275  dmrpp_sax_parser.startDocument = &DmrppParserSax2::dmr_start_document;
276  dmrpp_sax_parser.endDocument = &DmrppParserSax2::dmr_end_document;
277  dmrpp_sax_parser.characters = &DmrppParserSax2::dmr_get_characters;
278  dmrpp_sax_parser.ignorableWhitespace = &DmrppParserSax2::dmr_ignoreable_whitespace;
279  dmrpp_sax_parser.cdataBlock = &DmrppParserSax2::dmr_get_cdata;
280  dmrpp_sax_parser.warning = &DmrppParserSax2::dmr_error;
281  dmrpp_sax_parser.error = &DmrppParserSax2::dmr_error;
282  dmrpp_sax_parser.fatalError = &DmrppParserSax2::dmr_fatal_error;
283  dmrpp_sax_parser.initialized = XML_SAX2_MAGIC;
284  dmrpp_sax_parser.startElementNs = &DmrppParserSax2::dmr_start_element;
285  dmrpp_sax_parser.endElementNs = &DmrppParserSax2::dmr_end_element;
286  }
287 
288  ~DmrppParserSax2(){}
289 
290  void intern(std::istream &f, libdap::DMR *dest_dmr);
291  void intern(const std::string &document, libdap::DMR *dest_dmr);
292  void intern(const char *buffer, int size, libdap::DMR *dest_dmr);
293 
306  void set_strict(bool s) { d_strict = s; }
310  bool get_strict() const { return d_strict; }
313  static void dmr_start_document(void *parser);
314  static void dmr_end_document(void *parser);
315 
316  static void dmr_start_element(void *parser,
317  const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
318  int nb_namespaces, const xmlChar **namespaces, int nb_attributes,
319  int nb_defaulted, const xmlChar **attributes);
320  static void dmr_end_element(void *parser, const xmlChar *localname,
321  const xmlChar *prefix, const xmlChar *URI);
322 
323  static void dmr_get_characters(void *parser, const xmlChar *ch, int len);
324  static void dmr_ignoreable_whitespace(void *parser,
325  const xmlChar * ch, int len);
326  static void dmr_get_cdata(void *parser, const xmlChar *value, int len);
327 
328  static xmlEntityPtr dmr_get_entity(void *parser, const xmlChar *name);
329  static void dmr_fatal_error(void *parser, const char *msg, ...);
330  static void dmr_error(void *parser, const char *msg, ...);
331 
332  };
333 
334 } // namespace dmrpp
335 
336 #endif // dmrpp_parser_sax2_h
void intern(std::istream &f, libdap::DMR *dest_dmr)
static void dmr_get_cdata(void *parser, const xmlChar *value, int len)
bool get_strict() const
Get the setting of the 'strict' mode.
void set_strict(bool s)
Set the 'strict' mode to true or false.
static void dmr_ignoreable_whitespace(void *parser, const xmlChar *ch, int len)
static void dmr_end_document(void *parser)
static void dmr_get_characters(void *parser, const xmlChar *ch, int len)
static void dmr_fatal_error(void *parser, const char *msg,...)
static xmlEntityPtr dmr_get_entity(void *parser, const xmlChar *name)
static void dmr_start_document(void *parser)
Type
Type of JSON value.
Definition: rapidjson.h:664