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;
151 D4Attribute::m_duplicate(
const D4Attribute &src)
155 d_values = src.d_values;
156 if (src.d_attributes)
157 d_attributes =
new D4Attributes(*src.d_attributes);
162 D4Attribute::D4Attribute(
const D4Attribute &src)
167 D4Attribute::~D4Attribute()
173 D4Attribute::operator=(
const D4Attribute &rhs)
175 if (
this == &rhs)
return *
this;
181 D4Attribute::attributes()
183 if (!d_attributes) d_attributes =
new D4Attributes();
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 throw InternalErr(__FILE__, __LINE__,
"Unknown DAP4 attribute");
309 string name = (*i)->name();
310 D4AttributeType d4_attr_type = (*i)->type();
311 AttrType d2_attr_type = get_dap2_AttrType(d4_attr_type);
314 D4Attribute::D4AttributeIter vitr = (*i)->value_begin();
315 D4Attribute::D4AttributeIter end = (*i)->value_end();
317 vector<string> values;
318 for (; vitr != end; vitr++) {
319 values.push_back((*vitr));
322 switch (d4_attr_type) {
323 case attr_container_c: {
333 d2_attr_table->
append_attr(name, d2_attr_type_name, &values);
352 return my_pretty_pony;
357 D4Attributes::find_depth_first(
const string &name, D4AttributesIter i)
361 else if ((*i)->name() == name)
363 else if ((*i)->type() == attr_container_c)
364 return find_depth_first(name, (*i)->attributes()->attribute_begin());
366 return find_depth_first(name, ++i);
370 D4Attributes::find(
const string &name)
384 size_t pos = fqn.find(
'.');
385 string part = fqn.substr(0, pos);
388 if (pos != string::npos)
389 rest = fqn.substr(pos + 1);
391 DBG(cerr <<
"part: '" << part <<
"'; rest: '" << rest <<
"'" << endl);
397 if ((*i)->name() == part && (*i)->type() == attr_container_c)
398 return (*i)->attributes()->get(rest);
405 if ((*i)->name() == part)
416 D4Attribute::print_dap4(
XMLWriter &xml)
const
418 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Attribute") < 0)
419 throw InternalErr(__FILE__, __LINE__,
"Could not write Attribute element");
420 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"name", (
const xmlChar*)name().c_str()) < 0)
421 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for name");
422 if (xmlTextWriterWriteAttribute(xml.get_writer(), (
const xmlChar*)
"type", (
const xmlChar*)
D4AttributeTypeToString(type()).c_str()) < 0)
423 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute for type");
426 case attr_container_c:
428 throw InternalErr(__FILE__, __LINE__,
"Null Attribute container");
429 d_attributes->print_dap4(xml);
432 case attr_otherxml_c:
433 if (num_values() != 1)
434 throw Error(
"OtherXML attributes cannot be vector-valued.");
435 if (xmlTextWriterWriteRaw(xml.get_writer(), (
const xmlChar*) value(0).c_str()) < 0)
436 throw InternalErr(__FILE__, __LINE__,
"Could not write OtherXML value");
441 D4AttributeCIter i = d_values.begin();
442 while (i != d_values.end()) {
443 if (xmlTextWriterStartElement(xml.get_writer(), (
const xmlChar*)
"Value") < 0)
444 throw InternalErr(__FILE__, __LINE__,
"Could not write value element");
446 if (xmlTextWriterWriteString(xml.get_writer(), (
const xmlChar*) (*i++).c_str()) < 0)
447 throw InternalErr(__FILE__, __LINE__,
"Could not write attribute value");
449 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
450 throw InternalErr(__FILE__, __LINE__,
"Could not end value element");
457 if (xmlTextWriterEndElement(xml.get_writer()) < 0)
458 throw InternalErr(__FILE__, __LINE__,
"Could not end Attribute element");
472 strm << DapIndent::LMarg <<
"D4Attribute::dump - (" << (
void *)
this <<
")" << endl;
474 DapIndent::Indent() ;
478 strm << DapIndent::LMarg << xml.get_doc() << flush;
480 DapIndent::UnIndent() ;
485 D4Attributes::print_dap4(
XMLWriter &xml)
const
490 D4AttributesCIter i = d_attrs.begin();
491 while (i != d_attrs.end()) {
492 (*i++)->print_dap4(xml);
507 strm << DapIndent::LMarg <<
"D4Attributes::dump - (" << (
void *)
this <<
")" << endl;
509 DapIndent::Indent() ;
513 strm << DapIndent::LMarg << xml.get_doc() << flush;
515 DapIndent::UnIndent() ;