libdap++ Updated for version 3.8.2
|
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) 2003 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 ais_resources_h 00027 #define ais_resources_h 00028 00029 #include <string> 00030 #include <iostream> 00031 #include <vector> 00032 #include <map> 00033 00034 #include "GNURegex.h" 00035 00036 #ifndef resource_h 00037 #include "Resource.h" 00038 #endif 00039 00040 #ifndef ais_exceptions_h 00041 #include "AISExceptions.h" 00042 #endif 00043 00044 using namespace std; 00045 00046 namespace libdap 00047 { 00048 00049 typedef vector<Resource> ResourceVector; 00050 typedef ResourceVector::iterator ResourceVectorIter; 00051 typedef ResourceVector::const_iterator ResourceVectorCIter; 00052 00070 class AISResources 00071 { 00072 private: 00073 // The AIS database is broken into two parts. The entries where the primary 00074 // resource is a URL are stored in a map<> while the primaries that are 00075 // regular expressions are stored in a vector of pairs. The latter is 00076 // searched using the MatchRegexp struct. 00077 typedef map<string, ResourceVector> ResourceMap; 00078 typedef ResourceMap::iterator ResourceMapIter; 00079 typedef ResourceMap::const_iterator ResourceMapCIter; 00080 00081 typedef pair<string, ResourceVector> RVPair; 00082 typedef vector<RVPair> ResourceRegexps; 00083 typedef ResourceRegexps::iterator ResourceRegexpsIter; 00084 typedef ResourceRegexps::const_iterator ResourceRegexpsCIter; 00085 00086 ResourceMap d_db; // This holds the URL resources 00087 ResourceRegexps d_re; // This holds the regular expression res. 00088 00089 // Scan RegExps looking for a particular regular expression. 00090 struct FindRegexp : public binary_function<RVPair, string, bool> 00091 { 00092 string local_re; 00093 FindRegexp(const string &re) : local_re(re) 00094 {} 00095 bool operator()(const RVPair &p) 00096 { 00097 return p.first == local_re; 00098 } 00099 }; 00100 00101 // Scan RegExps looking for one that matches a URL. 00102 // *** Make this more efficient by storing the Regex objects in the 00103 // vector. 03/11/03 jhrg 00104 struct MatchRegexp : public binary_function<RVPair, string, bool> 00105 { 00106 string candidate; 00107 MatchRegexp(const string &url) : candidate(url) 00108 {} 00109 bool operator()(const RVPair &p) 00110 { 00111 Regex r(p.first.c_str()); 00112 return r.match(candidate.c_str(), candidate.length()) != -1; 00113 } 00114 }; 00115 00116 friend class AISResourcesTest; // unit tests access to private stuff 00117 friend ostream &operator<<(ostream &os, const AISResources &ais_res); 00118 00119 public: 00121 AISResources() 00122 {} 00123 AISResources(const string &database) throw(AISDatabaseReadFailed); 00124 00125 virtual ~AISResources() 00126 {} 00127 00128 virtual void add_url_resource(const string &url, 00129 const Resource &ancillary); 00130 virtual void add_url_resource(const string &url, const ResourceVector &rv); 00131 00132 virtual void add_regexp_resource(const string ®exp, 00133 const Resource &ancillary); 00134 virtual void add_regexp_resource(const string ®exp, 00135 const ResourceVector &rv); 00136 00137 virtual bool has_resource(const string &primary) const; 00138 00139 virtual ResourceVector get_resource(const string &primary); 00140 00141 virtual void read_database(const string &database); 00142 00143 virtual void write_database(const string &filename); 00144 }; 00145 00146 } // namespace libdap 00147 00148 #endif // ais_resources_h