libdap  Updated for version 3.17.0
D4Attributes.h
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2013 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 #ifndef _d4attributes_h
00027 #define _d4attributes_h 1
00028 
00029 #include <string>
00030 #include <vector>
00031 
00032 #include "D4AttributeType.h"
00033 #include "XMLWriter.h"
00034 
00035 using namespace std;
00036 
00037 namespace libdap
00038 {
00039 
00040 class AttrTable;
00041 class D4Attributes;
00042 
00043 class D4Attribute {
00044     string d_name;
00045     D4AttributeType d_type;    // Attributes are limited to the simple types
00046 
00047     // If d_type is attr_container_c is true, use d_attributes to read
00048     // the contained attributes, otherwise use d_values to read the vector
00049     // of values.
00050     D4Attributes *d_attributes;
00051 
00052     // IF d_type is attr_otherxml_c, the first string in d_values holds the
00053     // XML, otherwise, the strings hold attributes of type d_type.
00054     vector<string> d_values;
00055 
00056     // perform a deep copy
00057     void m_duplicate(const D4Attribute &src);
00058 
00059 public:
00060     typedef vector<string>::iterator D4AttributeIter;
00061     typedef vector<string>::const_iterator D4AttributeCIter;
00062 
00063     D4Attribute() : d_name(""), d_type(attr_null_c), d_attributes(0) {}
00064     D4Attribute(const string &name, D4AttributeType type)
00065         : d_name(name), d_type(type), d_attributes(0) {}
00066 
00067     D4Attribute(const D4Attribute &src);
00068     ~D4Attribute();
00069     D4Attribute &operator=(const D4Attribute &rhs);
00070 
00071     string name() const { return d_name; }
00072     void set_name(const string &name) { d_name = name; }
00073 
00074     D4AttributeType type() const { return d_type; }
00075     void set_type(D4AttributeType type) { d_type = type; }
00076 
00077     void add_value(const string &value) { d_values.push_back(value); }
00078     void add_value_vector(const vector<string> &values) { d_values = values; }
00079 
00080     D4AttributeIter value_begin() { return d_values.begin(); }
00081     D4AttributeIter value_end() { return d_values.end(); }
00082 
00083     unsigned int num_values() const { return d_values.size(); }
00084     string value(unsigned int i) const { return d_values[i]; }
00085 
00086     D4Attributes *attributes();
00087 
00088     void print_dap4(XMLWriter &xml) const;
00089 };
00090 
00091 class D4Attributes {
00092 public:
00093     typedef vector<D4Attribute*>::iterator D4AttributesIter;
00094     typedef vector<D4Attribute*>::const_iterator D4AttributesCIter;
00095 
00096 private:
00097     vector<D4Attribute*> d_attrs;
00098 
00099     void m_duplicate(const D4Attributes &src) {
00100         D4AttributesCIter i = src.d_attrs.begin();
00101         while (i != src.d_attrs.end()) {
00102             d_attrs.push_back(new D4Attribute(**i++));    // deep copy
00103         }
00104     }
00105 
00106     D4Attribute *find_depth_first(const string &name, D4AttributesIter i);
00107 
00108 public:
00109 
00110     D4Attributes() {}
00111     D4Attributes(const D4Attributes &rhs) {
00112         m_duplicate(rhs);
00113     }
00114 
00115     virtual ~D4Attributes() {
00116         D4AttributesIter i = d_attrs.begin();
00117         while(i != d_attrs.end()) {
00118             delete *i++;
00119         }
00120     }
00121 
00122     D4Attributes &operator=(const D4Attributes &rhs) {
00123         if (this == &rhs) return *this;
00124         m_duplicate(rhs);
00125         return *this;
00126     }
00127 
00128     void transform_to_dap4(AttrTable &at);
00129 
00130     bool empty() const { return d_attrs.empty(); }
00131 
00132     void add_attribute(D4Attribute *attr) {
00133         d_attrs.push_back(new D4Attribute(*attr));
00134     }
00135     void add_attribute_nocopy(D4Attribute *attr) {
00136         d_attrs.push_back(attr);
00137     }
00138 
00140     D4AttributesIter attribute_begin() { return d_attrs.begin(); }
00141 
00143     D4AttributesIter attribute_end() { return d_attrs.end(); }
00144 
00145     D4Attribute *find(const string &name);
00146     D4Attribute *get(const string &fqn);
00147 
00148     // D4Attribute *find_container(const string &name);
00149     // D4Attribute *get_container(const string &fqn);
00150 
00151     // Might add erase()
00152 
00153     void print_dap4(XMLWriter &xml) const;
00154 };
00155 
00156 string D4AttributeTypeToString(D4AttributeType at);
00157 D4AttributeType StringToD4AttributeType(string s);
00158 
00159 } // namespace libdap
00160 
00161 #endif // _d4attributes_h