26 #ifndef dmrpp_parser_sax2_h
27 #define dmrpp_parser_sax2_h
36 #include <unordered_map>
39 #include <libxml/parserInternals.h>
44 #define D4_PARSE_BUFF_SIZE 1048576
49 class D4BaseTypeFactory;
76 inside_attribute_container,
78 inside_attribute_value,
79 inside_other_xml_attribute,
100 inside_dmrpp_chunkDimensionSizes_element,
109 char d_parse_buffer[D4_PARSE_BUFF_SIZE+1];
111 xmlSAXHandler dmrpp_sax_parser;
116 libdap::DMR *dmr()
const {
return d_dmr; }
119 std::stack<ParseState> s;
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(); }
125 std::stack<libdap::BaseType*> btp_stack;
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(); }
131 std::stack<libdap::D4Group*> grp_stack;
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(); }
137 std::stack<libdap::D4Attributes*> d_attrs_stack;
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(); }
143 libdap::D4EnumDef *d_enum_def;
144 libdap::D4EnumDef *enum_def();
145 void clear_enum_def() { d_enum_def = 0; }
147 libdap::D4Dimension *d_dim_def;
148 libdap::D4Dimension *dim_def();
149 void clear_dim_def() { d_dim_def = 0; }
152 std::string other_xml;
156 unsigned int other_xml_depth;
157 unsigned int unknown_depth;
160 std::string error_msg;
161 xmlParserCtxtPtr context;
164 std::string dods_attr_name;
165 std::string dods_attr_type;
166 std::string char_data;
170 bool debug()
const {
return d_debug; }
174 std::string dmrpp_dataset_href;
182 void clone(
const XMLAttribute &src) {
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) {}
193 XMLAttribute(
const xmlChar **attributes) {
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]);
198 XMLAttribute(
const XMLAttribute &rhs) {
201 XMLAttribute &operator=(
const XMLAttribute &rhs) {
209 typedef std::unordered_map<std::string, XMLAttribute> XMLAttrMap;
210 XMLAttrMap xml_attrs;
212 XMLAttrMap::iterator xml_attr_begin() {
return xml_attrs.begin(); }
214 XMLAttrMap::iterator xml_attr_end() {
return xml_attrs.end(); }
216 std::map<std::string, std::string> namespace_table;
218 void cleanup_parse();
227 void transfer_xml_attrs(
const xmlChar **attrs,
int nb_attributes);
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);
237 void process_enum_const_helper(
const xmlChar **attrs,
int nb_attributes);
238 void process_enum_def_helper(
const xmlChar **attrs,
int nb_attributes);
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);
250 void finish_variable(
const char *tag,
libdap::Type t,
const char *expected);
253 friend class DmrppParserSax2Test;
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(
"")
265 memset(&dmrpp_sax_parser, 0,
sizeof(xmlSAXHandler));
273 dmrpp_sax_parser.warning = &DmrppParserSax2::dmr_error;
274 dmrpp_sax_parser.error = &DmrppParserSax2::dmr_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;
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);
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);
316 const xmlChar * ch,
int len);
317 static void dmr_get_cdata(
void *parser,
const xmlChar *value,
int len);
319 static xmlEntityPtr
dmr_get_entity(
void *parser,
const xmlChar *name);
321 static void dmr_error(
void *parser,
const char *msg, ...);
326 #endif // dmrpp_parser_sax2_h