libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
AISResources.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) 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 #ifndef ais_resources_h
27 #define ais_resources_h
28 
29 #include <string>
30 #include <iostream>
31 #include <vector>
32 #include <map>
33 
34 #include "GNURegex.h"
35 
36 #ifndef resource_h
37 #include "Resource.h"
38 #endif
39 
40 #ifndef ais_exceptions_h
41 #include "AISExceptions.h"
42 #endif
43 
44 using namespace std;
45 
46 namespace libdap
47 {
48 
49 typedef vector<Resource> ResourceVector;
50 typedef ResourceVector::iterator ResourceVectorIter;
51 typedef ResourceVector::const_iterator ResourceVectorCIter;
52 
71 {
72 private:
73  // The AIS database is broken into two parts. The entries where the primary
74  // resource is a URL are stored in a map<> while the primaries that are
75  // regular expressions are stored in a vector of pairs. The latter is
76  // searched using the MatchRegexp struct.
77  typedef map<string, ResourceVector> ResourceMap;
78  typedef ResourceMap::iterator ResourceMapIter;
79  typedef ResourceMap::const_iterator ResourceMapCIter;
80 
81  typedef pair<string, ResourceVector> RVPair;
82  typedef vector<RVPair> ResourceRegexps;
83  typedef ResourceRegexps::iterator ResourceRegexpsIter;
84  typedef ResourceRegexps::const_iterator ResourceRegexpsCIter;
85 
86  ResourceMap d_db; // This holds the URL resources
87  ResourceRegexps d_re; // This holds the regular expression res.
88 
89  // Scan RegExps looking for a particular regular expression.
90 struct FindRegexp : public binary_function<RVPair, string, bool>
91  {
92  string local_re;
93  FindRegexp(const string &re) : local_re(re)
94  {}
95  bool operator()(const RVPair &p)
96  {
97  return p.first == local_re;
98  }
99  };
100 
101  // Scan RegExps looking for one that matches a URL.
102  // *** Make this more efficient by storing the Regex objects in the
103  // vector. 03/11/03 jhrg
104 struct MatchRegexp : public binary_function<RVPair, string, bool>
105  {
106  string candidate;
107  MatchRegexp(const string &url) : candidate(url)
108  {}
109  bool operator()(const RVPair &p)
110  {
111  Regex r(p.first.c_str());
112  return r.match(candidate.c_str(), candidate.length()) != -1;
113  }
114  };
115 
116  friend class AISResourcesTest; // unit tests access to private stuff
117  friend ostream &operator<<(ostream &os, const AISResources &ais_res);
118 
119 public:
122  {}
123  AISResources(const string &database) throw(AISDatabaseReadFailed);
124 
125  virtual ~AISResources()
126  {}
127 
128  virtual void add_url_resource(const string &url,
129  const Resource &ancillary);
130  virtual void add_url_resource(const string &url, const ResourceVector &rv);
131 
132  virtual void add_regexp_resource(const string &regexp,
133  const Resource &ancillary);
134  virtual void add_regexp_resource(const string &regexp,
135  const ResourceVector &rv);
136 
137  virtual bool has_resource(const string &primary) const;
138 
139  virtual ResourceVector get_resource(const string &primary);
140 
141  virtual void read_database(const string &database);
142 
143  virtual void write_database(const string &filename);
144 };
145 
146 } // namespace libdap
147 
148 #endif // ais_resources_h
Manage AIS resources.
Definition: AISResources.h:70
STL namespace.
top level DAP object to house generic methods
Definition: AISConnect.cc:30
int match(const char *s, int len, int pos=0)
Does the pattern match.
Definition: GNURegex.cc:115
ostream & operator<<(ostream &os, const Resource &r)
Definition: AISResources.cc:46
Associate a rule with an ancillary resource.
Definition: Resource.h:50