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