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++) {
std::vector< entry * >::iterator Attr_iter
const string c_dap32_namespace
void print(FILE *out)
Print the entire DDS to the specified file.
virtual void add_var_nocopy(BaseType *bt, Part part=nil)
const string c_dap_32_n_sl
virtual Attr_iter attr_end()
DDS(BaseTypeFactory *factory, const string &n="")
void insert_var_nocopy(Vars_iter i, BaseType *ptr)
const string c_dap20_namespace
Contains the attributes for a dataset.
void dds_delete_buffer(void *buffer)
virtual BaseType * var(const string &name, bool exact_match=true, btp_stack *s=0)
Returns a pointer to a member of a constructor class.
Vars_iter get_vars_iter(int i)
Get an iterator.
string get_error_message() const
const string c_default_dap32_schema_location
BaseType * leaf_match(const string &name, BaseType::btp_stack *s=0)
void insert_var(Vars_iter i, BaseType *ptr)
Insert a variable before the referenced element.
Vars_iter var_begin()
Return an iterator to the first variable.
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
string get_request_xml_base() const
Get the URL that will return this DDS/DDX/DataThing.
string id2xml(string in, const string ¬_allowed)
int get_request_size(bool constrained)
Get the estimated response size.
BaseType * var(const string &n, BaseType::btp_stack &s)
virtual void transfer_attributes(DAS *das)
void print_xml(FILE *out, bool constrained, const string &blob="")
void print_xml_writer(XMLWriter &xml)
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
std::vector< BaseType * >::const_iterator Vars_citer
void print_xml_writer(ostream &out, bool constrained, const string &blob="")
virtual void add_var(BaseType *bt, Part part=nil)
void add_var_nocopy(BaseType *bt)
Adds the variable to the DDS.
Holds a structure (aggregate) type.
Vars_riter var_rend()
Return a reverse iterator.
virtual string get_name() const
Get the name of this attribute table.
int num_var()
Returns the number of variables in the DDS.
std::vector< BaseType * >::reverse_iterator Vars_riter
stack< BaseType * > btp_stack
bool mark(const string &name, bool state)
Mark the send_p flag of the named variable to state.
A class for software fault reporting.
virtual bool is_vector_type()
Returns true if the instance is a vector (i.e., array) type variable.
void parse(string fname)
Parse a DDS from a file with the given name.
DDS & operator=(const DDS &rhs)
virtual bool is_constructor_type()
Returns true if the instance is a constructor (i.e., Structure, Sequence or Grid) type variable...
xmlTextWriterPtr get_writer()
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Returns a pointer to a member of a constructor class.
string type_name() const
Returns the type of the class instance as a string.
int get_dap_major() const
Get the DAP major version as sent by the client.
virtual void set_send_p(bool state)
void mark_all(bool state)
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
const string c_xml_namespace
std::vector< BaseType * >::iterator Vars_iter
void print_das(ostream &out)
virtual AttrTable & get_attr_table()
BaseType * get_var_index(int i)
Get a variable.
bool check_semantics(bool all=false)
Check the semantics of each of the variables represented in the DDS.
void * dds_buffer(FILE *fp)
void set_dap_version(const string &version_string)
virtual void del_var(const string &name)
void dds_switch_to_buffer(void *new_buffer)
string name() const
Returns the name of the class instance.
virtual AttrTable * get_top_level_attributes()
Returns the top most set of attributes.
virtual Attr_iter attr_begin()
virtual BaseType * ptr_duplicate()=0
string www2id(const string &in, const string &escape, const string &except)
Vars_iter var_end()
Return an iterator.
BaseType * exact_match(const string &name, BaseType::btp_stack *s=0)
void tag_nested_sequences()
Traverse DDS, set Sequence leaf nodes.
static ostream & LMarg(ostream &strm)
const string c_default_dap20_schema_location
void ddsrestart(FILE *yyin)
void set_dap_minor(int p)
Set the DAP minor version (typically using info from the client)
void set_dataset_name(const string &n)
The basic data type for the DODS DAP types.
libdap base object for common functionality of libdap objects
Pass parameters by reference to a parser.
void del_var(const string &n)
Removes a variable from the DDS.
bool unique_names(vector< BaseType * > l, const string &var_name, const string &type_name, string &msg)
virtual void dump(ostream &strm) const
dumps information about this object
Vars_riter var_rbegin()
Return a reverse iterator.
Hold attribute data for a DAP2 dataset.
const string grddl_transformation_dap32
int get_dap_minor() const
Get the DAP minor version as sent by the client.
A class for error processing.
string get_dataset_name() const
void duplicate(const DDS &dds)
void set_dap_major(int p)
Set the DAP major version (typically using info from the client)
void print_constrained(FILE *out)
Print a constrained DDS to the specified file.
virtual void dump(ostream &strm) const
dumps information about this object
virtual string container_name()
Returns the name of the current attribute container when multiple files used to build this DAS...
string id2www(string in, const string &allowable)
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 ...
const string c_dap_20_n_sl