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());
392 ap->append_dim(atoi(attribute_table[
"size"].value.c_str()),
393 attribute_table[
"name"].value);
399 void DDXParser::process_blob(
const xmlChar **attrs,
int nb_attributes)
401 transfer_xml_attrs(attrs, nb_attributes);
402 if (check_required_attribute(
string(
"href"))) {
403 set_state(inside_blob_href);
404 *blob_href = attribute_table[
"href"].value;
415 DDXParser::is_attribute_or_alias(
const char *name,
const xmlChar **attrs,
418 if (strcmp(name,
"Attribute") == 0) {
419 process_attribute_element(attrs, nb_attributes);
423 else if (strcmp(name,
"Alias") == 0) {
424 process_attribute_alias(attrs, nb_attributes);
437 inline bool DDXParser::is_variable(
const char *name,
const xmlChar **attrs,
442 process_variable(t, inside_simple_type, attrs, nb_attributes);
445 else if (strcmp(name,
"Array") == 0) {
446 process_variable(
dods_array_c, inside_array, attrs, nb_attributes);
449 else if (strcmp(name,
"Structure") == 0) {
453 else if (strcmp(name,
"Sequence") == 0) {
454 process_variable(
dods_sequence_c, inside_sequence, attrs, nb_attributes);
457 else if (strcmp(name,
"Grid") == 0) {
458 process_variable(
dods_grid_c, inside_grid, attrs, nb_attributes);
465 void DDXParser::finish_variable(
const char *tag,
Type t,
const char *expected)
467 if (strcmp(tag, expected) != 0) {
469 "Expected an end tag for a %s; found '%s' instead.",
476 BaseType *btp = bt_stack.top();
481 if (btp->type() != t) {
483 "Internal error: Expected a %s variable.",
489 && dynamic_cast < Array * >(btp)->dimensions() == 0) {
491 "No dimension element included in the Array '%s'.",
492 btp->name().c_str());
496 BaseType *parent = bt_stack.top();
498 if (!(parent->is_vector_type() || parent->is_constructor_type())) {
500 "Tried to add the array variable '%s' to a non-constructor type (%s %s).",
502 bt_stack.top()->type_name().c_str(),
503 bt_stack.top()->name().c_str());
507 parent->add_var(btp);
524 parser->error_msg =
"";
525 parser->char_data =
"";
533 parser->bt_stack.push(
new Structure(
"dummy_dds"));
535 parser->set_state(parser_start);
537 DBG2(cerr <<
"Parser state: " << states[parser->get_state()] << endl);
545 DBG2(cerr <<
"Ending state == " << states[parser->get_state()] <<
548 if (parser->get_state() != parser_start)
550 "The document contained unbalanced tags.");
554 if (parser->get_state() == parser_error)
561 ddx_fatal_error(parser,
"Parse error: Expected a Structure, Sequence or Grid variable.");
569 parser->bt_stack.pop();
574 const xmlChar *l,
const xmlChar *prefix,
const xmlChar *URI,
575 int nb_namespaces,
const xmlChar **namespaces,
576 int nb_attributes,
int ,
const xmlChar **attributes)
579 const char *localname = (
const char *)l;
581 DBG2(cerr <<
"start element: " << localname <<
", states: "
582 << states[parser->get_state()]);
584 switch (parser->get_state()) {
586 if (strcmp(localname,
"Dataset") == 0) {
587 parser->set_state(inside_dataset);
588 parser->root_ns = URI != 0 ? (
const char *)URI:
"";
589 parser->transfer_xml_attrs(attributes, nb_attributes);
591 if (parser->check_required_attribute(
string(
"name")))
594 if (parser->check_attribute(
"dapVersion"))
595 parser->dds->
set_dap_version(parser->attribute_table[
"dapVersion"].value);
599 "Expected response to start with a Dataset element; found '%s' instead.",
604 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
606 else if (parser->is_variable(localname, attributes, nb_attributes))
608 else if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0) {
609 parser->process_blob(attributes, nb_attributes);
614 "Expected an Attribute, Alias or variable element; found '%s' instead.",
618 case inside_attribute_container:
619 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
623 "Expected an Attribute or Alias element; found '%s' instead.",
627 case inside_attribute:
628 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
630 else if (strcmp(localname,
"value") == 0)
631 parser->set_state(inside_attribute_value);
634 "Expected an 'Attribute', 'Alias' or 'value' element; found '%s' instead.",
638 case inside_attribute_value:
640 "Internal parser error; unexpected state, inside value while processing element '%s'.",
644 case inside_other_xml_attribute:
645 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname << endl);
647 parser->other_xml_depth++;
651 parser->other_xml.append(
"<");
653 parser->other_xml.append((
const char *)prefix);
654 parser->other_xml.append(
":");
656 parser->other_xml.append(localname);
658 if (nb_namespaces != 0) {
659 parser->transfer_xml_ns(namespaces, nb_namespaces);
661 for (map<string,string>::iterator i = parser->namespace_table.begin();
662 i != parser->namespace_table.end();
664 parser->other_xml.append(
" xmlns");
665 if (!i->first.empty()) {
666 parser->other_xml.append(
":");
667 parser->other_xml.append(i->first);
669 parser->other_xml.append(
"=\"");
670 parser->other_xml.append(i->second);
671 parser->other_xml.append(
"\"");
675 if (nb_attributes != 0) {
676 parser->transfer_xml_attrs(attributes, nb_attributes);
677 for (XMLAttrMap::iterator i = parser->attr_table_begin();
678 i != parser->attr_table_end();
680 parser->other_xml.append(
" ");
681 if (!i->second.prefix.empty()) {
682 parser->other_xml.append(i->second.prefix);
683 parser->other_xml.append(
":");
685 parser->other_xml.append(i->first);
686 parser->other_xml.append(
"=\"");
687 parser->other_xml.append(i->second.value);
688 parser->other_xml.append(
"\"");
692 parser->other_xml.append(
">");
697 "Internal parser error; unexpected state, inside alias while processing element '%s'.",
701 case inside_simple_type:
702 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
706 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
711 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
713 else if (is_not(localname,
"Array")
714 && parser->is_variable(localname, attributes, nb_attributes))
716 else if (strcmp(localname,
"dimension") == 0) {
717 parser->process_dimension(attributes, nb_attributes);
722 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
726 case inside_dimension:
728 "Internal parser error; unexpected state, inside dimension while processing element '%s'.",
732 case inside_structure:
733 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
735 else if (parser->is_variable(localname, attributes, nb_attributes))
739 "Expected an Attribute, Alias or variable element; found '%s' instead.",
743 case inside_sequence:
744 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
746 else if (parser->is_variable(localname, attributes, nb_attributes))
750 "Expected an Attribute, Alias or variable element; found '%s' instead.",
755 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
757 else if (strcmp(localname,
"Array") == 0)
758 parser->process_variable(
dods_array_c, inside_array, attributes, nb_attributes);
759 else if (strcmp(localname,
"Map") == 0)
760 parser->process_variable(
dods_array_c, inside_map, attributes, nb_attributes);
763 "Expected an Attribute, Alias or variable element; found '%s' instead.",
768 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
770 else if (is_not(localname,
"Array") && is_not(localname,
"Sequence")
771 && is_not(localname,
"Grid")
772 && parser->is_variable(localname, attributes, nb_attributes))
774 else if (strcmp(localname,
"dimension") == 0) {
775 parser->process_dimension(attributes, nb_attributes);
780 "Expected an 'Attribute', 'Alias', variable or 'dimension' element; found '%s' instead.",
784 case inside_blob_href:
786 "Internal parser error; unexpected state, inside blob href while processing element '%s'.",
792 parser->set_state(parser_unknown);
799 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
803 const xmlChar *prefix,
const xmlChar *URI)
806 const char *localname = (
const char *)l;
808 DBG2(cerr <<
"End element " << localname <<
" (state "
809 << states[parser->get_state()] <<
")" << endl);
811 switch (parser->get_state()) {
814 "Internal parser error; unexpected state, inside start state while processing element '%s'.",
819 if (strcmp(localname,
"Dataset") == 0)
823 "Expected an end Dataset tag; found '%s' instead.",
827 case inside_attribute_container:
828 if (strcmp(localname,
"Attribute") == 0) {
830 parser->at_stack.pop();
834 "Expected an end Attribute tag; found '%s' instead.",
838 case inside_attribute:
839 if (strcmp(localname,
"Attribute") == 0)
843 "Expected an end Attribute tag; found '%s' instead.",
847 case inside_attribute_value:
848 if (strcmp(localname,
"value") == 0) {
852 parser->dods_attr_type, parser->char_data);
853 parser->char_data =
"";
857 "Expected an end value tag; found '%s' instead.",
862 case inside_other_xml_attribute: {
863 if (strcmp(localname,
"Attribute") == 0
864 && parser->root_ns == (
const char *)URI) {
866 DBGN(cerr << endl <<
"\t Popping the 'inside_other_xml_attribute' state"
873 parser->dods_attr_type, parser->other_xml);
875 parser->other_xml =
"";
878 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname
879 <<
", depth: " << parser->other_xml_depth << endl);
880 if (parser->other_xml_depth == 0)
882 "Expected an OtherXML attribute to end! Instead I found '%s'",
884 parser->other_xml_depth--;
886 parser->other_xml.append(
"</");
888 parser->other_xml.append((
const char *)prefix);
889 parser->other_xml.append(
":");
891 parser->other_xml.append(localname);
892 parser->other_xml.append(
">");
901 case inside_simple_type:
904 BaseType *btp = parser->bt_stack.top();
905 parser->bt_stack.pop();
906 parser->at_stack.pop();
908 BaseType *parent = parser->bt_stack.top();
914 "Tried to add the simple-type variable '%s' to a non-constructor type (%s %s).",
916 parser->bt_stack.top()->
918 parser->bt_stack.top()->name().
923 "Expected an end tag for a simple type; found '%s' instead.",
928 parser->finish_variable(localname,
dods_array_c,
"Array");
931 case inside_dimension:
932 if (strcmp(localname,
"dimension") == 0)
936 "Expected an end dimension tag; found '%s' instead.",
940 case inside_structure:
944 case inside_sequence:
949 parser->finish_variable(localname,
dods_grid_c,
"Grid");
953 parser->finish_variable(localname,
dods_array_c,
"Map");
956 case inside_blob_href:
957 if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0)
961 "Expected an end dataBLOB/blob tag; found '%s' instead.",
974 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
984 switch (parser->get_state()) {
985 case inside_attribute_value:
986 parser->char_data.append((
const char *)(ch), len);
987 DBG2(cerr <<
"Characters: '" << parser->char_data <<
"'" << endl);
990 case inside_other_xml_attribute:
991 parser->other_xml.append((
const char *)(ch), len);
992 DBG2(cerr <<
"Other XML Characters: '" << parser->other_xml <<
"'" << endl);
1009 switch (parser->get_state()) {
1010 case inside_other_xml_attribute:
1011 parser->other_xml.append((
const char *)(ch), len);
1028 switch (parser->get_state()) {
1029 case inside_other_xml_attribute:
1030 parser->other_xml.append((
const char *)(value), len);
1033 case parser_unknown:
1038 "Found a CData block but none are allowed by DAP.");
1050 return xmlGetPredefinedEntity(name);
1065 parser->set_state(parser_error);
1067 va_start(args, msg);
1069 vsnprintf(str, 1024, msg, args);
1072 int line = xmlSAX2GetLineNumber(parser->ctxt);
1075 parser->error_msg += string(str) + string(
"\n");
1080 void DDXParser::cleanup_parse(xmlParserCtxtPtr & context)
const
1082 if (!context->wellFormed) {
1083 context->sax = NULL;
1084 xmlFreeParserCtxt(context);
1087 (
"\nThe DDX is not a well formed XML document.\n")
1091 if (!context->valid) {
1092 context->sax = NULL;
1093 xmlFreeParserCtxt(context);
1094 throw DDXParseFailed(
string(
"\nThe DDX is not a valid document.\n")
1098 if (get_state() == parser_error) {
1099 context->sax = NULL;
1100 xmlFreeParserCtxt(context);
1101 throw DDXParseFailed(
string(
"\nError parsing DDX response.\n") +
1105 context->sax = NULL;
1106 xmlFreeParserCtxt(context);
1112 const string &boundary)
1116 if (!in || feof(in) || ferror(in))
1118 "Input stream not open or read error");
1120 const int size = 1024;
1123 int res = fread(chars, 1, 4, in);
1125 xmlParserCtxtPtr context =
1126 xmlCreatePushParserCtxt(NULL, NULL, chars, res,
"stream");
1132 xmlSAXHandler ddx_sax_parser;
1133 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1144 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1148 context->sax = &ddx_sax_parser;
1149 context->userData =
this;
1150 context->validate =
true;
1153 while ((fgets(chars, size, in) > 0) && !
is_boundary(chars, boundary)) {
1154 chars[size-1] =
'\0';
1155 DBG(cerr <<
"line: " << chars << endl);
1156 xmlParseChunk(ctxt, chars, strlen(chars), 0);
1160 xmlParseChunk(ctxt, chars, 0, 1);
1162 cleanup_parse(context);
1187 xmlParserCtxtPtr context = xmlCreateFileParserCtxt(document.c_str());
1191 (
"Could not initialize the parser with the file: '")
1192 + document +
string(
"'."));
1198 xmlSAXHandler ddx_sax_parser;
1199 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1210 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1214 context->sax = &ddx_sax_parser;
1215 context->userData =
this;
1216 context->validate =
false;
1218 xmlParseDocument(context);
1220 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 Structure * NewStructure(const string &n="") const
virtual Sequence * NewSequence(const string &n="") const
std::vector< BaseType * >::iterator Vars_iter
virtual Byte * NewByte(const string &n="") const
virtual Str * NewStr(const string &n="") const
static void ddx_start_document(void *parser)
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.
virtual Url * NewUrl(const string &n="") const
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 Float32 * NewFloat32(const string &n="") const
virtual Float64 * NewFloat64(const string &n="") 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 AttrTable & get_attr_table()
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)
virtual Array * NewArray(const string &n="", BaseType *v=0) const
string long_to_string(long val, int base)
bool is_boundary(const char *line, const string &boundary)
virtual Grid * NewGrid(const string &n="") const
virtual Int16 * NewInt16(const string &n="") const
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)
The basic data type for the DODS DAP types.
virtual Int32 * NewInt32(const string &n="") const
virtual UInt16 * NewUInt16(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)
virtual UInt32 * NewUInt32(const string &n="") const
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 ...