29 #include "D4Attributes.h" 30 #include "D4AttributeType.h" 31 #include "InternalErr.h" 33 #include "AttrTable.h" 97 case attr_container_c:
100 case attr_otherxml_c:
104 throw InternalErr(__FILE__, __LINE__,
"Unsupported attribute type");
108 D4AttributeType StringToD4AttributeType(
string s)
112 if (s ==
"container")
113 return attr_container_c;
115 else if (s ==
"byte")
117 else if (s ==
"int8")
119 else if (s ==
"uint8")
121 else if (s ==
"int16")
123 else if (s ==
"uint16")
124 return attr_uint16_c;
125 else if (s ==
"int32")
127 else if (s ==
"uint32")
128 return attr_uint32_c;
129 else if (s ==
"int64")
131 else if (s ==
"uint64")
132 return attr_uint64_c;
134 else if (s ==
"float32")
135 return attr_float32_c;
136 else if (s ==
"float64")
137 return attr_float64_c;
139 else if (s ==
"string")
143 else if (s ==
"otherxml")
144 return attr_otherxml_c;
150 D4Attribute::m_duplicate(
const D4Attribute &src)
154 d_values = src.d_values;
155 if (src.d_attributes)
156 d_attributes =
new D4Attributes(*src.d_attributes);
161 D4Attribute::D4Attribute(
const D4Attribute &src)
166 D4Attribute::~D4Attribute()
172 D4Attribute::operator=(
const D4Attribute &rhs)
174 if (
this == &rhs)
return *
this;
180 D4Attribute::attributes()
182 if (!d_attributes) d_attributes =
new D4Attributes();
203 case Attr_container: {
207 add_attribute_nocopy(a);
213 add_attribute_nocopy(a);
219 add_attribute_nocopy(a);
225 add_attribute_nocopy(a);
231 add_attribute_nocopy(a);
237 add_attribute_nocopy(a);
243 add_attribute_nocopy(a);
249 add_attribute_nocopy(a);
255 add_attribute_nocopy(a);
261 add_attribute_nocopy(a);
264 case Attr_other_xml: {
267 add_attribute_nocopy(a);
271 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP2 attribute type in D4Attributes::copy_from_dap2()");
277 AttrType get_dap2_AttrType(D4AttributeType d4_type){
279 case attr_container_c: {
return Attr_container; }
280 case attr_byte_c: {
return Attr_byte; }
281 case attr_int16_c: {
return Attr_int16; }
282 case attr_uint16_c: {
return Attr_uint16; }
283 case attr_int32_c: {
return Attr_int32; }
284 case attr_uint32_c: {
return Attr_uint32; }
285 case attr_float32_c: {
return Attr_float32; }
286 case attr_float64_c: {
return Attr_float64; }
287 case attr_str_c: {
return Attr_string; }
288 case attr_url_c: {
return Attr_url; }
289 case attr_otherxml_c: {
return Attr_other_xml; }
291 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP4 attribute");
297 D4Attributes::load_AttrTable(AttrTable *d2_attr_table, D4Attributes *d4_attrs)
302 for ( D4Attributes::D4AttributesIter i = d4_attrs->attribute_begin(), e = d4_attrs->attribute_end(); i != e; ++i) {
303 string name = (*i)->name();
304 D4AttributeType d4_attr_type = (*i)->type();
305 AttrType d2_attr_type = get_dap2_AttrType(d4_attr_type);
308 D4Attribute::D4AttributeIter vitr =(*i)->value_begin();
309 D4Attribute::D4AttributeIter end =(*i)->value_end();
311 vector<string> values;
312 for(;vitr!=end; vitr++){
313 values.push_back((*vitr));
316 switch (d4_attr_type) {
317 case attr_container_c: {
319 AttrTable *child_attr_table =
new AttrTable();
320 child_attr_table->set_name(name);
322 load_AttrTable(child_attr_table,(*i)->attributes());
323 d2_attr_table->append_container(child_attr_table,name);
328 d2_attr_table->append_attr(name,d2_attr_type_name, &values);
346 load_AttrTable(my_pretty_pony,
this);
348 return my_pretty_pony;
353 D4Attributes::find_depth_first(
const string &name, D4AttributesIter i)
357 else if ((*i)->name() == name)
359 else if ((*i)->type() == attr_container_c)
360 return find_depth_first(name, (*i)->attributes()->attribute_begin());
362 return find_depth_first(name, ++i);
366 D4Attributes::find(
const string &name)
380 size_t pos = fqn.find(
'.');
381 string part = fqn.substr(0, pos);
384 if (pos != string::npos)
385 rest = fqn.substr(pos + 1);
387 DBG(cerr <<
"part: '" << part <<
"'; rest: '" << rest <<
"'" << endl);
393 if ((*i)->name() == part && (*i)->type() == attr_container_c)
394 return (*i)->attributes()->get(rest);
401 if ((*i)->name() == part)
412 D4Attribute::print_dap4(
XMLWriter &xml)
const 414 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Attribute") < 0)
415 throw InternalErr(__FILE__, __LINE__,
"Could not write Attribute element");
416 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)name().c_str()) < 0)
417 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
418 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"type", (
const xmlChar*)
D4AttributeTypeToString(type()).c_str()) < 0)
419 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for type");
422 case attr_container_c:
424 throw InternalErr(__FILE__, __LINE__,
"Null Attribute container");
425 d_attributes->print_dap4(xml);
428 case attr_otherxml_c:
429 if (num_values() != 1)
430 throw Error(
"OtherXML attributes cannot be vector-valued.");
431 if (xmlTextWriterWriteRaw(xml.get_writer(), (
const xmlChar*) value(0).c_str()) < 0)
432 throw InternalErr(__FILE__, __LINE__,
"Could not write OtherXML value");
437 D4AttributeCIter i = d_values.begin();
438 while (i != d_values.end()) {
439 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Value") < 0)
440 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
442 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) (*i++).c_str()) < 0)
443 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
445 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
446 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
453 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
454 throw InternalErr(__FILE__, __LINE__,
"Could not end Attribute element");
468 strm << DapIndent::LMarg <<
"D4Attribute::dump - (" << (
void *)
this <<
")" << endl;
470 DapIndent::Indent() ;
474 strm << DapIndent::LMarg << xml.get_doc() << flush;
476 DapIndent::UnIndent() ;
481 D4Attributes::print_dap4(
XMLWriter &xml)
const 486 D4AttributesCIter i = d_attrs.begin();
487 while (i != d_attrs.end()) {
488 (*i++)->print_dap4(xml);
503 strm << DapIndent::LMarg <<
"D4Attributes::dump - (" << (
void *)
this <<
")" << endl;
505 DapIndent::Indent() ;
509 strm << DapIndent::LMarg << xml.get_doc() << flush;
511 DapIndent::UnIndent() ;
AttrTable * get_AttrTable(const std::string name)
copy attributes from DAP4 to DAP2
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.
A class for software fault reporting.
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
virtual Attr_iter attr_begin()
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