29 #include "D4Attributes.h" 30 #include "D4AttributeType.h" 31 #include "InternalErr.h" 33 #include "AttrTable.h" 37 #include "DapIndent.h" 98 case attr_container_c:
101 case attr_otherxml_c:
105 throw InternalErr(__FILE__, __LINE__,
"Unsupported attribute type");
109 D4AttributeType StringToD4AttributeType(
string s)
113 if (s ==
"container")
114 return attr_container_c;
116 else if (s ==
"byte")
118 else if (s ==
"int8")
120 else if (s ==
"uint8")
122 else if (s ==
"int16")
124 else if (s ==
"uint16")
125 return attr_uint16_c;
126 else if (s ==
"int32")
128 else if (s ==
"uint32")
129 return attr_uint32_c;
130 else if (s ==
"int64")
132 else if (s ==
"uint64")
133 return attr_uint64_c;
135 else if (s ==
"float32")
136 return attr_float32_c;
137 else if (s ==
"float64")
138 return attr_float64_c;
140 else if (s ==
"string")
144 else if (s ==
"otherxml")
145 return attr_otherxml_c;
155 d_values = src.d_values;
156 if (src.d_attributes)
167 D4Attribute::~D4Attribute()
175 if (
this == &rhs)
return *
this;
181 D4Attribute::attributes()
204 case Attr_container: {
208 add_attribute_nocopy(a);
214 add_attribute_nocopy(a);
220 add_attribute_nocopy(a);
226 add_attribute_nocopy(a);
232 add_attribute_nocopy(a);
238 add_attribute_nocopy(a);
244 add_attribute_nocopy(a);
250 add_attribute_nocopy(a);
256 add_attribute_nocopy(a);
262 add_attribute_nocopy(a);
265 case Attr_other_xml: {
268 add_attribute_nocopy(a);
272 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP2 attribute type in D4Attributes::copy_from_dap2()");
278 AttrType get_dap2_AttrType(D4AttributeType d4_type) {
280 case attr_container_c: {
return Attr_container; }
281 case attr_byte_c: {
return Attr_byte; }
282 case attr_int16_c: {
return Attr_int16; }
283 case attr_uint16_c: {
return Attr_uint16; }
284 case attr_int32_c: {
return Attr_int32; }
285 case attr_uint32_c: {
return Attr_uint32; }
286 case attr_float32_c: {
return Attr_float32; }
287 case attr_float64_c: {
return Attr_float64; }
288 case attr_str_c: {
return Attr_string; }
289 case attr_url_c: {
return Attr_url; }
290 case attr_otherxml_c: {
return Attr_other_xml; }
292 case attr_int8_c: {
return Attr_byte; }
293 case attr_uint8_c: {
return Attr_byte; }
295 throw InternalErr(__FILE__, __LINE__,
"Unable to convert DAP4 attribute to DAP2. " 296 "There is no accepted DAP2 representation of Int64.");
298 case attr_uint64_c: {
299 throw InternalErr(__FILE__, __LINE__,
"Unable to convert DAP4 attribute to DAP2. " 300 "There is no accepted DAP2 representation of UInt64.");
303 throw InternalErr(__FILE__, __LINE__,
"Unable to convert DAP4 attribute to DAP2. " 304 "There is no accepted DAP2 representation of Enumeration.");
306 case attr_opaque_c: {
307 throw InternalErr(__FILE__, __LINE__,
"Unable to convert DAP4 attribute to DAP2. " 308 "There is no accepted DAP2 representation of Opaque.");
312 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP4 attribute.");
324 for (D4Attributes::D4AttributesIter i = attribute_begin(), e = attribute_end(); i != e; ++i) {
325 string name = (*i)->name();
326 D4AttributeType d4_attr_type = (*i)->type();
327 AttrType d2_attr_type = get_dap2_AttrType(d4_attr_type);
330 switch (d4_attr_type) {
331 case attr_container_c: {
335 (*i)->attributes()->transform_attrs_to_dap2(child_attr_table);
340 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
341 d2_attr_table->
append_attr(name, d2_attr_type_name, *vi);
364 string name = (*i)->name();
365 D4AttributeType d4_attr_type = (*i)->type();
369 D4Attribute::D4AttributeIter vitr = (*i)->value_begin();
370 D4Attribute::D4AttributeIter end = (*i)->value_end();
372 vector<string> values;
373 for (; vitr != end; vitr++) {
374 values.push_back((*vitr));
378 switch (d4_attr_type) {
379 case attr_container_c: {
384 load_AttrTable(child_attr_table, (*i)->attributes());
389 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
390 d2_attr_table->
append_attr(name, d2_attr_type_name, *vi);
407 AttrTable *D4Attributes::get_AttrTable(
const string name)
410 transform_attrs_to_dap2(at);
412 load_AttrTable(at,
this);
420 D4Attributes::find_depth_first(
const string &name, D4AttributesIter i)
422 if (i == attribute_end())
424 else if ((*i)->name() == name)
426 else if ((*i)->type() == attr_container_c)
427 return find_depth_first(name, (*i)->attributes()->attribute_begin());
429 return find_depth_first(name, ++i);
433 D4Attributes::find(
const string &name)
435 return find_depth_first(name, attribute_begin());
447 size_t pos = fqn.find(
'.');
448 string part = fqn.substr(0, pos);
451 if (pos != string::npos)
452 rest = fqn.substr(pos + 1);
454 DBG(cerr <<
"part: '" << part <<
"'; rest: '" << rest <<
"'" << endl);
458 D4AttributesIter i = attribute_begin();
459 while (i != attribute_end()) {
460 if ((*i)->name() == part && (*i)->type() == attr_container_c)
461 return (*i)->attributes()->get(rest);
466 D4AttributesIter i = attribute_begin();
467 while (i != attribute_end()) {
468 if ((*i)->name() == part)
479 D4Attribute::print_dap4(
XMLWriter &xml)
const 481 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Attribute") < 0)
482 throw InternalErr(__FILE__, __LINE__,
"Could not write Attribute element");
483 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)name().c_str()) < 0)
484 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
485 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"type", (
const xmlChar*)
D4AttributeTypeToString(type()).c_str()) < 0)
486 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for type");
489 case attr_container_c:
491 throw InternalErr(__FILE__, __LINE__,
"Null Attribute container");
492 d_attributes->print_dap4(xml);
495 case attr_otherxml_c:
496 if (num_values() != 1)
497 throw Error(
"OtherXML attributes cannot be vector-valued.");
498 if (xmlTextWriterWriteRaw(xml.get_writer(), (
const xmlChar*) value(0).c_str()) < 0)
499 throw InternalErr(__FILE__, __LINE__,
"Could not write OtherXML value");
504 D4AttributeCIter i = d_values.begin();
505 while (i != d_values.end()) {
506 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Value") < 0)
507 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
509 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) (*i++).c_str()) < 0)
510 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
512 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
513 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
520 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
521 throw InternalErr(__FILE__, __LINE__,
"Could not end Attribute element");
535 strm << DapIndent::LMarg <<
"D4Attribute::dump - (" << (
void *)
this <<
")" << endl;
537 DapIndent::Indent() ;
541 strm << DapIndent::LMarg << xml.get_doc() << flush;
543 DapIndent::UnIndent() ;
548 D4Attributes::print_dap4(
XMLWriter &xml)
const 553 D4AttributesCIter i = d_attrs.begin();
554 while (i != d_attrs.end()) {
555 (*i++)->print_dap4(xml);
570 strm << DapIndent::LMarg <<
"D4Attributes::dump - (" << (
void *)
this <<
")" << endl;
572 DapIndent::Indent() ;
576 strm << DapIndent::LMarg << xml.get_doc() << flush;
578 DapIndent::UnIndent() ;
virtual void dump(ostream &strm) const
dumps information about this object
virtual Attr_iter attr_end()
D4AttributesIter attribute_begin()
Get an iterator to the start of the enumerations.
Contains the attributes for a dataset.
string AttrType_to_String(const AttrType at)
virtual string get_name() const
Get the name of this attribute table.
top level DAP object to house generic methods
A class for software fault reporting.
void transform_attrs_to_dap2(AttrTable *d2_attr_table)
Copy the attributes from this D4Attributes object to a DAP2 AttrTable.
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
virtual Attr_iter attr_begin()
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
D4AttributesIter attribute_end()
Get an iterator to the end of the enumerations.
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
D4Attribute * get(const string &fqn)
A class for error processing.
string D4AttributeTypeToString(D4AttributeType at)
virtual void set_name(const string &n)
Set the name of this attribute table.
void transform_to_dap4(AttrTable &at)
copy attributes from DAP2 to DAP4
virtual void dump(ostream &strm) const
dumps information about this object