36 #include <libxml2/libxml/parserInternals.h> 43 #include "D4Attributes.h" 46 #include "D4BaseTypeFactory.h" 48 #include "DapXmlNamespaces.h" 49 #include "D4ParserSax2.h" 56 static const char *states[] = {
65 "inside_attribute_container",
67 "inside_attribute_value",
68 "inside_other_xml_attribute",
93 static bool is_not(
const char *name,
const char *tag)
95 return strcmp(name, tag) != 0;
107 D4ParserSax2::enum_def()
109 if (!d_enum_def) d_enum_def =
new D4EnumDef;
121 D4ParserSax2::dim_def() {
122 if (!d_dim_def) d_dim_def =
new D4Dimension;
132 void D4ParserSax2::transfer_xml_attrs(
const xmlChar **attributes,
int nb_attributes)
134 if (!xml_attrs.empty())
139 unsigned int index = 0;
140 for (
int i = 0; i < nb_attributes; ++i, index += 5) {
141 xml_attrs.insert(map<string, XMLAttribute>::value_type(
string((
const char *)attributes[index]),
142 XMLAttribute(attributes + index + 1)));
144 DBG(cerr <<
"XML Attribute '" << (
const char *)attributes[index] <<
"': " 145 << xml_attrs[(
const char *)attributes[index]].value << endl);
155 void D4ParserSax2::transfer_xml_ns(
const xmlChar **namespaces,
int nb_namespaces)
158 for (
int i = 0; i < nb_namespaces; ++i) {
159 namespace_table.insert(map<string, string>::value_type(namespaces[i * 2] != 0 ? (
const char *)namespaces[i * 2] :
"",
160 (
const char *)namespaces[i * 2 + 1]));
170 bool D4ParserSax2::check_required_attribute(
const string & attr)
172 if (xml_attrs.find(attr) == xml_attrs.end()) {
173 dmr_error(
this,
"Required attribute '%s' not found.", attr.c_str());
186 bool D4ParserSax2::check_attribute(
const string & attr)
188 return (xml_attrs.find(attr) != xml_attrs.end());
191 bool D4ParserSax2::process_dimension_def(
const char *name,
const xmlChar **attrs,
int nb_attributes)
193 if (is_not(name,
"Dimension"))
196 transfer_xml_attrs(attrs, nb_attributes);
198 if (!(check_required_attribute(
"name") && check_required_attribute(
"size"))) {
199 dmr_error(
this,
"The required attribute 'name' or 'size' was missing from a Dimension element.");
204 dim_def()->set_name(xml_attrs[
"name"].value);
206 dim_def()->set_size(xml_attrs[
"size"].value);
209 dmr_error(
this,
"%s", e.get_error_message().c_str());
233 bool D4ParserSax2::process_dimension(
const char *name,
const xmlChar **attrs,
int nb_attributes)
235 if (is_not(name,
"Dim"))
238 transfer_xml_attrs(attrs, nb_attributes);
240 if (check_attribute(
"size") && check_attribute(
"name")) {
241 dmr_error(
this,
"Only one of 'size' and 'name' are allowed in a Dim element, but both were used.");
244 if (!(check_attribute(
"size") || check_attribute(
"name"))) {
245 dmr_error(
this,
"Either 'size' or 'name' must be used in a Dim element.");
251 BaseType *b = top_basetype();
254 Array *a =
static_cast<Array*
>(dmr()->
factory()->
NewVariable(dods_array_c, b->name()));
255 a->set_is_dap4(
true);
256 a->add_var_nocopy(b);
257 a->set_attributes_nocopy(b->attributes());
261 b->set_attributes_nocopy(0);
268 Array *a =
static_cast<Array*
>(top_basetype());
269 if (check_attribute(
"size")) {
270 a->append_dim(atoi(xml_attrs[
"size"].value.c_str()));
273 else if (check_attribute(
"name")) {
274 string name = xml_attrs[
"name"].value;
276 D4Dimension *dim = 0;
283 throw Error(
"The dimension '" + name +
"' was not found while parsing the variable '" + a->name() +
"'.");
291 bool D4ParserSax2::process_map(
const char *name,
const xmlChar **attrs,
int nb_attributes)
293 if (is_not(name,
"Map"))
296 transfer_xml_attrs(attrs, nb_attributes);
298 if (!check_attribute(
"name")) {
299 dmr_error(
this,
"The 'name' attribute must be used in a Map element.");
305 BaseType *b = top_basetype();
308 Array *a =
static_cast<Array*
>(dmr()->
factory()->
NewVariable(dods_array_c, b->name()));
309 a->set_is_dap4(
true);
310 a->add_var_nocopy(b);
311 a->set_attributes_nocopy(b->attributes());
315 b->set_attributes_nocopy(0);
322 Array *a =
static_cast<Array*
>(top_basetype());
324 string map_name = xml_attrs[
"name"].value;
325 if (xml_attrs[
"name"].value[0] !=
'/')
326 map_name = top_group()->
FQN() + map_name;
328 Array *map_source = 0;
330 if (map_name[0] ==
'/')
331 map_source = dmr()->
root()->find_map_source(map_name);
333 map_source = top_group()->find_map_source(map_name);
341 if (!map_source && d_strict)
342 throw Error(
"The Map '" + map_name +
"' was not found while parsing the variable '" + a->name() +
"'.");
344 a->maps()->
add_map(
new D4Map(map_name, map_source));
349 bool D4ParserSax2::process_group(
const char *name,
const xmlChar **attrs,
int nb_attributes)
351 if (is_not(name,
"Group"))
354 transfer_xml_attrs(attrs, nb_attributes);
356 if (!check_required_attribute(
"name")) {
357 dmr_error(
this,
"The required attribute 'name' was missing from a Group element.");
363 dmr_fatal_error(
this,
"Could not instantiate the Group '%s'.", xml_attrs[
"name"].value.c_str());
367 D4Group *grp =
static_cast<D4Group*
>(btp);
371 grp->set_is_dap4(
true);
374 D4Group *parent = top_group();
380 grp->set_parent(parent);
381 parent->add_group_nocopy(grp);
384 push_attributes(grp->attributes());
394 inline bool D4ParserSax2::process_attribute(
const char *name,
const xmlChar **attrs,
int nb_attributes)
396 if (is_not(name,
"Attribute"))
400 transfer_xml_attrs(attrs, nb_attributes);
403 if (!(check_required_attribute(
string(
"name")) && check_required_attribute(
string(
"type")))) {
404 dmr_error(
this,
"The required attribute 'name' or 'type' was missing from an Attribute element.");
408 if (xml_attrs[
"type"].value ==
"Container") {
409 push_state(inside_attribute_container);
411 DBG(cerr <<
"Pushing attribute container " << xml_attrs[
"name"].value << endl);
412 D4Attribute *child =
new D4Attribute(xml_attrs[
"name"].value, attr_container_c);
414 D4Attributes *tos = top_attributes();
418 dmr_fatal_error(
this,
"Expected an Attribute container on the top of the attribute stack.");
422 tos->add_attribute_nocopy(child);
423 push_attributes(child->attributes());
425 else if (xml_attrs[
"type"].value ==
"OtherXML") {
426 push_state(inside_other_xml_attribute);
428 dods_attr_name = xml_attrs[
"name"].value;
429 dods_attr_type = xml_attrs[
"type"].value;
432 push_state(inside_attribute);
434 dods_attr_name = xml_attrs[
"name"].value;
435 dods_attr_type = xml_attrs[
"type"].value;
446 inline bool D4ParserSax2::process_enum_def(
const char *name,
const xmlChar **attrs,
int nb_attributes)
448 if (is_not(name,
"Enumeration"))
451 transfer_xml_attrs(attrs, nb_attributes);
453 if (!(check_required_attribute(
"name") && check_required_attribute(
"basetype"))) {
454 dmr_error(
this,
"The required attribute 'name' or 'basetype' was missing from an Enumeration element.");
460 dmr_error(
this,
"The Enumeration '%s' must have an integer type, instead the type '%s' was used.",
461 xml_attrs[
"name"].value.c_str(), xml_attrs[
"basetype"].value.c_str());
466 string enum_def_path = xml_attrs[
"name"].value;
469 if (xml_attrs[
"name"].value[0] !=
'/')
470 enum_def_path = top_group()->
FQN() + enum_def_path;
472 enum_def()->set_name(enum_def_path);
473 enum_def()->set_type(t);
478 inline bool D4ParserSax2::process_enum_const(
const char *name,
const xmlChar **attrs,
int nb_attributes)
480 if (is_not(name,
"EnumConst"))
484 transfer_xml_attrs(attrs, nb_attributes);
486 if (!(check_required_attribute(
"name") && check_required_attribute(
"value"))) {
487 dmr_error(
this,
"The required attribute 'name' or 'value' was missing from an EnumConst element.");
491 istringstream iss(xml_attrs[
"value"].value);
493 iss >> skipws >> value;
494 if (iss.fail() || iss.bad()) {
495 dmr_error(
this,
"Expected an integer value for an Enumeration constant, got '%s' instead.",
496 xml_attrs[
"value"].value.c_str());
499 dmr_error(
this,
"In an Enumeration constant, the value '%s' cannot fit in a variable of type '%s'.",
500 xml_attrs[
"value"].value.c_str(),
D4type_name(d_enum_def->type()).c_str());
504 enum_def()->add_value(xml_attrs[
"name"].value, value);
515 inline bool D4ParserSax2::process_variable(
const char *name,
const xmlChar **attrs,
int nb_attributes)
519 process_variable_helper(t, inside_simple_type, attrs, nb_attributes);
524 case dods_structure_c:
525 process_variable_helper(t, inside_constructor, attrs, nb_attributes);
528 case dods_sequence_c:
529 process_variable_helper(t, inside_constructor, attrs, nb_attributes);
545 void D4ParserSax2::process_variable_helper(
Type t, ParseState s,
const xmlChar **attrs,
int nb_attributes)
547 transfer_xml_attrs(attrs, nb_attributes);
549 if (check_required_attribute(
"name")) {
552 dmr_fatal_error(
this,
"Could not instantiate the variable '%s'.", xml_attrs[
"name"].value.c_str());
556 if ((t == dods_enum_c) && check_required_attribute(
"enum")) {
557 D4EnumDef *enum_def = 0;
558 string enum_path = xml_attrs[
"enum"].value;
559 if (enum_path[0] ==
'/')
560 enum_def = dmr()->
root()->find_enum_def(enum_path);
562 enum_def = top_group()->find_enum_def(enum_path);
565 dmr_fatal_error(
this,
"Could not find the Enumeration definition '%s'.", enum_path.c_str());
567 static_cast<D4Enum*
>(btp)->set_enumeration(enum_def);
570 btp->set_is_dap4(
true);
573 push_attributes(btp->attributes());
593 parser->d_error_msg =
"";
594 parser->char_data =
"";
603 if (parser->debug()) cerr <<
"Parser start state: " << states[parser->get_state()] << endl;
612 if (parser->debug()) cerr <<
"Parser end state: " << states[parser->get_state()] << endl;
614 if (parser->get_state() != parser_end)
615 D4ParserSax2::dmr_error(parser,
"The document contained unbalanced tags.");
619 if (parser->get_state() == parser_error || parser->get_state() == parser_fatal_error)
622 if (!parser->empty_basetype() || parser->empty_group())
623 D4ParserSax2::dmr_error(parser,
"The document did not contain a valid root Group or contained unbalanced tags.");
626 parser->pop_attributes();
643 int nb_namespaces,
const xmlChar **namespaces,
int nb_attributes,
int ,
644 const xmlChar **attributes)
647 const char *localname = (
const char *) l;
649 if (parser->debug()) cerr <<
"Start element " << localname <<
" prefix: "<< (prefix?(
char *)prefix:
"null") <<
" ns: "<< (URI?(
char *)URI:
"null")
650 <<
" (state: " << states[parser->get_state()] <<
")" << endl;
652 if(parser->get_state() != parser_error){
654 if (parser->debug()) cerr <<
"dap4_ns_name: " << dap4_ns_name << endl;
656 string this_element_ns_name = (URI != 0) ? ((
char *)URI) :
"";
657 if (parser->debug()) cerr <<
"this_element_ns_name: " << this_element_ns_name << endl;
659 if(this_element_ns_name.compare(dap4_ns_name)){
660 if (parser->debug()) cerr <<
"Start of non DAP4 element: " << localname <<
" detected." << endl;
661 parser->push_state(not_dap4_element);
667 switch (parser->get_state()) {
669 if (is_not(localname,
"Dataset"))
670 D4ParserSax2::dmr_error(parser,
"Expected DMR to start with a Dataset element; found '%s' instead.", localname);
672 parser->root_ns = URI ? (
const char *) URI :
"";
673 parser->transfer_xml_attrs(attributes, nb_attributes);
675 if (parser->check_required_attribute(
string(
"name")))
676 parser->dmr()->set_name(parser->xml_attrs[
"name"].value);
678 if (parser->check_attribute(
"dapVersion"))
681 if (parser->check_attribute(
"dmrVersion"))
682 parser->dmr()->set_dmr_version(parser->xml_attrs[
"dmrVersion"].value);
684 if (parser->check_attribute(
"base"))
687 if (!parser->root_ns.empty())
691 parser->push_group(parser->dmr()->
root());
693 parser->push_state(inside_dataset);
702 if (parser->process_enum_def(localname, attributes, nb_attributes))
703 parser->push_state(inside_enum_def);
704 else if (parser->process_dimension_def(localname, attributes, nb_attributes))
705 parser->push_state(inside_dim_def);
706 else if (parser->process_group(localname, attributes, nb_attributes))
707 parser->push_state(inside_group);
708 else if (parser->process_variable(localname, attributes, nb_attributes))
712 else if (parser->process_attribute(localname, attributes, nb_attributes))
717 D4ParserSax2::dmr_error(parser,
"Expected an Attribute, Enumeration, Dimension, Group or variable element; found '%s' instead.", localname);
720 case inside_attribute_container:
721 if (parser->process_attribute(localname, attributes, nb_attributes))
724 D4ParserSax2::dmr_error(parser,
"Expected an Attribute element; found '%s' instead.", localname);
727 case inside_attribute:
728 if (parser->process_attribute(localname, attributes, nb_attributes))
730 else if (strcmp(localname,
"Value") == 0)
731 parser->push_state(inside_attribute_value);
733 dmr_error(parser,
"Expected an 'Attribute' or 'Value' element; found '%s' instead.", localname);
736 case inside_attribute_value:
740 case inside_other_xml_attribute:
741 parser->other_xml_depth++;
744 parser->other_xml.append(
"<");
746 parser->other_xml.append((
const char *) prefix);
747 parser->other_xml.append(
":");
749 parser->other_xml.append(localname);
751 if (nb_namespaces != 0) {
752 parser->transfer_xml_ns(namespaces, nb_namespaces);
754 for (map<string, string>::iterator i = parser->namespace_table.begin();
755 i != parser->namespace_table.end(); ++i) {
756 parser->other_xml.append(
" xmlns");
757 if (!i->first.empty()) {
758 parser->other_xml.append(
":");
759 parser->other_xml.append(i->first);
761 parser->other_xml.append(
"=\"");
762 parser->other_xml.append(i->second);
763 parser->other_xml.append(
"\"");
767 if (nb_attributes != 0) {
768 parser->transfer_xml_attrs(attributes, nb_attributes);
769 for (XMLAttrMap::iterator i = parser->xml_attr_begin(); i != parser->xml_attr_end(); ++i) {
770 parser->other_xml.append(
" ");
771 if (!i->second.prefix.empty()) {
772 parser->other_xml.append(i->second.prefix);
773 parser->other_xml.append(
":");
775 parser->other_xml.append(i->first);
776 parser->other_xml.append(
"=\"");
777 parser->other_xml.append(i->second.value);
778 parser->other_xml.append(
"\"");
782 parser->other_xml.append(
">");
785 case inside_enum_def:
787 if (parser->process_enum_const(localname, attributes, nb_attributes))
788 parser->push_state(inside_enum_const);
790 dmr_error(parser,
"Expected an 'EnumConst' element; found '%s' instead.", localname);
793 case inside_enum_const:
801 case inside_dimension:
813 case inside_simple_type:
814 if (parser->process_attribute(localname, attributes, nb_attributes))
816 else if (parser->process_dimension(localname, attributes, nb_attributes))
817 parser->push_state(inside_dim);
818 else if (parser->process_map(localname, attributes, nb_attributes))
819 parser->push_state(inside_map);
821 dmr_error(parser,
"Expected an 'Attribute', 'Dim' or 'Map' element; found '%s' instead.", localname);
824 case inside_constructor:
825 if (parser->process_variable(localname, attributes, nb_attributes))
829 else if (parser->process_attribute(localname, attributes, nb_attributes))
831 else if (parser->process_dimension(localname, attributes, nb_attributes))
832 parser->push_state(inside_dim);
833 else if (parser->process_map(localname, attributes, nb_attributes))
834 parser->push_state(inside_map);
836 D4ParserSax2::dmr_error(parser,
"Expected an Attribute, Dim, Map or variable element; found '%s' instead.", localname);
839 case not_dap4_element:
840 if (parser->debug()) cerr <<
"Inside non DAP4 element. localname: " << localname << endl;
846 parser->push_state(parser_unknown);
850 case parser_fatal_error:
858 if (parser->debug()) cerr <<
"Start element exit state: " << states[parser->get_state()] << endl;
861 void D4ParserSax2::dmr_end_element(
void *p,
const xmlChar *l,
const xmlChar *prefix,
const xmlChar *URI)
864 const char *localname = (
const char *) l;
867 cerr <<
"End element " << localname <<
" (state " << states[parser->get_state()] <<
")" << endl;
869 switch (parser->get_state()) {
871 dmr_fatal_error(parser,
"Unexpected state, inside start state while processing element '%s'.", localname);
875 if (is_not(localname,
"Dataset"))
876 D4ParserSax2::dmr_error(parser,
"Expected an end Dataset tag; found '%s' instead.", localname);
879 if (parser->get_state() != parser_start)
883 parser->push_state(parser_end);
888 if (is_not(localname,
"Group"))
889 D4ParserSax2::dmr_error(parser,
"Expected an end tag for a Group; found '%s' instead.", localname);
891 if (!parser->empty_basetype() || parser->empty_group())
892 D4ParserSax2::dmr_error(parser,
893 "The document did not contain a valid root Group or contained unbalanced tags.");
900 case inside_attribute_container:
901 if (is_not(localname,
"Attribute"))
902 D4ParserSax2::dmr_error(parser,
"Expected an end Attribute tag; found '%s' instead.", localname);
905 parser->pop_attributes();
908 case inside_attribute:
909 if (is_not(localname,
"Attribute"))
910 D4ParserSax2::dmr_error(parser,
"Expected an end Attribute tag; found '%s' instead.", localname);
915 case inside_attribute_value: {
916 if (is_not(localname,
"Value"))
917 D4ParserSax2::dmr_error(parser,
"Expected an end value tag; found '%s' instead.", localname);
927 attr =
new D4Attribute(parser->dods_attr_name, StringToD4AttributeType(parser->dods_attr_type));
928 attrs->add_attribute_nocopy(attr);
930 attr->add_value(parser->char_data);
932 parser->char_data =
"";
936 case inside_other_xml_attribute: {
937 if (strcmp(localname,
"Attribute") == 0 && parser->root_ns == (
const char *) URI) {
946 attr =
new D4Attribute(parser->dods_attr_name, StringToD4AttributeType(parser->dods_attr_type));
947 attrs->add_attribute_nocopy(attr);
949 attr->add_value(parser->other_xml);
951 parser->other_xml =
"";
954 if (parser->other_xml_depth == 0) {
955 D4ParserSax2::dmr_error(parser,
"Expected an OtherXML attribute to end! Instead I found '%s'",
959 parser->other_xml_depth--;
961 parser->other_xml.append(
"</");
963 parser->other_xml.append((
const char *) prefix);
964 parser->other_xml.append(
":");
966 parser->other_xml.append(localname);
967 parser->other_xml.append(
">");
972 case inside_enum_def:
973 if (is_not(localname,
"Enumeration"))
974 D4ParserSax2::dmr_error(parser,
"Expected an end Enumeration tag; found '%s' instead.", localname);
975 if (!parser->top_group())
977 "Expected a Group to be the current item, while finishing up an Enumeration.");
980 parser->top_group()->
enum_defs()->add_enum_nocopy(parser->enum_def());
983 parser->clear_enum_def();
988 case inside_enum_const:
989 if (is_not(localname,
"EnumConst"))
990 D4ParserSax2::dmr_error(parser,
"Expected an end EnumConst tag; found '%s' instead.", localname);
995 case inside_dim_def: {
996 if (is_not(localname,
"Dimension"))
997 D4ParserSax2::dmr_error(parser,
"Expected an end Dimension tag; found '%s' instead.", localname);
999 if (!parser->top_group())
1000 D4ParserSax2::dmr_error(parser,
1001 "Expected a Group to be the current item, while finishing up an Dimension.");
1012 parser->clear_dim_def();
1013 parser->pop_state();
1017 case inside_simple_type:
1019 BaseType *btp = parser->top_basetype();
1020 parser->pop_basetype();
1021 parser->pop_attributes();
1024 if (!parser->empty_basetype())
1025 parent = parser->top_basetype();
1026 else if (!parser->empty_group())
1027 parent = parser->top_group();
1029 dmr_fatal_error(parser,
"Both the Variable and Groups stacks are empty while closing a %s element.",
1032 parser->pop_state();
1036 if (parent->
type() == dods_array_c)
1037 static_cast<Array*>(parent)->prototype()->add_var_nocopy(btp);
1039 parent->add_var_nocopy(btp);
1042 D4ParserSax2::dmr_error(parser,
"Expected an end tag for a simple type; found '%s' instead.", localname);
1044 parser->pop_state();
1048 if (is_not(localname,
"Dim"))
1051 parser->pop_state();
1055 if (is_not(localname,
"Map"))
1058 parser->pop_state();
1061 case inside_constructor: {
1062 if (strcmp(localname,
"Structure") != 0 && strcmp(localname,
"Sequence") != 0) {
1063 D4ParserSax2::dmr_error(parser,
"Expected an end tag for a constructor; found '%s' instead.", localname);
1067 BaseType *btp = parser->top_basetype();
1068 parser->pop_basetype();
1069 parser->pop_attributes();
1072 if (!parser->empty_basetype())
1073 parent = parser->top_basetype();
1074 else if (!parser->empty_group())
1075 parent = parser->top_group();
1077 dmr_fatal_error(parser,
"Both the Variable and Groups stacks are empty while closing a %s element.",
1080 parser->pop_state();
1086 parent->add_var_nocopy(btp);
1087 parser->pop_state();
1091 case not_dap4_element:
1092 if (parser->debug()) cerr <<
"End of non DAP4 element: " << localname << endl;
1093 parser->pop_state();
1096 case parser_unknown:
1097 parser->pop_state();
1101 case parser_fatal_error:
1109 if (parser->debug()) cerr <<
"End element exit state: " << states[parser->get_state()] << endl;
1119 switch (parser->get_state()) {
1120 case inside_attribute_value:
1121 parser->char_data.append((
const char *) (ch), len);
1122 DBG(cerr <<
"Characters: '" << parser->char_data <<
"'" << endl);
1125 case inside_other_xml_attribute:
1126 parser->other_xml.append((
const char *) (ch), len);
1127 DBG(cerr <<
"Other XML Characters: '" << parser->other_xml <<
"'" << endl);
1143 switch (parser->get_state()) {
1144 case inside_other_xml_attribute:
1145 parser->other_xml.append((
const char *) (ch), len);
1162 switch (parser->get_state()) {
1163 case inside_other_xml_attribute:
1164 parser->other_xml.append((
const char *) (value), len);
1167 case parser_unknown:
1171 D4ParserSax2::dmr_error(parser,
"Found a CData block but none are allowed by DAP4.");
1183 return xmlGetPredefinedEntity(name);
1201 parser->push_state(parser_fatal_error);
1203 va_start(args, msg);
1205 vsnprintf(str, 1024, msg, args);
1208 int line = xmlSAX2GetLineNumber(parser->d_context);
1210 if (!parser->d_error_msg.empty()) parser->d_error_msg +=
"\n";
1211 parser->d_error_msg +=
"At line " + long_to_string(line) +
": " + string(str);
1214 void D4ParserSax2::dmr_error(
void *p,
const char *msg, ...)
1219 parser->push_state(parser_error);
1221 va_start(args, msg);
1223 vsnprintf(str, 1024, msg, args);
1226 int line = xmlSAX2GetLineNumber(parser->d_context);
1228 if (!parser->d_error_msg.empty()) parser->d_error_msg +=
"\n";
1229 parser->d_error_msg +=
"At line " + long_to_string(line) +
": " + string(str);
1236 void D4ParserSax2::cleanup_parse()
1238 bool wellFormed = d_context->wellFormed;
1239 bool valid = d_context->valid;
1242 xmlFreeParserCtxt(d_context);
1252 while (!btp_stack.empty()) {
1253 delete top_basetype();
1258 throw Error(
"The DMR was not well formed. " + d_error_msg);
1260 throw Error(
"The DMR was not valid." + d_error_msg);
1261 else if (get_state() == parser_error)
1262 throw Error(d_error_msg);
1263 else if (get_state() == parser_fatal_error)
1281 void D4ParserSax2::intern(istream &f,
DMR *dest_dmr,
bool debug)
1288 throw Error(
"Input stream not open or read error");
1290 throw InternalErr(__FILE__, __LINE__,
"DMR object is null");
1299 if (line.length() == 0)
throw Error(
"No input found while parsing the DMR.");
1301 if (debug) cerr <<
"line: (" << line_num <<
"): " << endl << line << endl << endl;
1303 d_context = xmlCreatePushParserCtxt(&d_dmr_sax_parser,
this, line.c_str(), line.length(),
"stream");
1304 d_context->validate =
true;
1305 push_state(parser_start);
1311 if (debug) cerr <<
"line: (" << line_num <<
"): " << endl << line << endl << endl;
1313 while (!f.eof() && (get_state() != parser_end)) {
1314 xmlParseChunk(d_context, line.c_str(), line.length(), 0);
1320 if (debug) cerr <<
"line: (" << line_num <<
"): " << endl << line << endl << endl;
1323 xmlParseChunk(d_context, line.c_str(), 0, 1);
1331 if (line.length() == 0)
throw Error(
"No input found while parsing the DMR.");
1333 if (debug) cerr <<
"line: (" << line_num <<
"): " << endl << line << endl << endl;
1335 d_context = xmlCreatePushParserCtxt(&d_dmr_sax_parser,
this, line.c_str(), line.length(),
"stream");
1336 d_context->validate =
true;
1337 push_state(parser_start);
1340 long chunk_count = 0;
1341 long chunk_size = 0;
1343 f.read(d_parse_buffer, D4_PARSE_BUFF_SIZE);
1344 chunk_size=f.gcount();
1345 d_parse_buffer[chunk_size]=0;
1346 if (debug) cerr <<
"chunk: (" << chunk_count++ <<
"): " << endl << d_parse_buffer << endl << endl;
1348 while(!f.eof() && (get_state() != parser_end)){
1350 xmlParseChunk(d_context, d_parse_buffer, chunk_size, 0);
1353 f.read(d_parse_buffer, D4_PARSE_BUFF_SIZE);
1354 chunk_size=f.gcount();
1355 d_parse_buffer[chunk_size]=0;
1356 if (debug) cerr <<
"chunk: (" << chunk_count++ <<
"): " << endl << d_parse_buffer << endl << endl;
1360 xmlParseChunk(d_context, d_parse_buffer, chunk_size, 1);
1382 void D4ParserSax2::intern(
const string &document,
DMR *dest_dmr,
bool debug)
1384 intern(document.c_str(), document.length(), dest_dmr, debug);
1397 void D4ParserSax2::intern(
const char *buffer,
int size,
DMR *dest_dmr,
bool debug)
1399 if (!(size > 0))
return;
1405 if (!dest_dmr)
throw InternalErr(__FILE__, __LINE__,
"DMR object is null");
1408 push_state(parser_start);
1409 d_context = xmlCreatePushParserCtxt(&d_dmr_sax_parser,
this, buffer, size,
"stream");
1410 d_context->validate =
true;
1413 xmlParseChunk(d_context, buffer, 0, 1);
virtual BaseType * NewVariable(Type t, const string &name) const
static void dmr_end_document(void *parser)
bool is_valid_enum_value(long long value)
void set_namespace(const std::string &ns)
Set the namespace for this DMR.
D4Dimension * find_dim(const string &path)
Find the dimension using a path. Using the DAP4 name syntax, lookup a dimension. The dimension must b...
static void dmr_start_document(void *parser)
static xmlEntityPtr dmr_get_entity(void *parser, const xmlChar *name)
bool is_vector_type(Type t)
Returns true if the instance is a vector (i.e., array) type variable.
void set_request_xml_base(const std::string &xb)
Type
Identifies the data type.
top level DAP object to house generic methods
A class for software fault reporting.
static std::string getDapNamespaceString(DAPVersion version)
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)
void add_dim_nocopy(D4Dimension *dim)
virtual D4BaseTypeFactory * factory()
static void dmr_get_cdata(void *parser, const xmlChar *value, int len)
virtual Type type() const
Returns the type of the class instance.
ObjectType get_type(const string &value)
string D4type_name(Type t)
Returns the type of the class instance as a string. Supports all DAP4 types and not the DAP2-only typ...
bool is_simple_type(Type t)
Returns true if the instance is a numeric, string or URL type variable.
virtual D4Attributes * attributes()
void set_dap_version(const std::string &version_string)
static void dmr_get_characters(void *parser, const xmlChar *ch, int len)
The basic data type for the DODS DAP types.
virtual std::string FQN() const
D4Attribute * get(const string &fqn)
A class for error processing.
D4EnumDefs * enum_defs()
Get the enumerations defined for this Group.
static void dmr_fatal_error(void *parser, const char *msg,...)
bool is_integer_type(Type t)
D4Dimensions * dims()
Get the dimensions defined for this Group.