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);
368 || check_required_attribute(
"name")) {
369 BaseType *btp = factory(t, attribute_table[
"name"].value);
373 "Internal parser error; could not instantiate the variable '%s'.",
374 attribute_table[
"name"].value.c_str());
381 at_stack.push(&btp->get_attr_table());
388 void DDXParser::process_dimension(
const xmlChar **attrs,
int nb_attributes)
390 transfer_xml_attrs(attrs, nb_attributes);
391 if (check_required_attribute(
string(
"size"))) {
392 set_state(inside_dimension);
393 Array *ap = dynamic_cast < Array * >(bt_stack.top());
399 ap->append_dim(atoi(attribute_table[
"size"].value.c_str()),
400 attribute_table[
"name"].value);
406 void DDXParser::process_blob(
const xmlChar **attrs,
int nb_attributes)
408 transfer_xml_attrs(attrs, nb_attributes);
409 if (check_required_attribute(
string(
"href"))) {
410 set_state(inside_blob_href);
411 *blob_href = attribute_table[
"href"].value;
422 DDXParser::is_attribute_or_alias(
const char *name,
const xmlChar **attrs,
425 if (strcmp(name,
"Attribute") == 0) {
426 process_attribute_element(attrs, nb_attributes);
430 else if (strcmp(name,
"Alias") == 0) {
431 process_attribute_alias(attrs, nb_attributes);
444 inline bool DDXParser::is_variable(
const char *name,
const xmlChar **attrs,
450 process_variable(t, inside_simple_type, attrs, nb_attributes);
453 else if (strcmp(name,
"Array") == 0) {
454 process_variable(
dods_array_c, inside_array, attrs, nb_attributes);
457 else if (strcmp(name,
"Structure") == 0) {
461 else if (strcmp(name,
"Sequence") == 0) {
462 process_variable(
dods_sequence_c, inside_sequence, attrs, nb_attributes);
465 else if (strcmp(name,
"Grid") == 0) {
466 process_variable(
dods_grid_c, inside_grid, attrs, nb_attributes);
473 void DDXParser::finish_variable(
const char *tag,
Type t,
const char *expected)
475 if (strcmp(tag, expected) != 0) {
477 "Expected an end tag for a %s; found '%s' instead.",
484 BaseType *btp = bt_stack.top();
489 if (btp->type() != t) {
491 "Internal error: Expected a %s variable.",
498 && static_cast<Array*>(btp)->dimensions() == 0) {
500 "No dimension element included in the Array '%s'.",
501 btp->name().c_str());
506 BaseType *parent = bt_stack.top();
508 if (!(parent->is_vector_type() || parent->is_constructor_type())) {
510 "Tried to add the array variable '%s' to a non-constructor type (%s %s).",
512 bt_stack.top()->type_name().c_str(),
513 bt_stack.top()->name().c_str());
518 parent->add_var_nocopy(btp);
535 parser->error_msg =
"";
536 parser->char_data =
"";
544 parser->bt_stack.push(
new Structure(
"dummy_dds"));
546 parser->set_state(parser_start);
548 DBG2(cerr <<
"Parser state: " << states[parser->get_state()] << endl);
556 DBG2(cerr <<
"Ending state == " << states[parser->get_state()] <<
559 if (parser->get_state() != parser_start)
564 if (parser->get_state() == parser_error) {
572 delete parser->bt_stack.top();
573 parser->bt_stack.pop();
574 ddx_fatal_error(parser,
"Parse error: Expected a Structure, Sequence or Grid variable.");
583 delete parser->bt_stack.top();
584 parser->bt_stack.pop();
588 const xmlChar *l,
const xmlChar *prefix,
const xmlChar *URI,
589 int nb_namespaces,
const xmlChar **namespaces,
590 int nb_attributes,
int ,
const xmlChar **attributes)
593 const char *localname = (
const char *)l;
595 DBG2(cerr <<
"start element: " << localname <<
", states: "
596 << states[parser->get_state()]);
598 switch (parser->get_state()) {
600 if (strcmp(localname,
"Dataset") == 0) {
601 parser->set_state(inside_dataset);
602 parser->root_ns = URI != 0 ? (
const char *)URI:
"";
603 parser->transfer_xml_attrs(attributes, nb_attributes);
605 if (parser->check_required_attribute(
string(
"name")))
608 if (parser->check_attribute(
"dapVersion"))
609 parser->dds->
set_dap_version(parser->attribute_table[
"dapVersion"].value);
613 "Expected response to start with a Dataset element; found '%s' instead.",
618 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
620 else if (parser->is_variable(localname, attributes, nb_attributes))
622 else if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0) {
623 parser->process_blob(attributes, nb_attributes);
628 "Expected an Attribute, Alias or variable element; found '%s' instead.",
632 case inside_attribute_container:
633 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
637 "Expected an Attribute or Alias element; found '%s' instead.",
641 case inside_attribute:
642 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
644 else if (strcmp(localname,
"value") == 0)
645 parser->set_state(inside_attribute_value);
648 "Expected an 'Attribute', 'Alias' or 'value' element; found '%s' instead.",
652 case inside_attribute_value:
654 "Internal parser error; unexpected state, inside value while processing element '%s'.",
658 case inside_other_xml_attribute:
659 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname << endl);
661 parser->other_xml_depth++;
665 parser->other_xml.append(
"<");
667 parser->other_xml.append((
const char *)prefix);
668 parser->other_xml.append(
":");
670 parser->other_xml.append(localname);
672 if (nb_namespaces != 0) {
673 parser->transfer_xml_ns(namespaces, nb_namespaces);
675 for (map<string,string>::iterator i = parser->namespace_table.begin();
676 i != parser->namespace_table.end();
678 parser->other_xml.append(
" xmlns");
679 if (!i->first.empty()) {
680 parser->other_xml.append(
":");
681 parser->other_xml.append(i->first);
683 parser->other_xml.append(
"=\"");
684 parser->other_xml.append(i->second);
685 parser->other_xml.append(
"\"");
689 if (nb_attributes != 0) {
690 parser->transfer_xml_attrs(attributes, nb_attributes);
691 for (XMLAttrMap::iterator i = parser->attr_table_begin();
692 i != parser->attr_table_end();
694 parser->other_xml.append(
" ");
695 if (!i->second.prefix.empty()) {
696 parser->other_xml.append(i->second.prefix);
697 parser->other_xml.append(
":");
699 parser->other_xml.append(i->first);
700 parser->other_xml.append(
"=\"");
701 parser->other_xml.append(i->second.value);
702 parser->other_xml.append(
"\"");
706 parser->other_xml.append(
">");
711 "Internal parser error; unexpected state, inside alias while processing element '%s'.",
715 case inside_simple_type:
716 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
720 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
725 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
727 else if (is_not(localname,
"Array")
728 && parser->is_variable(localname, attributes, nb_attributes))
730 else if (strcmp(localname,
"dimension") == 0) {
731 parser->process_dimension(attributes, nb_attributes);
736 "Expected an 'Attribute' or 'Alias' element; found '%s' instead.",
740 case inside_dimension:
742 "Internal parser error; unexpected state, inside dimension while processing element '%s'.",
746 case inside_structure:
747 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
749 else if (parser->is_variable(localname, attributes, nb_attributes))
753 "Expected an Attribute, Alias or variable element; found '%s' instead.",
757 case inside_sequence:
758 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
760 else if (parser->is_variable(localname, attributes, nb_attributes))
764 "Expected an Attribute, Alias or variable element; found '%s' instead.",
769 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
771 else if (strcmp(localname,
"Array") == 0)
772 parser->process_variable(
dods_array_c, inside_array, attributes, nb_attributes);
773 else if (strcmp(localname,
"Map") == 0)
774 parser->process_variable(
dods_array_c, inside_map, attributes, nb_attributes);
777 "Expected an Attribute, Alias or variable element; found '%s' instead.",
782 if (parser->is_attribute_or_alias(localname, attributes, nb_attributes))
784 else if (is_not(localname,
"Array") && is_not(localname,
"Sequence")
785 && is_not(localname,
"Grid")
786 && parser->is_variable(localname, attributes, nb_attributes))
788 else if (strcmp(localname,
"dimension") == 0) {
789 parser->process_dimension(attributes, nb_attributes);
794 "Expected an 'Attribute', 'Alias', variable or 'dimension' element; found '%s' instead.",
798 case inside_blob_href:
800 "Internal parser error; unexpected state, inside blob href while processing element '%s'.",
806 parser->set_state(parser_unknown);
813 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
817 const xmlChar *prefix,
const xmlChar *URI)
820 const char *localname = (
const char *)l;
822 DBG2(cerr <<
"End element " << localname <<
" (state "
823 << states[parser->get_state()] <<
")" << endl);
825 switch (parser->get_state()) {
828 "Internal parser error; unexpected state, inside start state while processing element '%s'.",
833 if (strcmp(localname,
"Dataset") == 0)
837 "Expected an end Dataset tag; found '%s' instead.",
841 case inside_attribute_container:
842 if (strcmp(localname,
"Attribute") == 0) {
844 parser->at_stack.pop();
848 "Expected an end Attribute tag; found '%s' instead.",
852 case inside_attribute:
853 if (strcmp(localname,
"Attribute") == 0)
857 "Expected an end Attribute tag; found '%s' instead.",
861 case inside_attribute_value:
862 if (strcmp(localname,
"value") == 0) {
866 parser->dods_attr_type, parser->char_data);
867 parser->char_data =
"";
871 "Expected an end value tag; found '%s' instead.",
876 case inside_other_xml_attribute: {
877 if (strcmp(localname,
"Attribute") == 0
878 && parser->root_ns == (
const char *)URI) {
880 DBGN(cerr << endl <<
"\t Popping the 'inside_other_xml_attribute' state"
887 parser->dods_attr_type, parser->other_xml);
889 parser->other_xml =
"";
892 DBGN(cerr << endl <<
"\t inside_other_xml_attribute: " << localname
893 <<
", depth: " << parser->other_xml_depth << endl);
894 if (parser->other_xml_depth == 0)
896 "Expected an OtherXML attribute to end! Instead I found '%s'",
898 parser->other_xml_depth--;
900 parser->other_xml.append(
"</");
902 parser->other_xml.append((
const char *)prefix);
903 parser->other_xml.append(
":");
905 parser->other_xml.append(localname);
906 parser->other_xml.append(
">");
915 case inside_simple_type: {
919 BaseType *btp = parser->bt_stack.top();
920 parser->bt_stack.pop();
921 parser->at_stack.pop();
923 BaseType *parent = parser->bt_stack.top();
931 "Tried to add the simple-type variable '%s' to a non-constructor type (%s %s).",
933 parser->bt_stack.top()->
935 parser->bt_stack.top()->name().
942 "Expected an end tag for a simple type; found '%s' instead.",
949 parser->finish_variable(localname,
dods_array_c,
"Array");
952 case inside_dimension:
953 if (strcmp(localname,
"dimension") == 0)
957 "Expected an end dimension tag; found '%s' instead.",
961 case inside_structure:
965 case inside_sequence:
970 parser->finish_variable(localname,
dods_grid_c,
"Grid");
974 parser->finish_variable(localname,
dods_array_c,
"Map");
977 case inside_blob_href:
978 if (strcmp(localname,
"blob") == 0 || strcmp(localname,
"dataBLOB") == 0)
982 "Expected an end dataBLOB/blob tag; found '%s' instead.",
995 DBGN(cerr <<
" ... " << states[parser->get_state()] << endl);
1005 switch (parser->get_state()) {
1006 case inside_attribute_value:
1007 parser->char_data.append((
const char *)(ch), len);
1008 DBG2(cerr <<
"Characters: '" << parser->char_data <<
"'" << endl);
1011 case inside_other_xml_attribute:
1012 parser->other_xml.append((
const char *)(ch), len);
1013 DBG2(cerr <<
"Other XML Characters: '" << parser->other_xml <<
"'" << endl);
1030 switch (parser->get_state()) {
1031 case inside_other_xml_attribute:
1032 parser->other_xml.append((
const char *)(ch), len);
1049 switch (parser->get_state()) {
1050 case inside_other_xml_attribute:
1051 parser->other_xml.append((
const char *)(value), len);
1054 case parser_unknown:
1059 "Found a CData block but none are allowed by DAP.");
1071 return xmlGetPredefinedEntity(name);
1086 parser->set_state(parser_error);
1088 va_start(args, msg);
1090 vsnprintf(str, 1024, msg, args);
1093 int line = xmlSAX2GetLineNumber(parser->ctxt);
1096 parser->error_msg += string(str) + string(
"\n");
1101 void DDXParser::cleanup_parse(xmlParserCtxtPtr & context)
1103 bool wellFormed = context->wellFormed;
1104 bool valid = context->valid;
1106 context->sax = NULL;
1107 xmlFreeParserCtxt(context);
1111 while (!bt_stack.empty()) {
1112 delete bt_stack.top();
1117 throw DDXParseFailed(
string(
"\nThe DDX is not a well formed XML document.\n") + error_msg);
1121 throw DDXParseFailed(
string(
"\nThe DDX is not a valid document.\n") + error_msg);
1124 if (get_state() == parser_error) {
1125 throw DDXParseFailed(
string(
"\nError parsing DDX response.\n") + error_msg);
1139 if (!in || in.eof())
1140 throw InternalErr(__FILE__, __LINE__,
"Input stream not open or read error");
1142 const int size = 1024;
1143 char chars[size + 1];
1146 in.readsome(chars, 4);
1147 int res = in.gcount();
1150 xmlParserCtxtPtr context = xmlCreatePushParserCtxt(NULL, NULL, chars, res,
"stream");
1156 xmlSAXHandler ddx_sax_parser;
1157 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1168 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1172 context->sax = &ddx_sax_parser;
1173 context->userData =
this;
1174 context->validate =
true;
1176 in.getline(chars, size);
1178 chars[res-1] =
'\n';
1180 while (res > 0 && !
is_boundary(chars, boundary)) {
1181 DBG(cerr <<
"line (" << res <<
"): " << chars << endl);
1182 xmlParseChunk(ctxt, chars, res, 0);
1184 in.getline(chars, size);
1187 chars[res-1] =
'\n';
1194 xmlParseChunk(ctxt, chars, 0, 1);
1196 cleanup_parse(context);
1205 if (!in || feof(in) || ferror(in))
1207 "Input stream not open or read error");
1209 const int size = 1024;
1212 int res = fread(chars, 1, 4, in);
1215 xmlParserCtxtPtr context =
1216 xmlCreatePushParserCtxt(NULL, NULL, chars, res,
"stream");
1222 xmlSAXHandler ddx_sax_parser;
1223 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1234 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1238 context->sax = &ddx_sax_parser;
1239 context->userData =
this;
1240 context->validate =
true;
1243 while ((fgets(chars, size, in) > 0) && !
is_boundary(chars, boundary)) {
1245 DBG(cerr <<
"line (" << strlen(chars) <<
"): " << chars << endl);
1247 xmlParseChunk(ctxt, chars, strlen(chars), 0);
1251 xmlParseChunk(ctxt, chars, 0, 1);
1253 cleanup_parse(context);
1278 xmlParserCtxtPtr context = xmlCreateFileParserCtxt(document.c_str());
1282 (
"Could not initialize the parser with the file: '")
1283 + document +
string(
"'."));
1289 xmlSAXHandler ddx_sax_parser;
1290 memset( &ddx_sax_parser, 0,
sizeof(xmlSAXHandler) );
1301 ddx_sax_parser.initialized = XML_SAX2_MAGIC;
1305 context->sax = &ddx_sax_parser;
1306 context->userData =
this;
1307 context->validate =
false;
1309 xmlParseDocument(context);
1311 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_constructor_type() const
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 bool is_vector_type() const
Returns true if the instance is a vector (i.e., array) type variable.
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)
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 ...