29#include "D4AttributeType.h"
30#include "D4Attributes.h"
31#include "InternalErr.h"
98 case attr_container_c:
101 case attr_otherxml_c:
105 throw InternalErr(__FILE__, __LINE__,
"Unsupported attribute type");
109D4AttributeType 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;
149void D4Attribute::m_duplicate(
const D4Attribute &src) {
152 d_values = src.d_values;
153 if (src.d_attributes)
154 d_attributes =
new D4Attributes(*src.d_attributes);
159D4Attribute::D4Attribute(
const D4Attribute &src) { m_duplicate(src); }
161D4Attribute::~D4Attribute() {
delete d_attributes; }
172 d_attributes =
new D4Attributes();
191 case Attr_container: {
195 add_attribute_nocopy(a);
201 add_attribute_nocopy(a);
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);
242 a->set_utf8_str_flag((*i)->is_utf8_str);
244 add_attribute_nocopy(a);
250 add_attribute_nocopy(a);
253 case Attr_other_xml: {
256 add_attribute_nocopy(a);
260 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP2 attribute type in D4Attributes::copy_from_dap2()");
265AttrType get_dap2_AttrType(D4AttributeType d4_type) {
268 case attr_container_c: {
269 return Attr_container;
277 case attr_uint16_c: {
283 case attr_uint32_c: {
286 case attr_float32_c: {
289 case attr_float64_c: {
298 case attr_otherxml_c: {
299 return Attr_other_xml;
312 case attr_uint64_c: {
318 case attr_opaque_c: {
322 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP4 attribute.");
334 string name = (*i)->name();
335 D4AttributeType d4_attr_type = (*i)->type();
336 AttrType d2_attr_type = get_dap2_AttrType(d4_attr_type);
339 switch (d4_attr_type) {
340 case attr_container_c: {
344 (*i)->attributes()->transform_attrs_to_dap2(child_attr_table);
349 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
350 d2_attr_table->
append_attr(name, d2_attr_type_name, *vi, (*i)->get_utf8_str_flag());
373 string name = (*i)->name();
374 D4AttributeType d4_attr_type = (*i)->type();
378 D4Attribute::D4AttributeIter vitr = (*i)->value_begin();
379 D4Attribute::D4AttributeIter end = (*i)->value_end();
381 vector<string> values;
382 for (; vitr != end; vitr++) {
383 values.push_back((*vitr));
387 switch (d4_attr_type) {
388 case attr_container_c: {
390 AttrTable *child_attr_table =
new AttrTable();
391 child_attr_table->set_name(name);
393 load_AttrTable(child_attr_table, (*i)->attributes());
398 for (D4Attribute::D4AttributeIter vi = (*i)->value_begin(), ve = (*i)->value_end(); vi != ve; vi++) {
399 d2_attr_table->
append_attr(name, d2_attr_type_name, *vi);
416AttrTable *D4Attributes::get_AttrTable(
const string name)
418 AttrTable *at =
new AttrTable();
421 load_AttrTable(at,
this);
428D4Attribute *D4Attributes::find_depth_first(
const string &name, D4AttributesIter i) {
431 else if ((*i)->name() == name)
433 else if ((*i)->type() == attr_container_c)
434 return find_depth_first(name, (*i)->attributes()->attribute_begin());
436 return find_depth_first(name, ++i);
448 size_t pos = fqn.find(
'.');
449 string part = fqn.substr(0, pos);
452 if (pos != string::npos)
453 rest = fqn.substr(pos + 1);
455 DBG(cerr <<
"part: '" << part <<
"'; rest: '" << rest <<
"'" << endl);
461 if ((*i)->name() == part && (*i)->type() == attr_container_c)
462 return (*i)->attributes()->get(rest);
468 if ((*i)->name() == part)
487 for (
auto &attr : d_attrs) {
488 if (attr->name() == name) {
493 d_attrs.erase(remove(d_attrs.begin(), d_attrs.end(),
nullptr), d_attrs.end());
504 size_t pos = fqn.find(
'.');
505 string part = fqn.substr(0, pos);
508 if (pos != string::npos)
509 rest = fqn.substr(pos + 1);
515 for (
auto &a : d_attrs) {
516 if (a->name() == part && a->type() == attr_container_c) {
517 a->attributes()->erase(rest);
526void D4Attribute::print_dap4(
XMLWriter &xml)
const {
527 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
"Attribute") < 0)
528 throw InternalErr(__FILE__, __LINE__,
"Could not write Attribute element");
529 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar *)
"name", (
const xmlChar *)name().c_str()) < 0)
530 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
531 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar *)
"type",
533 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for type");
536 case attr_container_c:
539 d_attributes->print_dap4(xml);
542 case attr_otherxml_c:
543 if (num_values() != 1)
544 throw Error(
"OtherXML attributes cannot be vector-valued.");
545 if (xmlTextWriterWriteRaw(xml.get_writer(), (
const xmlChar *)value(0).c_str()) < 0)
546 throw InternalErr(__FILE__, __LINE__,
"Could not write OtherXML value");
556 D4AttributeCIter i = d_values.begin();
558 auto i = d_values.begin();
559 while (i != d_values.end()) {
560 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
"Value") < 0)
561 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
563 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) (*i++).c_str()) < 0)
565 string s = (get_utf8_str_flag()) ? (*i++) :
escattr_xml(*i++);
566 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar *)s.c_str()) < 0)
567 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
569 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
570 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
577 D4AttributeCIter i = d_values.begin();
578 while (i != d_values.end()) {
579 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar *)
"Value") < 0)
580 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
582 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar *)(*i++).c_str()) < 0)
583 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
585 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
586 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
593 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
594 throw InternalErr(__FILE__, __LINE__,
"Could not end Attribute element");
606 strm << DapIndent::LMarg <<
"D4Attribute::dump - (" << (
void *)
this <<
")" << endl;
612 strm << DapIndent::LMarg << xml.get_doc() << flush;
614 DapIndent::UnIndent();
617void D4Attributes::print_dap4(
XMLWriter &xml)
const {
621 D4AttributesCIter i = d_attrs.begin();
622 while (i != d_attrs.end()) {
623 (*i++)->print_dap4(xml);
634 bool ima_d4_attr =
false;
641 case attr_container_c:
642 ima_d4_attr = attributes()->has_dap4_types(path, inventory);
657 bool has_d4_attr =
false;
659 string attr_fqn = path +
"@" + attr->name();
660 bool isa_d4_attr = attr->is_dap4_type(attr_fqn, inventory);
664 has_d4_attr |= isa_d4_attr;
678 strm << DapIndent::LMarg <<
"D4Attributes::dump - (" << (
void *)
this <<
")" << endl;
684 strm << DapIndent::LMarg << xml.get_doc() << flush;
686 DapIndent::UnIndent();
Contains the attributes for a dataset.
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
virtual void set_name(const string &n)
Set the name of this attribute table.
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
virtual Attr_iter attr_end()
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
virtual Attr_iter attr_begin()
virtual string get_name() const
Get the name of this attribute table.
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
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)