57 #if defined(DODS_DEBUG) || defined(DODS_DEUG2)
58 static const char *states[] =
64 "attribute_container",
67 "other_xml_attribute",
91 BaseType *DDXParser::factory(
Type t,
const string & name)
95 return d_factory->
NewByte(name);
123 return d_factory->
NewStr(name);
127 return d_factory->
NewUrl(name);
143 return d_factory->
NewGrid(name);
155 if (strcmp(name,
"Byte") == 0)
158 if (strcmp(name,
"Int16") == 0)
161 if (strcmp(name,
"UInt16") == 0)
164 if (strcmp(name,
"Int32") == 0)
167 if (strcmp(name,
"UInt32") == 0)
170 if (strcmp(name,
"Float32") == 0)
173 if (strcmp(name,
"Float64") == 0)
176 if (strcmp(name,
"String") == 0)
179 if (strcmp(name,
"Url") == 0)
182 if (strcmp(name,
"Array") == 0)
185 if (strcmp(name,
"Structure") == 0)
188 if (strcmp(name,
"Sequence") == 0)
191 if (strcmp(name,
"Grid") == 0)
220 static bool is_not(
const char *name,
const char *tag)
222 return strcmp(name, tag) != 0;
225 void DDXParser::set_state(DDXParser::ParseState state)
230 DDXParser::ParseState DDXParser::get_state()
const
235 void DDXParser::pop_state()
243 void DDXParser::transfer_xml_attrs(
const xmlChar **attributes,
int nb_attributes)
245 if (!attribute_table.empty())
246 attribute_table.clear();
248 unsigned int index = 0;
249 for (
int i = 0; i < nb_attributes; ++i, index += 5) {
252 attribute_table.insert(map<string, XMLAttribute>::value_type(
253 string((
const char *)attributes[index]),
254 XMLAttribute(attributes + index + 1)));
256 DBG(cerr <<
"Attribute '" << (
const char *)attributes[index] <<
"': "
257 << attribute_table[(
const char *)attributes[index]].value << endl);
261 void DDXParser::transfer_xml_ns(
const xmlChar **namespaces,
int nb_namespaces)
263 for (
int i = 0; i < nb_namespaces; ++i ) {
266 namespace_table.insert(map<string,string>::value_type(
267 namespaces[i*2] != 0 ? (
const char *)namespaces[i*2] :
"",
268 (
const char *)namespaces[i*2+1]));
276 bool DDXParser::check_required_attribute(
const string & attr)
278 map < string, XMLAttribute >::iterator i = attribute_table.find(attr);
279 if (i == attribute_table.end())
290 bool DDXParser::check_attribute(
const string & attr)
292 return (attribute_table.find(attr) != attribute_table.end());
303 void DDXParser::process_attribute_element(
const xmlChar **attrs,
int nb_attributes)
306 transfer_xml_attrs(attrs, nb_attributes);
308 bool error = !(check_required_attribute(
string(
"name"))
309 && check_required_attribute(
string(
"type")));
313 if (attribute_table[
"type"].value ==
"Container") {
314 set_state(inside_attribute_container);
317 AttrTable *parent = at_stack.top();
319 child = parent->append_container(attribute_table[
"name"].value);
320 at_stack.push(child);
321 DBG2(cerr <<
"Pushing at" << endl);
323 else if (attribute_table[
"type"].value ==
"OtherXML") {
324 set_state(inside_other_xml_attribute);
326 dods_attr_name = attribute_table[
"name"].value;
327 dods_attr_type = attribute_table[
"type"].value;
330 set_state(inside_attribute);
334 dods_attr_name = attribute_table[
"name"].value;
335 dods_attr_type = attribute_table[
"type"].value;
342 void DDXParser::process_attribute_alias(
const xmlChar **attrs,
int nb_attributes)
344 transfer_xml_attrs(attrs, nb_attributes);
345 if (check_required_attribute(
string(
"name"))
346 && check_required_attribute(
string(
"attribute"))) {
347 set_state(inside_alias);
348 at_stack.top()->attr_alias(attribute_table[
"name"].value,
349 attribute_table[
"attribute"].value);
360 void DDXParser::process_variable(
Type t, ParseState s,
const xmlChar **attrs,
363 transfer_xml_attrs(attrs, nb_attributes);
367 || check_required_attribute(
"name")) {
368 BaseType *btp = factory(t, attribute_table[
"name"].value);
372 "Internal parser error; could not instantiate the variable '%s'.",
373 attribute_table[
"name"].value.c_str());
380 at_stack.push(&btp->get_attr_table());
387 void DDXParser::process_dimension(
const xmlChar **attrs,
int nb_attributes)
389 transfer_xml_attrs(attrs, nb_attributes);
390 if (check_required_attribute(
string(
"size"))) {
391 set_state(inside_dimension);
392 Array *ap = dynamic_cast < Array * >(bt_stack.top());
398 ap->append_dim(atoi(attribute_table[
"size"].value.c_str()),
399 attribute_table[
"name"].value);
405 void DDXParser::process_blob(
const xmlChar **attrs,
int nb_attributes)
407 transfer_xml_attrs(attrs, nb_attributes);
408 if (check_required_attribute(
string(
"href"))) {
409 set_state(inside_blob_href);
410 *blob_href = attribute_table[
"href"].value;
421 DDXParser::is_attribute_or_alias(
const char *name,
const xmlChar **attrs,
424 if (strcmp(name,
"Attribute") == 0) {
425 process_attribute_element(attrs, nb_attributes);
429 else if (strcmp(name,
"Alias") == 0) {
430 process_attribute_alias(attrs, nb_attributes);
443 inline bool DDXParser::is_variable(
const char *name,
const xmlChar **attrs,
449 process_variable(t, inside_simple_type, attrs, nb_attributes);
452 else if (strcmp(name,
"Array") == 0) {
453 process_variable(
dods_array_c, inside_array, attrs, nb_attributes);
456 else if (strcmp(name,
"Structure") == 0) {
460 else if (strcmp(name,
"Sequence") == 0) {
461 process_variable(
dods_sequence_c, inside_sequence, attrs, nb_attributes);
464 else if (strcmp(name,
"Grid") == 0) {
465 process_variable(
dods_grid_c, inside_grid, attrs, nb_attributes);
472 void DDXParser::finish_variable(
const char *tag,
Type t,
const char *expected)
474 if (strcmp(tag, expected) != 0) {
476 "Expected an end tag for a %s; found '%s' instead.",
483 BaseType *btp = bt_stack.top();
488 if (btp->type() != t) {
490 "Internal error: Expected a %s variable.",
496 && static_cast<Array*>(btp)->dimensions() == 0) {
498 "No dimension element included in the Array '%s'.",
499 btp->name().c_str());
504 BaseType *parent = bt_stack.top();
506 if (!(parent->is_vector_type() || parent->is_constructor_type())) {
508 "Tried to add the array variable '%s' to a non-constructor type (%s %s).",
510 bt_stack.top()->type_name().c_str(),
511 bt_stack.top()->name().c_str());
516 parent->add_var(btp);
534 parser->error_msg =
"";
535 parser->char_data =
"";
543 parser->bt_stack.push(
new Structure(
"dummy_dds"));
545 parser->set_state(parser_start);
547 DBG2(cerr <<
"Parser state: " << states[parser->get_state()] << endl);
555 DBG2(cerr <<
"Ending state == " << states[parser->get_state()] <<
558 if (parser->get_state() != parser_start)
563 if (parser->get_state() == parser_error) {
564 delete parser->bt_stack.top();
572 ddx_fatal_error(parser,
"Parse error: Expected a Structure, Sequence or Grid variable.");
582 parser->bt_stack.pop();
587 const xmlChar *l,
const xmlChar *prefix,
const xmlChar *URI,
588 int nb_namespaces,
const xmlChar **namespaces,
589 int nb_attributes,
int ,
const xmlChar **attributes)
592 const char *localname = (
const char *)l;
594 DBG2(cerr <<
"start element: " << localname <<
", states: "
595 << states[parser->get_state()]);
597 switch (parser->get_state()) {
599 if (strcmp(localname,
"Dataset") == 0) {
600 parser->set_state(inside_dataset);
601 parser->root_ns = URI != 0 ? (
const char *)URI:
"";
602 parser->transfer_xml_attrs(attributes, nb_attributes);
604 if (parser->check_required_attribute(
string(
"name")))
607 if (parser->check_attribute(
"dapVersion"))
608 parser->dds->
set_dap_version(parser->attribute_table[
"dapVersion"].value);
612 "Expected response to start with a Dataset element; found '%s' instead.",
617 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
619 else if (parser->is_variable(localname, attributes, nb_attributes))
621 else if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0) {
622 parser->process_blob(attributes, nb_attributes);
627 "Expected an Attribute, Alias or variable element; found '%s' instead.",
631 case inside_attribute_container:
632 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
636 "Expected an Attribute or Alias element; found '%s' instead.",
640 case inside_attribute:
641 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
643 else if (strcmp(localname,
"value") == 0)
644 parser->set_state(inside_attribute_value);
647 "Expected an 'Attribute', 'Alias' or 'value' element; found '%s' instead.",
651 case inside_attribute_value:
653 "Internal parser error; unexpected state, inside value while processing element '%s'.",
657 case inside_other_xml_attribute:
658 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname << endl);
660 parser->other_xml_depth++;
664 parser->other_xml.append(
"<");
666 parser->other_xml.append((
const char *)prefix);
667 parser->other_xml.append(
":");
669 parser->other_xml.append(localname);
671 if (nb_namespaces != 0) {
672 parser->transfer_xml_ns(namespaces, nb_namespaces);
674 for (map<string,string>::iterator i = parser->namespace_table.begin();
675 i != parser->namespace_table.end();
677 parser->other_xml.append(
" xmlns");
678 if (!i->first.empty()) {
679 parser->other_xml.append(
":");
680 parser->other_xml.append(i->first);
682 parser->other_xml.append(
"=\"");
683 parser->other_xml.append(i->second);
684 parser->other_xml.append(
"\"");
688 if (nb_attributes != 0) {
689 parser->transfer_xml_attrs(attributes, nb_attributes);
690 for (XMLAttrMap::iterator i = parser->attr_table_begin();
691 i != parser->attr_table_end();
693 parser->other_xml.append(
" ");
694 if (!i->second.prefix.empty()) {
695 parser->other_xml.append(i->second.prefix);
696 parser->other_xml.append(
":");
698 parser->other_xml.append(i->first);
699 parser->other_xml.append(
"=\"");
700 parser->other_xml.append(i->second.value);
701 parser->other_xml.append(
"\"");
705 parser->other_xml.append(
">");
710 "Internal parser error; unexpected state, inside alias while processing element '%s'.",
714 case inside_simple_type:
715 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
719 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
724 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
726 else if (is_not(localname,
"Array")
727 && parser->is_variable(localname, attributes, nb_attributes))
729 else if (strcmp(localname,
"dimension") == 0) {
730 parser->process_dimension(attributes, nb_attributes);
735 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
739 case inside_dimension:
741 "Internal parser error; unexpected state, inside dimension while processing element '%s'.",
745 case inside_structure:
746 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
748 else if (parser->is_variable(localname, attributes, nb_attributes))
752 "Expected an Attribute, Alias or variable element; found '%s' instead.",
756 case inside_sequence:
757 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
759 else if (parser->is_variable(localname, 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 (strcmp(localname,
"Array") == 0)
771 parser->process_variable(
dods_array_c, inside_array, attributes, nb_attributes);
772 else if (strcmp(localname,
"Map") == 0)
773 parser->process_variable(
dods_array_c, inside_map, attributes, nb_attributes);
776 "Expected an Attribute, Alias or variable element; found '%s' instead.",
781 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
783 else if (is_not(localname,
"Array") && is_not(localname,
"Sequence")
784 && is_not(localname,
"Grid")
785 && parser->is_variable(localname, attributes, nb_attributes))
787 else if (strcmp(localname,
"dimension") == 0) {
788 parser->process_dimension(attributes, nb_attributes);
793 "Expected an 'Attribute', 'Alias', variable or 'dimension' element; found '%s' instead.",
797 case inside_blob_href:
799 "Internal parser error; unexpected state, inside blob href while processing element '%s'.",
805 parser->set_state(parser_unknown);
812 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
816 const xmlChar *prefix,
const xmlChar *URI)
819 const char *localname = (
const char *)l;
821 DBG2(cerr <<
"End element " << localname <<
" (state "
822 << states[parser->get_state()] <<
")" << endl);
824 switch (parser->get_state()) {
827 "Internal parser error; unexpected state, inside start state while processing element '%s'.",
832 if (strcmp(localname,
"Dataset") == 0)
836 "Expected an end Dataset tag; found '%s' instead.",
840 case inside_attribute_container:
841 if (strcmp(localname,
"Attribute") == 0) {
843 parser->at_stack.pop();
847 "Expected an end Attribute tag; found '%s' instead.",
851 case inside_attribute:
852 if (strcmp(localname,
"Attribute") == 0)
856 "Expected an end Attribute tag; found '%s' instead.",
860 case inside_attribute_value:
861 if (strcmp(localname,
"value") == 0) {
865 parser->dods_attr_type, parser->char_data);
866 parser->char_data =
"";
870 "Expected an end value tag; found '%s' instead.",
875 case inside_other_xml_attribute: {
876 if (strcmp(localname,
"Attribute") == 0
877 && parser->root_ns == (
const char *)URI) {
879 DBGN(cerr << endl <<
"\t Popping the 'inside_other_xml_attribute' state"
886 parser->dods_attr_type, parser->other_xml);
888 parser->other_xml =
"";
891 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname
892 <<
", depth: " << parser->other_xml_depth << endl);
893 if (parser->other_xml_depth == 0)
895 "Expected an OtherXML attribute to end! Instead I found '%s'",
897 parser->other_xml_depth--;
899 parser->other_xml.append(
"</");
901 parser->other_xml.append((
const char *)prefix);
902 parser->other_xml.append(
":");
904 parser->other_xml.append(localname);
905 parser->other_xml.append(
">");
914 case inside_simple_type: {
918 BaseType *btp = parser->bt_stack.top();
919 parser->bt_stack.pop();
920 parser->at_stack.pop();
922 BaseType *parent = parser->bt_stack.top();
930 "Tried to add the simple-type variable '%s' to a non-constructor type (%s %s).",
932 parser->bt_stack.top()->
934 parser->bt_stack.top()->name().
939 "Expected an end tag for a simple type; found '%s' instead.",
945 parser->finish_variable(localname,
dods_array_c,
"Array");
948 case inside_dimension:
949 if (strcmp(localname,
"dimension") == 0)
953 "Expected an end dimension tag; found '%s' instead.",
957 case inside_structure:
961 case inside_sequence:
966 parser->finish_variable(localname,
dods_grid_c,
"Grid");
970 parser->finish_variable(localname,
dods_array_c,
"Map");
973 case inside_blob_href:
974 if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0)
978 "Expected an end dataBLOB/blob tag; found '%s' instead.",
991 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
1001 switch (parser->get_state()) {
1002 case inside_attribute_value:
1003 parser->char_data.append((
const char *)(ch), len);
1004 DBG2(cerr <<
"Characters: '" << parser->char_data <<
"'" << endl);
1007 case inside_other_xml_attribute:
1008 parser->other_xml.append((
const char *)(ch), len);
1009 DBG2(cerr <<
"Other XML Characters: '" << parser->other_xml <<
"'" << endl);
1026 switch (parser->get_state()) {
1027 case inside_other_xml_attribute:
1028 parser->other_xml.append((
const char *)(ch), len);
1045 switch (parser->get_state()) {
1046 case inside_other_xml_attribute:
1047 parser->other_xml.append((
const char *)(value), len);
1050 case parser_unknown:
1055 "Found a CData block but none are allowed by DAP.");
1067 return xmlGetPredefinedEntity(name);
1082 parser->set_state(parser_error);
1084 va_start(args, msg);
1086 vsnprintf(str, 1024, msg, args);
1089 int line = xmlSAX2GetLineNumber(parser->ctxt);
1092 parser->error_msg += string(str) + string(
"\n");
1097 void DDXParser::cleanup_parse(xmlParserCtxtPtr & context)
const
1099 if (!context->wellFormed) {
1100 context->sax = NULL;
1101 xmlFreeParserCtxt(context);
1104 (
"\nThe DDX is not a well formed XML document.\n")
1108 if (!context->valid) {
1109 context->sax = NULL;
1110 xmlFreeParserCtxt(context);
1111 throw DDXParseFailed(
string(
"\nThe DDX is not a valid document.\n")
1115 if (get_state() == parser_error) {
1116 context->sax = NULL;
1117 xmlFreeParserCtxt(context);
1118 throw DDXParseFailed(
string(
"\nError parsing DDX response.\n") +
1122 context->sax = NULL;
1123 xmlFreeParserCtxt(context);
1136 if (!in || in.eof())
1137 throw InternalErr(__FILE__, __LINE__,
"Input stream not open or read error");
1139 const int size = 1024;
1140 char chars[size + 1];
1143 in.readsome(chars, 4);
1144 int res = in.gcount();
1147 xmlParserCtxtPtr context = xmlCreatePushParserCtxt(NULL, NULL, chars, res,
"stream");
1153 xmlSAXHandler ddx_sax_parser;
1154 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1165 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1169 context->sax = &ddx_sax_parser;
1170 context->userData =
this;
1171 context->validate =
true;
1173 in.getline(chars, size);
1175 chars[res-1] =
'\n';
1177 while (res > 0 && !
is_boundary(chars, boundary)) {
1178 DBG(cerr <<
"line (" << res <<
"): " << chars << endl);
1179 xmlParseChunk(ctxt, chars, res, 0);
1181 in.getline(chars, size);
1184 chars[res-1] =
'\n';
1191 xmlParseChunk(ctxt, chars, 0, 1);
1193 cleanup_parse(context);
1202 if (!in || feof(in) || ferror(in))
1204 "Input stream not open or read error");
1206 const int size = 1024;
1209 int res = fread(chars, 1, 4, in);
1212 xmlParserCtxtPtr context =
1213 xmlCreatePushParserCtxt(NULL, NULL, chars, res,
"stream");
1219 xmlSAXHandler ddx_sax_parser;
1220 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1231 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1235 context->sax = &ddx_sax_parser;
1236 context->userData =
this;
1237 context->validate =
true;
1240 while ((fgets(chars, size, in) > 0) && !
is_boundary(chars, boundary)) {
1242 DBG(cerr <<
"line (" << strlen(chars) <<
"): " << chars << endl);
1244 xmlParseChunk(ctxt, chars, strlen(chars), 0);
1248 xmlParseChunk(ctxt, chars, 0, 1);
1250 cleanup_parse(context);
1275 xmlParserCtxtPtr context = xmlCreateFileParserCtxt(document.c_str());
1279 (
"Could not initialize the parser with the file: '")
1280 + document +
string(
"'."));
1286 xmlSAXHandler ddx_sax_parser;
1287 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1298 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1302 context->sax = &ddx_sax_parser;
1303 context->userData =
this;
1304 context->validate =
false;
1306 xmlParseDocument(context);
1308 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
virtual void set_parent(BaseType *parent)
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,...)
ObjectType get_type(const string &value)
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)
bool is_simple_type(Type t)
Returns true if the instance is a numeric, string or URL type variable.
virtual AttrTable & get_attr_table()
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)
string type_name(Type t)
Returns the type of the class instance as a string.
virtual UInt32 * NewUInt32(const string &n="") const
void set_dap_version(const string &version_string="2.0")
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 ...