libdap Updated for version 3.20.11
libdap4 is an implementation of OPeNDAP's DAP protocol.
AttrTable.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) 2002,2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23//
24// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25
26// (c) COPYRIGHT URI/MIT 1994-1999
27// Please read the full copyright statement in the file COPYRIGHT_URI.
28//
29// Authors:
30// jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31
32// An AttrTable is a table of attributes (type-name-value tuples).
33
34#ifndef _attrtable_h
35#define _attrtable_h 1
36
37
38#include <string>
39#include <vector>
40
41#ifndef _error_h
42#include "Error.h"
43#endif
44
45using std::vector;
46using std::string;
47using std::vector;
48
49#ifndef A_DapObj_h
50#include "DapObj.h"
51#endif
52
53#ifndef XMLWRITER_H_
54#include "XMLWriter.h"
55#endif
56
57namespace libdap
58{
59
82 Attr_unknown,
83 Attr_container,
84 Attr_byte,
85 Attr_int16,
86 Attr_uint16,
87 Attr_int32,
88 Attr_uint32,
89 Attr_float32,
90 Attr_float64,
91 Attr_string,
92 Attr_url,
93 Attr_other_xml
94};
95
96string AttrType_to_String(const AttrType at);
97AttrType String_to_AttrType(const string &s);
98
142class AttrTable : public DapObj
143{
144 // entry needs to be made public to make up for issues with this class'
145 // design. It should probably be moved to it's own class. 05/22/03 jhrg
146public:
151 struct entry
152 {
153 string name;
154 AttrType type;
155
156 bool is_alias;
157 string aliased_to;
158
159 bool is_global; // use this to mark non-container attributes. see below.
160
161 // If type == Attr_container, use attributes to read the contained
162 // table, otherwise use attr to read the vector of values.
163 AttrTable *attributes;
164 std::vector<string> *attr; // a vector of values. jhrg 12/5/94
165
166 entry(): name(""), type(Attr_unknown), is_alias(false),
167 aliased_to(""), is_global(true), attributes(0), attr(0) {}
168
169 entry(const entry &rhs): name(rhs.name), type(rhs.type), is_alias(rhs.is_alias),
170 aliased_to(rhs.aliased_to), is_global(rhs.is_global),attributes(0), attr(0)
171 {
172 clone(rhs);
173 }
174
175 void delete_entry()
176 {
177 if (is_alias) // alias copies the pointers.
178 return;
179 if (type == Attr_container) {
180 delete attributes; attributes = 0;
181 }
182 else {
183 delete attr; attr = 0;
184 }
185 }
186
187 virtual ~entry()
188 {
189 delete_entry();
190 }
191
192 void clone(const entry &rhs)
193 {
194#if 0
195 name = rhs.name;
196 type = rhs.type;
197 is_alias = rhs.is_alias;
198 aliased_to = rhs.aliased_to;
199 is_global = rhs.is_global;
200#endif
201 switch (rhs.type) {
202 case Attr_unknown:
203 break;
204 case Attr_container: {
205 if (rhs.is_alias)
206 attributes = rhs.attributes;
207 else
208 attributes = new AttrTable(*rhs.attributes);
209 break;
210 }
211 default: {
212 if (rhs.is_alias)
213 attr = rhs.attr;
214 else
215 attr = new std::vector<string>(*rhs.attr);
216 break;
217 }
218 }
219 }
220
221 entry &operator=(const entry &rhs)
222 {
223 if (this != &rhs) {
224 delete_entry();
225 clone(rhs);
226 }
227 return *this;
228 }
229 };
230
231 typedef std::vector<entry *>::const_iterator Attr_citer ;
232 typedef std::vector<entry *>::iterator Attr_iter ;
233
234private:
235 string d_name;
236 AttrTable *d_parent;
237 std::vector<entry *> attr_map;
238
239 // Use this to mark container attributes. Look at the methods
240 // is_global_attribute() and set_is_...., esp. at the versions that take
241 // an iterator. This code is tricky because it has to track both whole
242 // containers that are global and individual attributes that are 'global'
243 // relative to a constructor. That is, there are some attributes that are
244 // bound to a container and not any of the container's children.
245 bool d_is_global_attribute;
246
247 void delete_attr_table();
248
249 friend class AttrTableTest;
250
251protected:
252 void clone(const AttrTable &at);
253
254 void simple_print(FILE *out, string pad, Attr_iter i,
255 bool dereference);
256 void simple_print(ostream &out, string pad, Attr_iter i,
257 bool dereference);
258
259public:
260 AttrTable();
261 AttrTable(const AttrTable &rhs);
262 virtual ~AttrTable();
263 AttrTable & operator=(const AttrTable &rhs);
264
265 virtual void erase();
266
267 virtual unsigned int get_size() const;
268 virtual string get_name() const;
269 virtual void set_name(const string &n);
270
274 virtual AttrTable *get_parent() const
275 {
276 return d_parent;
277 }
278
279 virtual bool is_global_attribute() const { return d_is_global_attribute; }
280 virtual void set_is_global_attribute(bool ga) { d_is_global_attribute = ga; }
281
282 virtual unsigned int append_attr(const string &name, const string &type,
283 const string &value);
284 virtual unsigned int append_attr(const string &name, const string &type,
285 vector<string> *values);
286
287 virtual AttrTable *append_container(const string &name);
288 virtual AttrTable *append_container(AttrTable *at, const string &name);
289
290 virtual void find(const string &target, AttrTable **at, Attr_iter *iter);
291 virtual AttrTable *find_container(const string &target);
292 virtual AttrTable *recurrsive_find(const string &target,
293 Attr_iter *location);
294
295 Attr_iter simple_find(const string &target);
296 AttrTable *simple_find_container(const string &target);
297
298
299 virtual AttrTable *get_attr_table(const string &name);
300 virtual string get_type(const string &name);
301 virtual AttrType get_attr_type(const string &name);
302 virtual unsigned int get_attr_num(const string &name);
303 virtual string get_attr(const string &name, unsigned int i = 0);
304 virtual vector<string> *get_attr_vector(const string &name);
305 virtual void del_attr(const string &name, int i = -1);
306
307 virtual Attr_iter attr_begin();
308 virtual Attr_iter attr_end();
309 virtual Attr_iter get_attr_iter(int i);
310 virtual string get_name(Attr_iter iter);
311 virtual bool is_container(Attr_iter iter);
312 virtual AttrTable *get_attr_table(Attr_iter iter);
313 virtual Attr_iter del_attr_table(Attr_iter iter);
314 virtual string get_type(Attr_iter iter);
315 virtual AttrType get_attr_type(Attr_iter iter);
316 virtual unsigned int get_attr_num(Attr_iter iter);
317 virtual string get_attr(Attr_iter iter, unsigned int i = 0);
318 virtual std::vector<string> *get_attr_vector(Attr_iter iter);
319 virtual bool is_global_attribute(Attr_iter iter);
320 virtual void set_is_global_attribute(Attr_iter iter, bool ga);
321
322 virtual void add_container_alias(const string &name, AttrTable *src);
323 virtual void add_value_alias(AttrTable *at, const string &name,
324 const string &source);
325 virtual bool attr_alias(const string &alias,
326 AttrTable *at,
327 const string &name);
328 virtual bool attr_alias(const string &alias, const string &name);
329
330 virtual void print(FILE *out, string pad = " ",
331 bool dereference = false);
332 virtual void print(ostream &out, string pad = " ",
333 bool dereference = false);
334
335 virtual void print_xml(FILE *out, string pad = " ",
336 bool constrained = false);
337 virtual void print_xml(ostream &out, string pad = " ",
338 bool constrained = false);
339
340 void print_xml_writer(XMLWriter &xml);
341
342 void print_dap4(XMLWriter &xml);
343
344 virtual void dump(ostream &strm) const ;
345};
346
347
348string remove_space_encoding(const string &s);
349string add_space_encoding(const string &s);
350
351} // namespace libdap
352
353#endif // _attrtable_h
Contains the attributes for a dataset.
Definition: AttrTable.h:143
virtual AttrTable * append_container(const string &name)
Add a container to the attribute table.
Definition: AttrTable.cc:410
void simple_print(FILE *out, string pad, Attr_iter i, bool dereference)
Definition: AttrTable.cc:1103
virtual unsigned int get_attr_num(const string &name)
Get the number of attributes in this container.
Definition: AttrTable.cc:634
virtual bool attr_alias(const string &alias, AttrTable *at, const string &name)
Adds an alias to the set of attributes.
Definition: AttrTable.cc:1015
virtual bool is_container(Attr_iter iter)
Definition: AttrTable.cc:746
virtual void find(const string &target, AttrTable **at, Attr_iter *iter)
Definition: AttrTable.cc:481
virtual void set_name(const string &n)
Set the name of this attribute table.
Definition: AttrTable.cc:245
virtual AttrTable * get_attr_table(const string &name)
Get an attribute container.
Definition: AttrTable.cc:607
void clone(const AttrTable &at)
Definition: AttrTable.cc:160
virtual Attr_iter attr_end()
Definition: AttrTable.cc:719
virtual void print_xml(FILE *out, string pad=" ", bool constrained=false)
Definition: AttrTable.cc:1313
virtual AttrTable * get_parent() const
Definition: AttrTable.h:274
virtual string get_type(const string &name)
Get the type name of an attribute within this attribute table.
Definition: AttrTable.cc:613
virtual vector< string > * get_attr_vector(const string &name)
Get a vector-valued attribute.
Definition: AttrTable.cc:653
virtual void add_value_alias(AttrTable *at, const string &name, const string &source)
Add an alias for an attribute.
Definition: AttrTable.cc:942
virtual unsigned int append_attr(const string &name, const string &type, const string &value)
Add an attribute to the table.
Definition: AttrTable.cc:307
virtual Attr_iter attr_begin()
Definition: AttrTable.cc:711
virtual Attr_iter get_attr_iter(int i)
Definition: AttrTable.cc:732
void print_dap4(XMLWriter &xml)
Definition: AttrTable.cc:1498
virtual void del_attr(const string &name, int i=-1)
Deletes an attribute.
Definition: AttrTable.cc:675
virtual string get_name() const
Get the name of this attribute table.
Definition: AttrTable.cc:238
virtual void erase()
Erase the attribute table.
Definition: AttrTable.cc:1036
void print_xml_writer(XMLWriter &xml)
Definition: AttrTable.cc:1425
virtual Attr_iter del_attr_table(Attr_iter iter)
Definition: AttrTable.cc:781
virtual void print(FILE *out, string pad=" ", bool dereference=false)
Prints the attribute table.
Definition: AttrTable.cc:1243
virtual void add_container_alias(const string &name, AttrTable *src)
Add an alias to a container held by this attribute table.
Definition: AttrTable.cc:908
virtual unsigned int get_size() const
Get the number of entries in this attribute table.
Definition: AttrTable.cc:231
virtual void dump(ostream &strm) const
dumps information about this object
Definition: AttrTable.cc:1510
virtual AttrTable * find_container(const string &target)
Find an attribute with a given name.
Definition: AttrTable.cc:566
Attr_iter simple_find(const string &target)
Definition: AttrTable.cc:541
virtual AttrType get_attr_type(const string &name)
Get the type of an attribute.
Definition: AttrTable.cc:621
virtual AttrTable * recurrsive_find(const string &target, Attr_iter *location)
Definition: AttrTable.cc:513
libdap base object for common functionality of libdap objects
Definition: DapObj.h:51
top level DAP object to house generic methods
Definition: AlarmHandler.h:36
string add_space_encoding(const string &s)
Definition: AttrTable.cc:78
string AttrType_to_String(const AttrType at)
Definition: AttrTable.cc:97
string remove_space_encoding(const string &s)
Definition: AttrTable.cc:61
AttrType
Definition: AttrTable.h:81