libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
D4Attributes.h
1
2// -*- mode: c++; c-basic-offset:4 -*-
3
4// This file is part of libdap, A C++ implementation of the OPeNDAP Data
5// Access Protocol.
6
7// Copyright (c) 2013 OPeNDAP, Inc.
8// Author: James Gallagher <jgallagher@opendap.org>
9//
10// This library is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26#ifndef _d4attributes_h
27#define _d4attributes_h 1
28
29#include <string>
30#include <vector>
31
32#include "DapObj.h"
33#include "D4AttributeType.h"
34#include "XMLWriter.h"
35
36using namespace std;
37
38namespace libdap
39{
40
41class AttrTable;
42class D4Attributes;
43
44class D4Attribute : public DapObj {
45 string d_name;
46 D4AttributeType d_type; // Attributes are limited to the simple types
47 bool is_utf8_str = false;
48
49 // If d_type is attr_container_c is true, use d_attributes to read
50 // the contained attributes, otherwise use d_values to read the vector
51 // of values.
52 D4Attributes *d_attributes;
53
54 // IF d_type is attr_otherxml_c, the first string in d_values holds the
55 // XML, otherwise, the strings hold attributes of type d_type.
56 vector<string> d_values;
57
58 // perform a deep copy
59 void m_duplicate(const D4Attribute &src);
60
61public:
62 typedef vector<string>::iterator D4AttributeIter;
63 typedef vector<string>::const_iterator D4AttributeCIter;
64
65 D4Attribute() : d_name(""), d_type(attr_null_c), d_attributes(0) {}
66 D4Attribute(const string &name, D4AttributeType type)
67 : d_name(name), d_type(type), d_attributes(0) {}
68
69 D4Attribute(const D4Attribute &src);
71 D4Attribute &operator=(const D4Attribute &rhs);
72
73 string name() const { return d_name; }
74 void set_name(const string &name) { d_name = name; }
75
76 D4AttributeType type() const { return d_type; }
77 void set_type(D4AttributeType type) { d_type = type; }
78
79 bool get_utf8_str_flag() const { return is_utf8_str; }
80 void set_utf8_str_flag(bool utf8_str_flag) { is_utf8_str = utf8_str_flag; }
81
82 void add_value(const string &value) { d_values.push_back(value); }
83 void add_value_vector(const vector<string> &values) { d_values = values; }
84
85 D4AttributeIter value_begin() { return d_values.begin(); }
86 D4AttributeIter value_end() { return d_values.end(); }
87
88 unsigned int num_values() const { return d_values.size(); }
89 string value(unsigned int i) const { return d_values[i]; }
90
91 D4Attributes *attributes();
92
93 bool is_dap4_type(const std::string &path, std::vector<std::string> &inventory);
94
95 void print_dap4(XMLWriter &xml) const;
96
97 virtual void dump(ostream &strm) const;
98};
99
100class D4Attributes : public DapObj {
101public:
102 typedef vector<D4Attribute*>::iterator D4AttributesIter;
103 typedef vector<D4Attribute*>::const_iterator D4AttributesCIter;
104
105private:
106 vector<D4Attribute*> d_attrs;
107
108 void m_duplicate(const D4Attributes &src) {
109 D4AttributesCIter i = src.d_attrs.begin();
110 while (i != src.d_attrs.end()) {
111 d_attrs.push_back(new D4Attribute(**i++)); // deep copy
112 }
113 }
114
115 D4Attribute *find_depth_first(const string &name, D4AttributesIter i);
116
117public:
118
119 D4Attributes() {}
120 D4Attributes(const D4Attributes &rhs) {
121 m_duplicate(rhs);
122 }
123
124 virtual ~D4Attributes() {
125 D4AttributesIter i = d_attrs.begin();
126 while(i != d_attrs.end()) {
127 delete *i++;
128 }
129 }
130
131 D4Attributes &operator=(const D4Attributes &rhs) {
132 if (this == &rhs) return *this;
133 m_duplicate(rhs);
134 return *this;
135 }
136
138 void transform_attrs_to_dap2(AttrTable *d2_attr_table);
139
140 bool empty() const { return d_attrs.empty(); }
141
142 void add_attribute(D4Attribute *attr) {
143 d_attrs.push_back(new D4Attribute(*attr));
144 }
145
146 void add_attribute_nocopy(D4Attribute *attr) {
147 d_attrs.push_back(attr);
148 }
149
151 D4AttributesIter attribute_begin() { return d_attrs.begin(); }
152
154 D4AttributesIter attribute_end() { return d_attrs.end(); }
155
156 D4Attribute *find(const string &name);
157 D4Attribute *get(const string &fqn);
158 void erase(const string &fqn);
159 void erase_named_attribute(const string &name);
160
166 const vector<D4Attribute*> &attributes() const { return d_attrs; }
167
168 bool has_dap4_types(const std::string &path, std::vector<std::string> &inventory) const;
169
170 void print_dap4(XMLWriter &xml) const;
171
172 virtual void dump(ostream &strm) const;
173};
174
175string D4AttributeTypeToString(D4AttributeType at);
176D4AttributeType StringToD4AttributeType(string s);
177
178} // namespace libdap
179
180#endif // _d4attributes_h
Contains the attributes for a dataset.
Definition AttrTable.h:154
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...
libdap base object for common functionality of libdap objects
Definition DapObj.h:51
top level DAP object to house generic methods
string D4AttributeTypeToString(D4AttributeType at)