38 #include <sys/types.h>
85 const string c_xml_xsi =
"http://www.w3.org/2001/XMLSchema-instance";
114 DDS::duplicate(
const DDS &dds)
116 DBG(cerr <<
"Entering DDS::duplicate... " <<endl);
122 string d_container_name;
128 string d_dap_version;
129 string d_request_xml_base;
134 vector<BaseType *> vars;
141 long d_max_response_size;
144 d_factory = dds.d_factory;
147 d_filename = dds.d_filename;
148 d_container_name = dds.d_container_name;
149 d_container = dds.d_container;
151 d_dap_major = dds.d_dap_major;
152 d_dap_minor = dds.d_dap_minor;
154 d_dap_version = dds.d_dap_version;
155 d_request_xml_base = dds.d_request_xml_base;
156 d_namespace = dds.d_namespace;
160 DDS &dds_tmp =
const_cast<DDS &
>(dds);
167 d_timeout = dds.d_timeout;
169 d_keywords = dds.d_keywords;
171 d_max_response_size = dds.d_max_response_size;
188 : d_factory(factory), d_name(name), d_container_name(
""), d_container(0),
189 d_request_xml_base(
""),
190 d_timeout(0), d_keywords(), d_max_response_size(0)
192 DBG(cerr <<
"Building a DDS for the default version (3.2)" << endl);
215 : d_factory(factory), d_name(name), d_container_name(
""), d_container(0),
216 d_request_xml_base(
""),
217 d_timeout(0), d_keywords(), d_max_response_size(0)
219 DBG(cerr <<
"Building a DDS for version: " << version << endl);
229 DBG(cerr <<
"Entering DDS(const DDS &rhs) ..." << endl);
231 DBG(cerr <<
" bye." << endl);
237 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
239 delete btp ; btp = 0;
246 DBG(cerr <<
"Entering DDS::operator= ..." << endl);
252 DBG(cerr <<
" bye." << endl);
277 "Error transferring attributes: working on a container in dds, but not das");
286 DBG(cerr <<
"Processing the attributes for: " << (*var)->d_name() <<
" a " << (*var)->type_name() << endl);
287 (*var)->transfer_attributes(top_level);
300 while (at_cont_p != top_level->
attr_end()) {
304 if ((*at_cont_p)->type ==
Attr_container && (*at_cont_p)->attributes->is_global_attribute()) {
305 DBG(cerr << (*at_cont_p)->d_name <<
" is a global attribute." << endl);
382 if (d_dap_minor >= 0) {
384 oss << d_dap_major <<
"." << d_dap_minor;
385 d_dap_version = oss.str();
397 if (d_dap_major >= 0) {
399 oss << d_dap_major <<
"." << d_dap_minor;
400 d_dap_version = oss.str();
412 istringstream iss(v);
414 int major = -1, minor = -1;
416 if (!iss.eof() && !iss.fail())
418 if (!iss.eof() && !iss.fail())
420 if (!iss.eof() && !iss.fail())
423 if (major == -1 || minor == -1 or dot !=
'.')
424 throw InternalErr(__FILE__, __LINE__,
"Could not parse dap version. Value given: " + v);
435 switch (d_dap_major) {
446 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP version.");
460 int major = floor(d);
461 int minor = (d-major)*10;
463 DBG(cerr <<
"Major: " << major <<
", Minor: " << minor << endl);
466 oss << major <<
"." << minor;
483 return d_container_name;
511 d_container_name = cn;
538 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
541 w += (*i)->width(constrained);
544 w += (*i)->width(constrained);
558 throw InternalErr(__FILE__, __LINE__,
"Trying to add a BaseType object with a NULL pointer.");
560 if (bt->is_dap4_only_type())
561 throw InternalErr(__FILE__, __LINE__,
"Attempt to add a DAP4 type to a DAP2 DDS.");
563 DBG2(cerr <<
"In DDS::add_var(), bt's address is: " << bt << endl);
566 DBG2(cerr <<
"In DDS::add_var(), btp's address is: " << btp << endl);
586 throw InternalErr(__FILE__, __LINE__,
"Trying to add a BaseType object with a NULL pointer.");
589 if (bt->is_dap4_only_type())
590 throw InternalErr(__FILE__, __LINE__,
"Attempt to add a DAP4 type to a DAP2 DDS.");
593 DBG2(cerr <<
"In DDS::add_var(), bt's address is: " << bt << endl);
619 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
620 if ((*i)->name() == n) {
636 if (i != vars.end()) {
652 for (
Vars_iter i_tmp = i1; i_tmp != i2; i_tmp++) {
695 return d_container->
var( name,
false, s ) ;
707 DBG(cerr <<
"DDS::leaf_match: Looking for " << n << endl);
709 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
711 DBG(cerr <<
"DDS::leaf_match: Looking for " << n <<
" in: " << btp->d_name() << endl);
713 if (btp->
name() == n) {
714 DBG(cerr <<
"Found " << n <<
" in: " << btp->d_name() << endl);
721 DBG(cerr <<
"Found " << n <<
" in: " << btp->d_name() << endl);
725 #if STRUCTURE_ARRAY_SYNTAX_OLD
730 DBG(cerr <<
"Found " << n <<
" in: " << btp->
var()->d_name() << endl);
743 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
745 DBG2(cerr <<
"Looking for " << d_name <<
" in: " << btp << endl);
747 if (btp->
name() == name) {
748 DBG2(cerr <<
"Found " << d_name <<
" in: " << btp << endl);
753 string::size_type dot_pos = name.find(
".");
754 if (dot_pos != string::npos) {
755 string aggregate = name.substr(0, dot_pos);
756 string field = name.substr(dot_pos + 1);
760 DBG2(cerr <<
"Descending into " << agg_ptr->
name() << endl);
761 return agg_ptr->
var(field,
true, s);
782 return vars.rbegin();
803 return vars.begin() + i;
812 return *(vars.begin() + i);
823 if (ptr->is_dap4_only_type())
824 throw InternalErr(__FILE__, __LINE__,
"Attempt to add a DAP4 type to a DAP2 DDS.");
840 if (ptr->is_dap4_only_type())
841 throw InternalErr(__FILE__, __LINE__,
"Attempt to add a DAP4 type to a DAP2 DDS.");
865 d_timeout = alarm(0);
887 for (
Vars_iter i = vars.begin(); i != vars.end(); i++) {
889 dynamic_cast<Sequence&>(**i).set_leaf_sequence();
891 dynamic_cast<Structure&>(**i).set_leaf_sequence();
899 FILE *in = fopen(fname.c_str(),
"r");
921 FILE *in = fdopen(_dup(fd),
"r");
923 FILE *in = fdopen(dup(fd),
"r");
927 throw InternalErr(__FILE__, __LINE__,
"Could not access file.");
950 throw InternalErr(__FILE__, __LINE__,
"Null input stream.");
962 DBG2(cout <<
"Status from parser: " << status << endl);
966 if (!status || !arg.
status()) {
978 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
985 out <<
"Dataset {\n" ;
987 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
988 (*i)->print_decl(out) ;
991 out <<
"} " <<
id2www(d_name) <<
";\n" ;
1008 out <<
"Attributes {\n" ;
1010 d_attr.
print(out,
" ");
1011 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
1012 (*i)->get_attr_table().print(out,
" ");
1033 fwrite(oss.str().data(),
sizeof(char), oss.str().length(), out);
1049 out <<
"Dataset {\n" ;
1051 for (
Vars_citer i = vars.begin(); i != vars.end(); i++) {
1055 (*i)->print_decl(out,
" ",
true,
false,
true) ;
1058 out <<
"} " <<
id2www(d_name) <<
";\n" ;
1079 fwrite(oss.str().data(), 1, oss.str().length(), out);
1099 class VariablePrintXMLWriter :
public unary_function<BaseType *, void>
1104 VariablePrintXMLWriter(
XMLWriter &xml,
bool constrained)
1105 : d_xml(xml), d_constrained(constrained)
1107 void operator()(BaseType *bt)
1109 bt->print_xml_writer(d_xml, d_constrained);
1137 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"Group") < 0)
1138 throw InternalErr(__FILE__, __LINE__,
"Could not write Group element");
1139 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)d_name.c_str()) < 0)
1140 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1142 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"dapVersion", (
const xmlChar*)
get_dap_version().c_str()) < 0)
1143 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for dapVersion");
1146 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xml", (
const xmlChar*)
c_xml_namespace.c_str()) < 0)
1147 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xml");
1150 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xml:base");
1153 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns", (
const xmlChar*)
get_namespace().c_str()) < 0)
1154 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1158 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"Dataset") < 0)
1159 throw InternalErr(__FILE__, __LINE__,
"Could not write Dataset element");
1160 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)d_name.c_str()) < 0)
1161 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1162 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xsi", (
const xmlChar*)
"http://www.w3.org/2001/XMLSchema-instance") < 0)
1163 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xsi");
1165 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation", (
const xmlChar*)
c_dap_32_n_sl.c_str()) < 0)
1166 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1168 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:grddl", (
const xmlChar*)
"http://www.w3.org/2003/g/data-view#") < 0)
1169 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:grddl");
1172 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:transformation");
1175 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1177 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:dap");
1179 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"dapVersion", (
const xmlChar*)
"3.2") < 0)
1180 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for dapVersion");
1183 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xml", (
const xmlChar*)
c_xml_namespace.c_str()) < 0)
1184 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xml");
1187 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xml:base");
1191 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"Dataset") < 0)
1192 throw InternalErr(__FILE__, __LINE__,
"Could not write Dataset element");
1193 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)d_name.c_str()) < 0)
1194 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for d_name");
1195 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xsi", (
const xmlChar*)
"http://www.w3.org/2001/XMLSchema-instance") < 0)
1196 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xsi");
1199 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1201 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation", (
const xmlChar*)
c_dap_20_n_sl.c_str()) < 0)
1202 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1209 for_each(
var_begin(),
var_end(), VariablePrintXMLWriter(xml, constrained));
1216 if (!blob.empty()) {
1217 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"blob") < 0)
1218 throw InternalErr(__FILE__, __LINE__,
"Could not write blob element");
1219 string cid =
"cid:" + blob;
1220 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*) cid.c_str()) < 0)
1221 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for d_name");
1222 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1223 throw InternalErr(__FILE__, __LINE__,
"Could not end blob element");
1227 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"blob") < 0)
1228 throw InternalErr(__FILE__, __LINE__,
"Could not write blob element");
1229 string cid =
"cid:" + blob;
1230 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*) cid.c_str()) < 0)
1231 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for d_name");
1232 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1233 throw InternalErr(__FILE__, __LINE__,
"Could not end blob element");
1236 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"dataBLOB") < 0)
1237 throw InternalErr(__FILE__, __LINE__,
"Could not write dataBLOB element");
1238 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*)
"") < 0)
1239 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for d_name");
1240 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1241 throw InternalErr(__FILE__, __LINE__,
"Could not end dataBLOB element");
1244 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1245 throw InternalErr(__FILE__, __LINE__,
"Could not end Dataset element");
1266 throw InternalErr(__FILE__, __LINE__,
"Tried to print a DMR with DAP major version less than 4");
1271 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"Group") < 0)
1272 throw InternalErr(__FILE__, __LINE__,
"Could not write Group element");
1274 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xml",
1276 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xml");
1278 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns:xsi", (
const xmlChar*)
c_xml_xsi.c_str())
1280 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:xsi");
1282 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xsi:schemaLocation",
1284 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns:schemaLocation");
1286 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xmlns",
1288 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xmlns");
1290 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"dapVersion",
1292 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for dapVersion");
1294 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"dmrVersion", (
const xmlChar*)
get_dmr_version().c_str()) < 0)
1295 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for dapVersion");
1298 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"xml:base",
1300 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for xml:base");
1303 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"name", (
const xmlChar*) d_name.c_str()) < 0)
1304 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
1310 for_each(
var_begin(),
var_end(), VariablePrintXMLWriter(xml, constrained));
1318 if (!blob.empty()) {
1319 if (xmlTextWriterStartElement(xml.
get_writer(), (
const xmlChar*)
"blob") < 0)
1320 throw InternalErr(__FILE__, __LINE__,
"Could not write blob element");
1321 string cid =
"cid:" + blob;
1322 if (xmlTextWriterWriteAttribute(xml.
get_writer(), (
const xmlChar*)
"href", (
const xmlChar*) cid.c_str()) < 0)
1323 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for d_name");
1324 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1325 throw InternalErr(__FILE__, __LINE__,
"Could not end blob element");
1330 if (xmlTextWriterEndElement(xml.
get_writer()) < 0)
1331 throw InternalErr(__FILE__, __LINE__,
"Could not end the top-level Group element");
1356 cerr <<
"A dataset must have a d_name" << endl;
1365 for (
Vars_iter i = vars.begin(); i != vars.end(); i++)
1366 if (!(*i)->check_semantics(msg,
true))
1403 DBG2(cerr <<
"DDS::mark: Looking for " << n << endl);
1407 DBG2(cerr <<
"Could not find variable " << n << endl);
1413 DBG2(cerr <<
"DDS::mark: Set variable " << variable->d_name()
1414 <<
" (a " << variable->
type_name() <<
")" << endl);
1420 while (!s->empty()) {
1421 s->top()->BaseType::set_send_p(state);
1423 DBG2(cerr <<
"DDS::mark: Set variable " << s->top()->d_name()
1424 <<
" (a " << s->top()->type_name() <<
")" << endl);
1427 string parent_name = (s->top()->get_parent()) ? s->top()->get_parent()->name():
"none";
1428 string parent_type = (s->top()->get_parent()) ? s->top()->get_parent()->type_name():
"none";
1429 DBG2(cerr <<
"DDS::mark: Parent variable " << parent_name <<
" (a " << parent_type <<
")" << endl);
1447 for (
Vars_iter i = vars.begin(); i != vars.end(); i++)
1448 (*i)->set_send_p(state);
1462 << (
void *)
this <<
")" << endl ;
1480 for (; i != ie; i++) {