57 static const not_used char *states[] =
63 "attribute_container",
66 "other_xml_attribute",
90 BaseType *DDXParser::factory(
Type t,
const string & name)
94 return d_factory->
NewByte(name);
122 return d_factory->
NewStr(name);
126 return d_factory->
NewUrl(name);
142 return d_factory->
NewGrid(name);
151 static Type get_type(
const char *name)
153 if (strcmp(name,
"Byte") == 0)
156 if (strcmp(name,
"Int16") == 0)
159 if (strcmp(name,
"UInt16") == 0)
162 if (strcmp(name,
"Int32") == 0)
165 if (strcmp(name,
"UInt32") == 0)
168 if (strcmp(name,
"Float32") == 0)
171 if (strcmp(name,
"Float64") == 0)
174 if (strcmp(name,
"String") == 0)
177 if (strcmp(name,
"Url") == 0)
180 if (strcmp(name,
"Array") == 0)
183 if (strcmp(name,
"Structure") == 0)
186 if (strcmp(name,
"Sequence") == 0)
189 if (strcmp(name,
"Grid") == 0)
195 static Type is_simple_type(
const char *name)
197 Type t = get_type(name);
214 static bool is_not(
const char *name,
const char *tag)
216 return strcmp(name, tag) != 0;
219 void DDXParser::set_state(DDXParser::ParseState state)
224 DDXParser::ParseState DDXParser::get_state()
const 229 void DDXParser::pop_state()
237 void DDXParser::transfer_xml_attrs(
const xmlChar **attributes,
int nb_attributes)
239 if (!attribute_table.empty())
240 attribute_table.clear();
242 unsigned int index = 0;
243 for (
int i = 0; i < nb_attributes; ++i, index += 5) {
246 attribute_table.insert(map<string, XMLAttribute>::value_type(
247 string((
const char *)attributes[index]),
248 XMLAttribute(attributes + index + 1)));
250 DBG(cerr <<
"Attribute '" << (
const char *)attributes[index] <<
"': " 251 << attribute_table[(
const char *)attributes[index]].value << endl);
255 void DDXParser::transfer_xml_ns(
const xmlChar **namespaces,
int nb_namespaces)
257 for (
int i = 0; i < nb_namespaces; ++i ) {
260 namespace_table.insert(map<string,string>::value_type(
261 namespaces[i*2] != 0 ? (
const char *)namespaces[i*2] :
"",
262 (
const char *)namespaces[i*2+1]));
270 bool DDXParser::check_required_attribute(
const string & attr)
272 map < string, XMLAttribute >::iterator i = attribute_table.find(attr);
273 if (i == attribute_table.end())
284 bool DDXParser::check_attribute(
const string & attr)
286 return (attribute_table.find(attr) != attribute_table.end());
297 void DDXParser::process_attribute_element(
const xmlChar **attrs,
int nb_attributes)
300 transfer_xml_attrs(attrs, nb_attributes);
302 bool error = !(check_required_attribute(
string(
"name"))
303 && check_required_attribute(
string(
"type")));
307 if (attribute_table[
"type"].value ==
"Container") {
308 set_state(inside_attribute_container);
311 AttrTable *parent = at_stack.top();
313 child = parent->append_container(attribute_table[
"name"].value);
314 at_stack.push(child);
315 DBG2(cerr <<
"Pushing at" << endl);
317 else if (attribute_table[
"type"].value ==
"OtherXML") {
318 set_state(inside_other_xml_attribute);
320 dods_attr_name = attribute_table[
"name"].value;
321 dods_attr_type = attribute_table[
"type"].value;
324 set_state(inside_attribute);
328 dods_attr_name = attribute_table[
"name"].value;
329 dods_attr_type = attribute_table[
"type"].value;
336 void DDXParser::process_attribute_alias(
const xmlChar **attrs,
int nb_attributes)
338 transfer_xml_attrs(attrs, nb_attributes);
339 if (check_required_attribute(
string(
"name"))
340 && check_required_attribute(
string(
"attribute"))) {
341 set_state(inside_alias);
342 at_stack.top()->attr_alias(attribute_table[
"name"].value,
343 attribute_table[
"attribute"].value);
354 void DDXParser::process_variable(
Type t, ParseState s,
const xmlChar **attrs,
357 transfer_xml_attrs(attrs, nb_attributes);
361 || check_required_attribute(
"name")) {
362 BaseType *btp = factory(t, attribute_table[
"name"].value);
366 "Internal parser error; could not instantiate the variable '%s'.",
367 attribute_table[
"name"].value.c_str());
374 at_stack.push(&btp->get_attr_table());
381 void DDXParser::process_dimension(
const xmlChar **attrs,
int nb_attributes)
383 transfer_xml_attrs(attrs, nb_attributes);
384 if (check_required_attribute(
string(
"size"))) {
385 set_state(inside_dimension);
386 Array *ap = dynamic_cast < Array * >(bt_stack.top());
390 ap->append_dim(atoi(attribute_table[
"size"].value.c_str()),
391 attribute_table[
"name"].value);
397 void DDXParser::process_blob(
const xmlChar **attrs,
int nb_attributes)
399 transfer_xml_attrs(attrs, nb_attributes);
400 if (check_required_attribute(
string(
"href"))) {
401 set_state(inside_blob_href);
402 *blob_href = attribute_table[
"href"].value;
413 DDXParser::is_attribute_or_alias(
const char *name,
const xmlChar **attrs,
416 if (strcmp(name,
"Attribute") == 0) {
417 process_attribute_element(attrs, nb_attributes);
421 else if (strcmp(name,
"Alias") == 0) {
422 process_attribute_alias(attrs, nb_attributes);
435 inline bool DDXParser::is_variable(
const char *name,
const xmlChar **attrs,
440 process_variable(t, inside_simple_type, attrs, nb_attributes);
443 else if (strcmp(name,
"Array") == 0) {
444 process_variable(
dods_array_c, inside_array, attrs, nb_attributes);
447 else if (strcmp(name,
"Structure") == 0) {
451 else if (strcmp(name,
"Sequence") == 0) {
452 process_variable(
dods_sequence_c, inside_sequence, attrs, nb_attributes);
455 else if (strcmp(name,
"Grid") == 0) {
456 process_variable(
dods_grid_c, inside_grid, attrs, nb_attributes);
463 void DDXParser::finish_variable(
const char *tag,
Type t,
const char *expected)
465 if (strcmp(tag, expected) != 0) {
467 "Expected an end tag for a %s; found '%s' instead.",
474 BaseType *btp = bt_stack.top();
479 if (btp->type() != t) {
481 "Internal error: Expected a %s variable.",
487 && dynamic_cast < Array * >(btp)->dimensions() == 0) {
489 "No dimension element included in the Array '%s'.",
490 btp->name().c_str());
494 BaseType *parent = bt_stack.top();
496 if (!(parent->is_vector_type() || parent->is_constructor_type())) {
498 "Tried to add the array variable '%s' to a non-constructor type (%s %s).",
500 bt_stack.top()->type_name().c_str(),
501 bt_stack.top()->name().c_str());
505 parent->add_var(btp);
522 parser->error_msg =
"";
523 parser->char_data =
"";
531 parser->bt_stack.push(
new Structure(
"dummy_dds"));
533 parser->set_state(parser_start);
535 DBG2(cerr <<
"Parser state: " << states[parser->get_state()] << endl);
543 DBG2(cerr <<
"Ending state == " << states[parser->get_state()] <<
546 if (parser->get_state() != parser_start)
548 "The document contained unbalanced tags.");
552 if (parser->get_state() == parser_error)
559 ddx_fatal_error(parser,
"Parse error: Expected a Structure, Sequence or Grid variable.");
565 parser->bt_stack.pop();
570 const xmlChar *l,
const xmlChar *prefix,
const xmlChar *URI,
571 int nb_namespaces,
const xmlChar **namespaces,
572 int nb_attributes,
int ,
const xmlChar **attributes)
575 const char *localname = (
const char *)l;
577 DBG2(cerr <<
"start element: " << localname <<
", states: " 578 << states[parser->get_state()]);
580 switch (parser->get_state()) {
582 if (strcmp(localname,
"Dataset") == 0) {
583 parser->set_state(inside_dataset);
584 parser->root_ns = URI != 0 ? (
const char *)URI:
"";
585 parser->transfer_xml_attrs(attributes, nb_attributes);
587 if (parser->check_required_attribute(
string(
"name")))
590 if (parser->check_attribute(
"dapVersion"))
591 parser->dds->
set_dap_version(parser->attribute_table[
"dapVersion"].value);
595 "Expected response to start with a Dataset element; found '%s' instead.",
600 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
602 else if (parser->is_variable(localname, attributes, nb_attributes))
604 else if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0) {
605 parser->process_blob(attributes, nb_attributes);
610 "Expected an Attribute, Alias or variable element; found '%s' instead.",
614 case inside_attribute_container:
615 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
619 "Expected an Attribute or Alias element; found '%s' instead.",
623 case inside_attribute:
624 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
626 else if (strcmp(localname,
"value") == 0)
627 parser->set_state(inside_attribute_value);
630 "Expected an 'Attribute', 'Alias' or 'value' element; found '%s' instead.",
634 case inside_attribute_value:
636 "Internal parser error; unexpected state, inside value while processing element '%s'.",
640 case inside_other_xml_attribute:
641 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname << endl);
643 parser->other_xml_depth++;
647 parser->other_xml.append(
"<");
649 parser->other_xml.append((
const char *)prefix);
650 parser->other_xml.append(
":");
652 parser->other_xml.append(localname);
654 if (nb_namespaces != 0) {
655 parser->transfer_xml_ns(namespaces, nb_namespaces);
657 for (map<string,string>::iterator i = parser->namespace_table.begin();
658 i != parser->namespace_table.end();
660 parser->other_xml.append(
" xmlns");
661 if (!i->first.empty()) {
662 parser->other_xml.append(
":");
663 parser->other_xml.append(i->first);
665 parser->other_xml.append(
"=\"");
666 parser->other_xml.append(i->second);
667 parser->other_xml.append(
"\"");
671 if (nb_attributes != 0) {
672 parser->transfer_xml_attrs(attributes, nb_attributes);
673 for (XMLAttrMap::iterator i = parser->attr_table_begin();
674 i != parser->attr_table_end();
676 parser->other_xml.append(
" ");
677 if (!i->second.prefix.empty()) {
678 parser->other_xml.append(i->second.prefix);
679 parser->other_xml.append(
":");
681 parser->other_xml.append(i->first);
682 parser->other_xml.append(
"=\"");
683 parser->other_xml.append(i->second.value);
684 parser->other_xml.append(
"\"");
688 parser->other_xml.append(
">");
693 "Internal parser error; unexpected state, inside alias while processing element '%s'.",
697 case inside_simple_type:
698 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
702 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
707 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
709 else if (is_not(localname,
"Array")
710 && parser->is_variable(localname, attributes, nb_attributes))
712 else if (strcmp(localname,
"dimension") == 0) {
713 parser->process_dimension(attributes, nb_attributes);
718 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
722 case inside_dimension:
724 "Internal parser error; unexpected state, inside dimension while processing element '%s'.",
728 case inside_structure:
729 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
731 else if (parser->is_variable(localname, attributes, nb_attributes))
735 "Expected an Attribute, Alias or variable element; found '%s' instead.",
739 case inside_sequence:
740 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
742 else if (parser->is_variable(localname, attributes, nb_attributes))
746 "Expected an Attribute, Alias or variable element; found '%s' instead.",
751 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
753 else if (strcmp(localname,
"Array") == 0)
754 parser->process_variable(
dods_array_c, inside_array, attributes, nb_attributes);
755 else if (strcmp(localname,
"Map") == 0)
756 parser->process_variable(
dods_array_c, inside_map, attributes, nb_attributes);
759 "Expected an Attribute, Alias or variable element; found '%s' instead.",
764 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
766 else if (is_not(localname,
"Array") && is_not(localname,
"Sequence")
767 && is_not(localname,
"Grid")
768 && parser->is_variable(localname, attributes, nb_attributes))
770 else if (strcmp(localname,
"dimension") == 0) {
771 parser->process_dimension(attributes, nb_attributes);
776 "Expected an 'Attribute', 'Alias', variable or 'dimension' element; found '%s' instead.",
780 case inside_blob_href:
782 "Internal parser error; unexpected state, inside blob href while processing element '%s'.",
788 parser->set_state(parser_unknown);
795 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
799 const xmlChar *prefix,
const xmlChar *URI)
802 const char *localname = (
const char *)l;
804 DBG2(cerr <<
"End element " << localname <<
" (state " 805 << states[parser->get_state()] <<
")" << endl);
807 switch (parser->get_state()) {
810 "Internal parser error; unexpected state, inside start state while processing element '%s'.",
815 if (strcmp(localname,
"Dataset") == 0)
819 "Expected an end Dataset tag; found '%s' instead.",
823 case inside_attribute_container:
824 if (strcmp(localname,
"Attribute") == 0) {
826 parser->at_stack.pop();
830 "Expected an end Attribute tag; found '%s' instead.",
834 case inside_attribute:
835 if (strcmp(localname,
"Attribute") == 0)
839 "Expected an end Attribute tag; found '%s' instead.",
843 case inside_attribute_value:
844 if (strcmp(localname,
"value") == 0) {
848 parser->dods_attr_type, parser->char_data);
849 parser->char_data =
"";
853 "Expected an end value tag; found '%s' instead.",
858 case inside_other_xml_attribute: {
859 if (strcmp(localname,
"Attribute") == 0
860 && parser->root_ns == (
const char *)URI) {
862 DBGN(cerr << endl <<
"\t Popping the 'inside_other_xml_attribute' state" 869 parser->dods_attr_type, parser->other_xml);
871 parser->other_xml =
"";
874 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname
875 <<
", depth: " << parser->other_xml_depth << endl);
876 if (parser->other_xml_depth == 0)
878 "Expected an OtherXML attribute to end! Instead I found '%s'",
880 parser->other_xml_depth--;
882 parser->other_xml.append(
"</");
884 parser->other_xml.append((
const char *)prefix);
885 parser->other_xml.append(
":");
887 parser->other_xml.append(localname);
888 parser->other_xml.append(
">");
897 case inside_simple_type:
900 BaseType *btp = parser->bt_stack.top();
901 parser->bt_stack.pop();
902 parser->at_stack.pop();
904 BaseType *parent = parser->bt_stack.top();
910 "Tried to add the simple-type variable '%s' to a non-constructor type (%s %s).",
912 parser->bt_stack.top()->
914 parser->bt_stack.top()->name().
919 "Expected an end tag for a simple type; found '%s' instead.",
924 parser->finish_variable(localname,
dods_array_c,
"Array");
927 case inside_dimension:
928 if (strcmp(localname,
"dimension") == 0)
932 "Expected an end dimension tag; found '%s' instead.",
936 case inside_structure:
940 case inside_sequence:
945 parser->finish_variable(localname,
dods_grid_c,
"Grid");
949 parser->finish_variable(localname,
dods_array_c,
"Map");
952 case inside_blob_href:
953 if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0)
957 "Expected an end dataBLOB/blob tag; found '%s' instead.",
970 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
980 switch (parser->get_state()) {
981 case inside_attribute_value:
982 parser->char_data.append((
const char *)(ch), len);
983 DBG2(cerr <<
"Characters: '" << parser->char_data <<
"'" << endl);
986 case inside_other_xml_attribute:
987 parser->other_xml.append((
const char *)(ch), len);
988 DBG2(cerr <<
"Other XML Characters: '" << parser->other_xml <<
"'" << endl);
1005 switch (parser->get_state()) {
1006 case inside_other_xml_attribute:
1007 parser->other_xml.append((
const char *)(ch), len);
1024 switch (parser->get_state()) {
1025 case inside_other_xml_attribute:
1026 parser->other_xml.append((
const char *)(value), len);
1029 case parser_unknown:
1034 "Found a CData block but none are allowed by DAP.");
1046 return xmlGetPredefinedEntity(name);
1061 parser->set_state(parser_error);
1063 va_start(args, msg);
1065 vsnprintf(str, 1024, msg, args);
1068 int line = xmlSAX2GetLineNumber(parser->ctxt);
1071 parser->error_msg += string(str) + string(
"\n");
1076 void DDXParser::cleanup_parse(xmlParserCtxtPtr & context)
const 1078 if (!context->wellFormed) {
1079 context->sax = NULL;
1080 xmlFreeParserCtxt(context);
1083 (
"\nThe DDX is not a well formed XML document.\n")
1087 if (!context->valid) {
1088 context->sax = NULL;
1089 xmlFreeParserCtxt(context);
1090 throw DDXParseFailed(
string(
"\nThe DDX is not a valid document.\n")
1094 if (get_state() == parser_error) {
1095 context->sax = NULL;
1096 xmlFreeParserCtxt(context);
1101 context->sax = NULL;
1102 xmlFreeParserCtxt(context);
1108 const string &boundary)
1112 if (!in || feof(in) || ferror(in))
1114 "Input stream not open or read error");
1116 const int size = 1024;
1119 int res = fread(chars, 1, 4, in);
1121 xmlParserCtxtPtr context =
1122 xmlCreatePushParserCtxt(NULL, NULL, chars, res,
"stream");
1128 xmlSAXHandler ddx_sax_parser;
1129 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1140 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1144 context->sax = &ddx_sax_parser;
1145 context->userData =
this;
1146 context->validate =
true;
1148 while ((fgets(chars, size, in) > 0) && !
is_boundary(chars, boundary)) {
1149 DBG(cerr <<
"line: " << chars << endl);
1150 xmlParseChunk(ctxt, chars, strlen(chars), 0);
1154 xmlParseChunk(ctxt, chars, 0, 1);
1156 cleanup_parse(context);
1181 xmlParserCtxtPtr context = xmlCreateFileParserCtxt(document.c_str());
1185 (
"Could not initialize the parser with the file: '")
1186 + document +
string(
"'."));
1192 xmlSAXHandler ddx_sax_parser;
1193 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1204 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1208 context->sax = &ddx_sax_parser;
1209 context->userData =
this;
1210 context->validate =
false;
1212 xmlParseDocument(context);
1214 cleanup_parse(context);
void intern_stream(FILE *in, DDS *dds, string &cid, const string &boundary="")
Read the DDX from a stream instead of a file.
Contains the attributes for a dataset.
virtual Int16 * NewInt16(const string &n="") const
std::vector< BaseType * >::iterator Vars_iter
static void ddx_start_document(void *parser)
virtual Structure * NewStructure(const string &n="") const
virtual UInt32 * NewUInt32(const string &n="") const
virtual UInt16 * NewUInt16(const string &n="") const
static void ddx_get_cdata(void *parser, const xmlChar *value, int len)
Holds a structure (aggregate) type.
virtual void add_var(BaseType *bt, Part part=nil)
Add a variable.
Type
Identifies the data type.
A class for software fault reporting.
virtual bool is_vector_type()
Returns true if the instance is a vector (i.e., array) type variable.
virtual bool is_constructor_type()
Returns true if the instance is a constructor (i.e., Structure, Sequence or Grid) type variable...
static void ddx_fatal_error(void *parser, const char *msg,...)
virtual Array * NewArray(const string &n="", BaseType *v=0) const
static void ddx_end_document(void *parser)
static xmlEntityPtr ddx_get_entity(void *parser, const xmlChar *name)
static void ddx_get_characters(void *parser, const xmlChar *ch, int len)
virtual Float32 * NewFloat32(const string &n="") const
virtual AttrTable & get_attr_table()
virtual Grid * NewGrid(const string &n="") const
void set_dap_version(const string &version_string)
static void ddx_sax2_end_element(void *parser, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI)
static void ddx_sax2_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)
string long_to_string(long val, int base)
virtual Sequence * NewSequence(const string &n="") const
bool is_boundary(const char *line, const string &boundary)
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
void set_dataset_name(const string &n)
virtual Str * NewStr(const string &n="") const
The basic data type for the DODS DAP types.
virtual Int32 * NewInt32(const string &n="") const
virtual Float64 * NewFloat64(const string &n="") const
virtual Url * NewUrl(const string &n="") const
static void ddx_ignoreable_whitespace(void *parser, const xmlChar *ch, int len)
void intern(const string &document, DDS *dest_dds, string &cid)
void add_var(BaseType *bt)
Adds a copy of the variable to the DDS. Using the ptr_duplicate() method, perform a deep copy on the ...
virtual Byte * NewByte(const string &n="") const