37 {
"$Id: DDS.cc 25066 2011-11-30 18:39:37Z 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);
500 DBG2(cerr <<
"In DDS::add_var(), btp's address is: " << btp << endl);
530 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
531 if ((*i)->name() == n) {
547 if (i != vars.end()) {
563 for (
Vars_iter i_tmp = i1; i_tmp != i2; i_tmp++) {
606 return d_container->
var( name,
false, s ) ;
618 DBG(cerr <<
"DDS::leaf_match: Looking for " << n << endl);
620 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
622 DBG(cerr <<
"DDS::leaf_match: Looking for " << n <<
" in: " << btp->
name() << endl);
624 if (btp->
name() == n) {
625 DBG(cerr <<
"Found " << n <<
" in: " << btp->
name() << endl);
632 DBG(cerr <<
"Found " << n <<
" in: " << btp->
name() << endl);
636 #if STRUCTURE_ARRAY_SYNTAX_OLD
641 DBG(cerr <<
"Found " << n <<
" in: " << btp->
var()->
name() << endl);
654 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
656 DBG2(cerr <<
"Looking for " << name <<
" in: " << btp << endl);
658 if (btp->
name() == name) {
659 DBG2(cerr <<
"Found " << name <<
" in: " << btp << endl);
664 string::size_type dot_pos = name.find(
".");
665 if (dot_pos != string::npos) {
666 string aggregate = name.substr(0, dot_pos);
667 string field = name.substr(dot_pos + 1);
671 DBG2(cerr <<
"Descending into " << agg_ptr->
name() << endl);
672 return agg_ptr->
var(field,
true, s);
693 return vars.rbegin();
714 return vars.begin() + i;
723 return *(vars.begin() + i);
768 d_timeout = alarm(0);
790 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
792 dynamic_cast<Sequence&>(**i).set_leaf_sequence();
794 dynamic_cast<Structure&>(**i).set_leaf_sequence();
802 FILE *in = fopen(fname.c_str(),
"r");
824 FILE *in = fdopen(_dup(fd),
"r");
826 FILE *in = fdopen(dup(fd),
"r");
830 throw InternalErr(__FILE__, __LINE__,
"Could not access file.");
853 throw InternalErr(__FILE__, __LINE__,
"Null input stream.");
861 bool status =
ddsparse((
void *) & arg) == 0;
865 DBG2(cout <<
"Status from parser: " << status << endl);
869 if (!status || !arg.
status()) {
884 fwrite(oss.str().c_str(), oss.str().length(), 1, out);
886 fprintf(out,
"Dataset {\n") ;
888 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
889 (*i)->print_decl(out) ;
892 fprintf(out,
"} %s;\n",
id2www(name).c_str()) ;
903 out <<
"Dataset {\n" ;
905 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
906 (*i)->print_decl(out) ;
909 out <<
"} " <<
id2www(name) <<
";\n" ;
928 fprintf(out,
"Dataset {\n") ;
930 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
934 (*i)->print_decl(out,
" ",
true,
false,
true) ;
937 fprintf(out,
"} %s;\n",
id2www(name).c_str()) ;
956 out <<
"Dataset {\n" ;
958 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
962 (*i)->print_decl(out,
" ",
true,
false,
true) ;
965 out <<
"} " <<
id2www(name) <<
";\n" ;
971 class VariablePrintXML :
public unary_function<BaseType *, void>
976 VariablePrintXML(FILE *out,
bool constrained)
977 : d_out(out), d_constrained(constrained)
979 void operator()(BaseType *bt)
981 bt->print_xml(d_out,
" ", d_constrained);
1002 string doc = oss.str();
1003 fwrite(doc.data(), 1, doc.length(), out);
1006 fprintf(out,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1008 fprintf(out,
"<Dataset name=\"%s\"\n",
id2xml(name).c_str());
1010 fprintf(out,
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
1012 fprintf(out,
"method=\"FILE*\"\n");
1021 fprintf(out,
"xsi:schemaLocation=\"%s %s\">\n\n",
1026 fprintf(out,
"xsi:schemaLocation=\"%s %s\">\n\n",
1031 d_attr.
print_xml(out,
" ", constrained);
1042 fprintf(out,
" <dataBLOB href=\"\"/>\n");
1044 else if (!blob.empty()
1047 fprintf(out,
" <blob href=\"cid:%s\"/>\n", blob.c_str());
1051 fprintf(out,
"</Dataset>\n");
1056 class VariablePrintXMLStrm :
public unary_function<BaseType *, void>
1061 VariablePrintXMLStrm(ostream &out,
bool constrained)
1062 : d_out(out), d_constrained(constrained)
1064 void operator()(BaseType *bt)
1066 bt->print_xml(d_out,
" ", d_constrained);
1087 out <<
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ;
1089 out <<
"<Dataset name=\"" <<
id2xml(name) <<
"\"\n" ;
1091 out <<
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" ;
1099 out <<
"xmlns:grddl=\"http://www.w3.org/2003/g/data-view#\"\n";
1103 out <<
"xmlns:dap=\"" << c_dap32_namespace <<
"\"\n" ;
1119 out <<
"xsi:schemaLocation=\"" << c_dap20_namespace
1123 d_attr.
print_xml(out,
" ", constrained);
1136 out <<
" <dataBLOB href=\"\"/>\n" ;
1138 else if (!blob.empty()
1141 out <<
" <blob href=\"cid:" << blob <<
"\"/>\n";
1144 out <<
"</Dataset>\n" ;
1148 class VariablePrintXMLWriter :
public unary_function<BaseType *, void>
1153 VariablePrintXMLWriter(
XMLWriter &xml,
bool constrained)
1154 : d_xml(xml), d_constrained(constrained)
1156 void operator()(BaseType *bt)
1158 bt->print_xml_writer(d_xml, d_constrained);
1168 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"Dataset") < 0)
1169 throw InternalErr(__FILE__, __LINE__,
"Could not write Dataset element");
1170 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)name.c_str()) < 0)
1171 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1172 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xsi", (
const xmlChar*)
"http://www.w3.org/2001/XMLSchema-instance") < 0)
1173 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xsi");
1178 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation", (
const xmlChar*)
c_dap_32_n_sl.c_str()) < 0)
1179 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1181 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:grddl", (
const xmlChar*)
"http://www.w3.org/2003/g/data-view#") < 0)
1182 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:grddl");
1185 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:transformation");
1188 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1190 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:dap");
1192 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"dapVersion", (
const xmlChar*)
"3.2") < 0)
1193 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for dapVersion");
1196 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xml", (
const xmlChar*)
c_xml_namespace.c_str()) < 0)
1197 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xml");
1200 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xml:base");
1205 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1207 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation", (
const xmlChar*)
c_dap_20_n_sl.c_str()) < 0)
1208 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1215 for_each(
var_begin(),
var_end(), VariablePrintXMLWriter(xml, constrained));
1222 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"dataBLOB") < 0)
1223 throw InternalErr(__FILE__, __LINE__,
"Could not write dataBLOB element");
1224 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*)
"") < 0)
1225 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1226 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1227 throw InternalErr(__FILE__, __LINE__,
"Could not end dataBLOB element");
1230 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"blob") < 0)
1231 throw InternalErr(__FILE__, __LINE__,
"Could not write blob element");
1232 string cid=
"cid:" + blob;
1233 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*)cid.c_str()) < 0)
1234 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1235 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1236 throw InternalErr(__FILE__, __LINE__,
"Could not end blob element");
1239 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1240 throw InternalErr(__FILE__, __LINE__,
"Could not end Dataset element");
1265 cerr <<
"A dataset must have a name" << endl;
1274 for (
Vars_iter i = vars.begin(); i != vars.end(); i++)
1275 if (!(*i)->check_semantics(msg,
true))
1311 DBG2(cerr <<
"DDS::mark: Looking for " << n << endl);
1315 DBG2(cerr <<
"Could not find variable " << n << endl);
1321 DBG2(cerr <<
"DDS::mark: Set variable " << variable->
name()
1322 <<
" (a " << variable->
type_name() <<
")" << endl);
1328 while (!s->empty()) {
1329 s->top()->BaseType::set_send_p(state);
1331 DBG2(cerr <<
"DDS::mark: Set variable " << s->top()->name()
1332 <<
" (a " << s->top()->type_name() <<
")" << endl);
1333 string parent_name = (s->top()->get_parent()) ? s->top()->get_parent()->name():
"none";
1334 string parent_type = (s->top()->get_parent()) ? s->top()->get_parent()->type_name():
"none";
1335 DBG2(cerr <<
"DDS::mark: Parent variable " << parent_name <<
" (a " << parent_type <<
")" << endl);
1353 for (
Vars_iter i = vars.begin(); i != vars.end(); i++)
1354 (*i)->set_send_p(state);
1368 << (
void *)
this <<
")" << endl ;
1377 DapIndent::Indent() ;
1383 DapIndent::Indent() ;
1386 for (; i != ie; i++) {