26 #ifndef dmrpp_parser_sax2_h
27 #define dmrpp_parser_sax2_h
36 #include <unordered_map>
39 #include <libxml/parserInternals.h>
43 #include "EffectiveUrlCache.h"
46 #define D4_PARSE_BUFF_SIZE 1048576
51 class D4BaseTypeFactory;
78 inside_attribute_container,
80 inside_attribute_value,
81 inside_other_xml_attribute,
102 inside_dmrpp_chunkDimensionSizes_element,
103 inside_dmrpp_compact_element,
112 char d_parse_buffer[D4_PARSE_BUFF_SIZE+1];
114 xmlSAXHandler dmrpp_sax_parser;
119 libdap::DMR *dmr()
const {
return d_dmr; }
126 std::stack<ParseState> s;
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(); }
132 std::stack<libdap::BaseType*> btp_stack;
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(); }
138 std::stack<libdap::D4Group*> grp_stack;
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(); }
144 std::stack<libdap::D4Attributes*> d_attrs_stack;
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(); }
150 libdap::D4EnumDef *d_enum_def;
151 libdap::D4EnumDef *enum_def();
152 void clear_enum_def() { d_enum_def = 0; }
154 libdap::D4Dimension *d_dim_def;
155 libdap::D4Dimension *dim_def();
156 void clear_dim_def() { d_dim_def = 0; }
159 std::string other_xml;
163 unsigned int other_xml_depth;
164 unsigned int unknown_depth;
167 std::string error_msg;
168 xmlParserCtxtPtr context;
171 std::string dods_attr_name;
172 std::string dods_attr_type;
173 std::string char_data;
179 std::string dmrpp_dataset_href;
187 void clone(
const XMLAttribute &src) {
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) {}
198 XMLAttribute(
const xmlChar **attributes) {
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]);
203 XMLAttribute(
const XMLAttribute &rhs) {
206 XMLAttribute &operator=(
const XMLAttribute &rhs) {
214 typedef std::unordered_map<std::string, XMLAttribute> XMLAttrMap;
215 XMLAttrMap xml_attrs;
217 XMLAttrMap::iterator xml_attr_begin() {
return xml_attrs.begin(); }
219 XMLAttrMap::iterator xml_attr_end() {
return xml_attrs.end(); }
221 std::map<std::string, std::string> namespace_table;
223 void cleanup_parse();
232 void transfer_xml_attrs(
const xmlChar **attrs,
int nb_attributes);
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);
242 void process_enum_const_helper(
const xmlChar **attrs,
int nb_attributes);
243 void process_enum_def_helper(
const xmlChar **attrs,
int nb_attributes);
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);
257 void finish_variable(
const char *tag,
libdap::Type t,
const char *expected);
260 friend class DmrppParserSax2Test;
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(
"")
272 memset(&dmrpp_sax_parser, 0,
sizeof(xmlSAXHandler));
280 dmrpp_sax_parser.warning = &DmrppParserSax2::dmr_error;
281 dmrpp_sax_parser.error = &DmrppParserSax2::dmr_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;
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);
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);
325 const xmlChar * ch,
int len);
326 static void dmr_get_cdata(
void *parser,
const xmlChar *value,
int len);
328 static xmlEntityPtr
dmr_get_entity(
void *parser,
const xmlChar *name);
330 static void dmr_error(
void *parser,
const char *msg, ...);
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)