29#include "D4Attributes.h"
30#include "D4AttributeType.h"
31#include "InternalErr.h"
99 case attr_container_c:
102 case attr_otherxml_c:
106 throw InternalErr(__FILE__, __LINE__,
"Unsupported attribute type");
110D4AttributeType StringToD4AttributeType(
string s)
114 if (s ==
"container")
115 return attr_container_c;
117 else if (s ==
"byte")
119 else if (s ==
"int8")
121 else if (s ==
"uint8")
123 else if (s ==
"int16")
125 else if (s ==
"uint16")
126 return attr_uint16_c;
127 else if (s ==
"int32")
129 else if (s ==
"uint32")
130 return attr_uint32_c;
131 else if (s ==
"int64")
133 else if (s ==
"uint64")
134 return attr_uint64_c;
136 else if (s ==
"float32")
137 return attr_float32_c;
138 else if (s ==
"float64")
139 return attr_float64_c;
141 else if (s ==
"string")
145 else if (s ==
"otherxml")
146 return attr_otherxml_c;
152D4Attribute::m_duplicate(
const D4Attribute &src)
156 d_values = src.d_values;
157 if (src.d_attributes)
158 d_attributes =
new D4Attributes(*src.d_attributes);
163D4Attribute::D4Attribute(
const D4Attribute &src)
168D4Attribute::~D4Attribute()
174D4Attribute::operator=(
const D4Attribute &rhs)
176 if (
this == &rhs)
return *
this;
182D4Attribute::attributes()
184 if (!d_attributes) d_attributes =
new D4Attributes();
200 for (AttrTable::Attr_iter i = at.attr_begin(), e = at.attr_end(); i != e; ++i) {
201 string name = at.get_name(i);
202 AttrType type = at.get_attr_type(i);
205 case Attr_container: {
208 attributes->transform_to_dap4(*at.get_attr_table(i));
209 add_attribute_nocopy(a);
214 a->add_value_vector(*at.get_attr_vector(i));
215 add_attribute_nocopy(a);
220 a->add_value_vector(*at.get_attr_vector(i));
221 add_attribute_nocopy(a);
226 a->add_value_vector(*at.get_attr_vector(i));
227 add_attribute_nocopy(a);
232 a->add_value_vector(*at.get_attr_vector(i));
233 add_attribute_nocopy(a);
238 a->add_value_vector(*at.get_attr_vector(i));
239 add_attribute_nocopy(a);
244 a->add_value_vector(*at.get_attr_vector(i));
245 add_attribute_nocopy(a);
250 a->add_value_vector(*at.get_attr_vector(i));
251 add_attribute_nocopy(a);
256 a->set_utf8_str_flag((*i)->is_utf8_str);
257 a->add_value_vector(*at.get_attr_vector(i));
258 add_attribute_nocopy(a);
263 a->add_value_vector(*at.get_attr_vector(i));
264 add_attribute_nocopy(a);
267 case Attr_other_xml: {
269 a->add_value_vector(*at.get_attr_vector(i));
270 add_attribute_nocopy(a);
274 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP2 attribute type in D4Attributes::copy_from_dap2()");
280AttrType get_dap2_AttrType(D4AttributeType d4_type) {
283 case attr_container_c: {
return Attr_container; }
284 case attr_byte_c: {
return Attr_byte; }
285 case attr_int16_c: {
return Attr_int16; }
286 case attr_uint16_c: {
return Attr_uint16; }
287 case attr_int32_c: {
return Attr_int32; }
288 case attr_uint32_c: {
return Attr_uint32; }
289 case attr_float32_c: {
return Attr_float32; }
290 case attr_float64_c: {
return Attr_float64; }
291 case attr_str_c: {
return Attr_string; }
292 case attr_url_c: {
return Attr_url; }
293 case attr_otherxml_c: {
return Attr_other_xml; }
294 case attr_uint8_c: {
return Attr_byte; }
297 case attr_int8_c: {
return Attr_int8; }
298 case attr_int64_c: {
return Attr_int64; }
299 case attr_uint64_c: {
return Attr_uint64; }
300 case attr_enum_c: {
return Attr_enum; }
301 case attr_opaque_c: {
return Attr_opaque; }
303 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP4 attribute.");
316 string name = (*i)->name();
317 D4AttributeType d4_attr_type = (*i)->type();
318 AttrType d2_attr_type = get_dap2_AttrType(d4_attr_type);
321 switch (d4_attr_type) {
322 case attr_container_c: {
324 child_attr_table->set_name(name);
326 (*i)->attributes()->transform_attrs_to_dap2(child_attr_table);
327 d2_attr_table->append_container(child_attr_table, name);
331 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
332 d2_attr_table->append_attr(name, d2_attr_type_name, *vi,(*i)->get_utf8_str_flag());
354 for (D4Attributes::D4AttributesIter i = d4_attrs->attribute_begin(), e = d4_attrs->attribute_end(); i != e; ++i) {
355 string name = (*i)->name();
356 D4AttributeType d4_attr_type = (*i)->type();
360 D4Attribute::D4AttributeIter vitr = (*i)->value_begin();
361 D4Attribute::D4AttributeIter end = (*i)->value_end();
363 vector<string> values;
364 for (; vitr != end; vitr++) {
365 values.push_back((*vitr));
369 switch (d4_attr_type) {
370 case attr_container_c: {
372 AttrTable *child_attr_table =
new AttrTable();
373 child_attr_table->set_name(name);
375 load_AttrTable(child_attr_table, (*i)->attributes());
376 d2_attr_table->append_container(child_attr_table, name);
380 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
381 d2_attr_table->append_attr(name, d2_attr_type_name, *vi);
398AttrTable *D4Attributes::get_AttrTable(
const string name)
400 AttrTable *at =
new AttrTable();
403 load_AttrTable(at,
this);
411D4Attributes::find_depth_first(
const string &name, D4AttributesIter i)
415 else if ((*i)->name() == name)
417 else if ((*i)->type() == attr_container_c)
418 return find_depth_first(name, (*i)->attributes()->attribute_begin());
420 return find_depth_first(name, ++i);
424D4Attributes::find(
const string &name)
438 size_t pos = fqn.find(
'.');
439 string part = fqn.substr(0, pos);
442 if (pos != string::npos)
443 rest = fqn.substr(pos + 1);
445 DBG(cerr <<
"part: '" << part <<
"'; rest: '" << rest <<
"'" << endl);
451 if ((*i)->name() == part && (*i)->type() == attr_container_c)
452 return (*i)->attributes()->get(rest);
459 if ((*i)->name() == part)
480 for (
auto &attr: d_attrs) {
481 if (attr->name() == name) {
486 d_attrs.erase(remove(d_attrs.begin(), d_attrs.end(),
nullptr), d_attrs.end());
499 size_t pos = fqn.find(
'.');
500 string part = fqn.substr(0, pos);
503 if (pos != string::npos)
504 rest = fqn.substr(pos + 1);
510 for (
auto &a: d_attrs) {
511 if (a->name() == part && a->type() == attr_container_c) {
512 a->attributes()->erase(rest);
523D4Attribute::print_dap4(
XMLWriter &xml)
const
525 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Attribute") < 0)
526 throw InternalErr(__FILE__, __LINE__,
"Could not write Attribute element");
527 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)name().c_str()) < 0)
528 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
529 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"type", (
const xmlChar*)
D4AttributeTypeToString(type()).c_str()) < 0)
530 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for type");
533 case attr_container_c:
536 d_attributes->print_dap4(xml);
539 case attr_otherxml_c:
540 if (num_values() != 1)
541 throw Error(
"OtherXML attributes cannot be vector-valued.");
542 if (xmlTextWriterWriteRaw(xml.get_writer(), (
const xmlChar*) value(0).c_str()) < 0)
543 throw InternalErr(__FILE__, __LINE__,
"Could not write OtherXML value");
553 D4AttributeCIter i = d_values.begin();
555 auto i = d_values.begin();
556 while (i != d_values.end()) {
557 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Value") < 0)
558 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
560 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) (*i++).c_str()) < 0)
562 string s = (get_utf8_str_flag())?(*i++):
escattr_xml(*i++);
563 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) s.c_str()) < 0)
564 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
566 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
567 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
575 D4AttributeCIter i = d_values.begin();
576 while (i != d_values.end()) {
577 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Value") < 0)
578 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
580 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) (*i++).c_str()) < 0)
581 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
583 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
584 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
591 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
592 throw InternalErr(__FILE__, __LINE__,
"Could not end Attribute element");
606 strm << DapIndent::LMarg <<
"D4Attribute::dump - (" << (
void *)
this <<
")" << endl;
608 DapIndent::Indent() ;
612 strm << DapIndent::LMarg << xml.get_doc() << flush;
614 DapIndent::UnIndent() ;
619D4Attributes::print_dap4(
XMLWriter &xml)
const
624 D4AttributesCIter i = d_attrs.begin();
625 while (i != d_attrs.end()) {
626 (*i++)->print_dap4(xml);
639 bool ima_d4_attr =
false;
646 case attr_container_c:
663 bool has_d4_attr =
false;
665 string attr_fqn = path +
"@" + attr->name();
666 bool isa_d4_attr = attr->is_dap4_type(attr_fqn, inventory);
670 has_d4_attr |= isa_d4_attr;
686 strm << DapIndent::LMarg <<
"D4Attributes::dump - (" << (
void *)
this <<
")" << endl;
688 DapIndent::Indent() ;
692 strm << DapIndent::LMarg << xml.get_doc() << flush;
694 DapIndent::UnIndent() ;
Contains the attributes for a dataset.
bool is_dap4_type(const std::string &path, std::vector< std::string > &inventory)
virtual void dump(ostream &strm) const
dumps information about this object
void erase(const string &fqn)
Erase the given attribute.
void transform_to_dap4(AttrTable &at)
copy attributes from DAP2 to DAP4
D4Attribute * get(const string &fqn)
const vector< D4Attribute * > & attributes() const
D4AttributesIter attribute_begin()
Get an iterator to the start of the enumerations.
D4AttributesIter attribute_end()
Get an iterator to the end of the enumerations.
void transform_attrs_to_dap2(AttrTable *d2_attr_table)
Copy the attributes from this D4Attributes object to a DAP2 AttrTable.
bool has_dap4_types(const std::string &path, std::vector< std::string > &inventory) const
virtual void dump(ostream &strm) const
dumps information about this object
void erase_named_attribute(const string &name)
Erase an attribute from a specific container This method expects to find 'name' in the D4Attributes o...
A class for error processing.
A class for software fault reporting.
top level DAP object to house generic methods
string escattr_xml(string s)
string AttrType_to_String(const AttrType at)
string D4AttributeTypeToString(D4AttributeType at)