37 {
"$Id: DDS.cc 25915 2012-10-24 00:14:58Z jimg $"
41 #include <sys/types.h>
114 DDS::duplicate(
const DDS &dds)
116 DBG(cerr <<
"Entering DDS::duplicate... " <<endl);
118 d_filename = dds.d_filename;
119 d_container_name = dds.d_container_name;
120 d_timeout = dds.d_timeout;
123 d_factory = dds.d_factory;
124 d_container = dds.d_container;
125 d_dap_major = dds.d_dap_major;
126 d_dap_minor = dds.d_dap_minor;
128 d_keywords = dds.d_keywords;
130 DDS &dds_tmp =
const_cast<DDS &
>(dds);
149 : d_factory(factory), name(n), d_container(0),
150 d_dap_major(2), d_dap_minor(0),
151 d_request_xml_base(
""), d_timeout(0), d_keywords(),
152 d_max_response_size(0)
154 DBG(cerr <<
"Building a DDS with client major/minor: "
155 << d_dap_major <<
"." << d_dap_minor << endl);
161 DBG(cerr <<
"Entering DDS(const DDS &rhs) ..." << endl);
163 DBG(cerr <<
" bye." << endl);
169 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
171 delete btp ; btp = 0;
178 DBG(cerr <<
"Entering DDS::operator= ..." << endl);
184 DBG(cerr <<
" bye." << endl);
209 "Error transferring attributes: working on a container in dds, but not das");
218 DBG(cerr <<
"Processing the attributes for: " << (*var)->name() <<
" a " << (*var)->type_name() << endl);
219 (*var)->transfer_attributes(top_level);
232 while (at_cont_p != top_level->
attr_end()) {
236 if ((*at_cont_p)->type ==
Attr_container && (*at_cont_p)->attributes->is_global_attribute()) {
237 DBG(cerr << (*at_cont_p)->name <<
" is a global attribute." << endl);
311 if (d_dap_minor >= 0) {
313 oss << d_dap_major <<
"." << d_dap_minor;
314 d_dap_version = oss.str();
323 if (d_dap_major >= 0) {
325 oss << d_dap_major <<
"." << d_dap_minor;
326 d_dap_version = oss.str();
339 istringstream iss(version_string);
341 int major = -1, minor = -1;
343 if (!iss.eof() && !iss.fail())
345 if (!iss.eof() && !iss.fail())
347 if (!iss.eof() && !iss.fail())
350 DBG(cerr <<
"Major: " << major <<
", dot: " << dot <<
", Minor: " << minor << endl);
352 if (major == -1 || minor == -1)
353 throw Error(
"Could not parse the client dap (XDAP-Accept header) value");
356 d_dap_version = version_string;
373 int minor = (d-major)*10;
375 DBG(cerr <<
"Major: " << major <<
", Minor: " << minor << endl);
378 oss << major <<
"." << minor;
379 d_dap_version = oss.str();
397 return d_container_name;
425 d_container_name = cn;
452 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
455 w += (*i)->width(constrained);
458 w += (*i)->width(constrained);
472 throw InternalErr(__FILE__, __LINE__,
"Trying to add a BaseType object with a NULL pointer.");
474 DBG2(cerr <<
"In DDS::add_var(), bt's address is: " << bt << endl);
477 DBG2(cerr <<
"In DDS::add_var(), btp's address is: " << btp << endl);
495 throw InternalErr(__FILE__, __LINE__,
"Trying to add a BaseType object with a NULL pointer.");
497 DBG2(cerr <<
"In DDS::add_var(), bt's address is: " << bt << endl);
523 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
524 if ((*i)->name() == n) {
540 if (i != vars.end()) {
556 for (
Vars_iter i_tmp = i1; i_tmp != i2; i_tmp++) {
599 return d_container->
var( name,
false, s ) ;
611 DBG(cerr <<
"DDS::leaf_match: Looking for " << n << endl);
613 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
615 DBG(cerr <<
"DDS::leaf_match: Looking for " << n <<
" in: " << btp->
name() << endl);
617 if (btp->
name() == n) {
618 DBG(cerr <<
"Found " << n <<
" in: " << btp->
name() << endl);
625 DBG(cerr <<
"Found " << n <<
" in: " << btp->
name() << endl);
629 #if STRUCTURE_ARRAY_SYNTAX_OLD
634 DBG(cerr <<
"Found " << n <<
" in: " << btp->
var()->
name() << endl);
647 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
649 DBG2(cerr <<
"Looking for " << name <<
" in: " << btp << endl);
651 if (btp->
name() == name) {
652 DBG2(cerr <<
"Found " << name <<
" in: " << btp << endl);
657 string::size_type dot_pos = name.find(
".");
658 if (dot_pos != string::npos) {
659 string aggregate = name.substr(0, dot_pos);
660 string field = name.substr(dot_pos + 1);
664 DBG2(cerr <<
"Descending into " << agg_ptr->
name() << endl);
665 return agg_ptr->
var(field,
true, s);
686 return vars.rbegin();
707 return vars.begin() + i;
716 return *(vars.begin() + i);
761 d_timeout = alarm(0);
783 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
785 dynamic_cast<Sequence&>(**i).set_leaf_sequence();
787 dynamic_cast<Structure&>(**i).set_leaf_sequence();
795 FILE *in = fopen(fname.c_str(),
"r");
817 FILE *in = fdopen(_dup(fd),
"r");
819 FILE *in = fdopen(dup(fd),
"r");
823 throw InternalErr(__FILE__, __LINE__,
"Could not access file.");
846 throw InternalErr(__FILE__, __LINE__,
"Null input stream.");
854 bool status =
ddsparse((
void *) & arg) == 0;
858 DBG2(cout <<
"Status from parser: " << status << endl);
862 if (!status || !arg.
status()) {
877 fwrite(oss.str().c_str(), oss.str().length(), 1, out);
879 fprintf(out,
"Dataset {\n") ;
881 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
882 (*i)->print_decl(out) ;
885 fprintf(out,
"} %s;\n",
id2www(name).c_str()) ;
896 out <<
"Dataset {\n" ;
898 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
899 (*i)->print_decl(out) ;
902 out <<
"} " <<
id2www(name) <<
";\n" ;
919 out <<
"Attributes {\n" ;
921 d_attr.
print(out,
" ");
922 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
923 (*i)->get_attr_table().print(out,
" ");
943 fprintf(out,
"Dataset {\n") ;
945 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
949 (*i)->print_decl(out,
" ",
true,
false,
true) ;
952 fprintf(out,
"} %s;\n",
id2www(name).c_str()) ;
971 out <<
"Dataset {\n" ;
973 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
977 (*i)->print_decl(out,
" ",
true,
false,
true) ;
980 out <<
"} " <<
id2www(name) <<
";\n" ;
986 class VariablePrintXML :
public unary_function<BaseType *, void>
991 VariablePrintXML(FILE *out,
bool constrained)
992 : d_out(out), d_constrained(constrained)
994 void operator()(BaseType *bt)
996 bt->print_xml(d_out,
" ", d_constrained);
1017 string doc = oss.str();
1018 fwrite(doc.data(), 1, doc.length(), out);
1021 fprintf(out,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1023 fprintf(out,
"<Dataset name=\"%s\"\n",
id2xml(name).c_str());
1025 fprintf(out,
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
1027 fprintf(out,
"method=\"FILE*\"\n");
1036 fprintf(out,
"xsi:schemaLocation=\"%s %s\">\n\n",
1041 fprintf(out,
"xsi:schemaLocation=\"%s %s\">\n\n",
1046 d_attr.
print_xml(out,
" ", constrained);
1057 fprintf(out,
" <dataBLOB href=\"\"/>\n");
1059 else if (!blob.empty()
1062 fprintf(out,
" <blob href=\"cid:%s\"/>\n", blob.c_str());
1066 fprintf(out,
"</Dataset>\n");
1071 class VariablePrintXMLStrm :
public unary_function<BaseType *, void>
1076 VariablePrintXMLStrm(ostream &out,
bool constrained)
1077 : d_out(out), d_constrained(constrained)
1079 void operator()(BaseType *bt)
1081 bt->print_xml(d_out,
" ", d_constrained);
1102 out <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ;
1104 out <<
"<Dataset name=\"" <<
id2xml(name) <<
"\"\n" ;
1106 out <<
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" ;
1114 out <<
"xmlns:grddl=\"http://www.w3.org/2003/g/data-view#\"\n";
1118 out <<
"xmlns:dap=\"" << c_dap32_namespace <<
"\"\n" ;
1134 out <<
"xsi:schemaLocation=\"" << c_dap20_namespace
1138 d_attr.
print_xml(out,
" ", constrained);
1151 out <<
" <dataBLOB href=\"\"/>\n" ;
1153 else if (!blob.empty()
1156 out <<
" <blob href=\"cid:" << blob <<
"\"/>\n";
1159 out <<
"</Dataset>\n" ;
1163 class VariablePrintXMLWriter :
public unary_function<BaseType *, void>
1168 VariablePrintXMLWriter(
XMLWriter &xml,
bool constrained)
1169 : d_xml(xml), d_constrained(constrained)
1171 void operator()(BaseType *bt)
1173 bt->print_xml_writer(d_xml, d_constrained);
1183 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"Dataset") < 0)
1184 throw InternalErr(__FILE__, __LINE__,
"Could not write Dataset element");
1185 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)name.c_str()) < 0)
1186 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1187 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xsi", (
const xmlChar*)
"http://www.w3.org/2001/XMLSchema-instance") < 0)
1188 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xsi");
1193 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation", (
const xmlChar*)
c_dap_32_n_sl.c_str()) < 0)
1194 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1196 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:grddl", (
const xmlChar*)
"http://www.w3.org/2003/g/data-view#") < 0)
1197 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:grddl");
1200 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:transformation");
1203 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1205 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:dap");
1207 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"dapVersion", (
const xmlChar*)
"3.2") < 0)
1208 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for dapVersion");
1211 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xml", (
const xmlChar*)
c_xml_namespace.c_str()) < 0)
1212 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xml");
1215 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xml:base");
1220 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1222 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation", (
const xmlChar*)
c_dap_20_n_sl.c_str()) < 0)
1223 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1230 for_each(
var_begin(),
var_end(), VariablePrintXMLWriter(xml, constrained));
1240 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"dataBLOB") < 0)
1241 throw InternalErr(__FILE__, __LINE__,
"Could not write dataBLOB element");
1242 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*)
"") < 0)
1243 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1244 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1245 throw InternalErr(__FILE__, __LINE__,
"Could not end dataBLOB element");
1248 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"blob") < 0)
1249 throw InternalErr(__FILE__, __LINE__,
"Could not write blob element");
1250 string cid=
"cid:" + blob;
1251 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*)cid.c_str()) < 0)
1252 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1253 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1254 throw InternalErr(__FILE__, __LINE__,
"Could not end blob element");
1257 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1258 throw InternalErr(__FILE__, __LINE__,
"Could not end Dataset element");
1283 cerr <<
"A dataset must have a name" << endl;
1292 for (
Vars_iter i = vars.begin(); i != vars.end(); i++)
1293 if (!(*i)->check_semantics(msg,
true))
1329 DBG2(cerr <<
"DDS::mark: Looking for " << n << endl);
1333 DBG2(cerr <<
"Could not find variable " << n << endl);
1339 DBG2(cerr <<
"DDS::mark: Set variable " << variable->
name()
1340 <<
" (a " << variable->
type_name() <<
")" << endl);
1346 while (!s->empty()) {
1347 s->top()->BaseType::set_send_p(state);
1349 DBG2(cerr <<
"DDS::mark: Set variable " << s->top()->name()
1350 <<
" (a " << s->top()->type_name() <<
")" << endl);
1353 string parent_name = (s->top()->get_parent()) ? s->top()->get_parent()->name():
"none";
1354 string parent_type = (s->top()->get_parent()) ? s->top()->get_parent()->type_name():
"none";
1355 DBG2(cerr <<
"DDS::mark: Parent variable " << parent_name <<
" (a " << parent_type <<
")" << endl);
1373 for (
Vars_iter i = vars.begin(); i != vars.end(); i++)
1374 (*i)->set_send_p(state);
1388 << (
void *)
this <<
")" << endl ;
1406 for (; i != ie; i++) {