AISResources.h

Go to the documentation of this file.
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 typedef vector<Resource> ResourceVector;
00047 typedef ResourceVector::iterator ResourceVectorIter;
00048 typedef ResourceVector::const_iterator ResourceVectorCIter;
00049 
00067 class AISResources
00068 {
00069 private:
00070     // The AIS database is broken into two parts. The entries where the primary
00071     // resource is a URL are stored in a map<> while the primaries that are
00072     // regular expressions are stored in a vector of pairs. The latter is
00073     // searched using the MatchRegexp struct.
00074     typedef map<string, ResourceVector> ResourceMap;
00075     typedef ResourceMap::iterator ResourceMapIter;
00076     typedef ResourceMap::const_iterator ResourceMapCIter;
00077 
00078     typedef pair<string, ResourceVector> RVPair;
00079     typedef vector<RVPair> ResourceRegexps;
00080     typedef ResourceRegexps::iterator ResourceRegexpsIter;
00081     typedef ResourceRegexps::const_iterator ResourceRegexpsCIter;
00082 
00083     ResourceMap d_db;  // This holds the URL resources
00084     ResourceRegexps d_re; // This holds the regular expression res.
00085 
00086     // Scan RegExps looking for a particular regular expression.
00087 struct FindRegexp : public binary_function<RVPair, string, bool>
00088     {
00089         string local_re;
00090         FindRegexp(const string &re) : local_re(re)
00091         {}
00092         bool operator()(const RVPair &p)
00093         {
00094             return p.first == local_re;
00095         }
00096     };
00097 
00098     // Scan RegExps looking for one that matches a URL.
00099     // *** Make this more efficient by storing the Regex objects in the
00100     // vector. 03/11/03 jhrg
00101 struct MatchRegexp : public binary_function<RVPair, string, bool>
00102     {
00103         string candidate;
00104         MatchRegexp(const string &url) : candidate(url)
00105         {}
00106         bool operator()(const RVPair &p)
00107         {
00108             Regex r(p.first.c_str());
00109             return r.match(candidate.c_str(), candidate.length()) != -1;
00110         }
00111     };
00112 
00113     friend class AISResourcesTest; // unit tests access to private stuff
00114     friend ostream &operator<<(ostream &os, const AISResources &ais_res);
00115 
00116 public:
00118     AISResources()
00119     {}
00120     AISResources(const string &database) throw(AISDatabaseReadFailed);
00121 
00122     virtual ~AISResources()
00123     {}
00124 
00125     virtual void add_url_resource(const string &url,
00126                                   const Resource &ancillary);
00127     virtual void add_url_resource(const string &url, const ResourceVector &rv);
00128 
00129     virtual void add_regexp_resource(const string &regexp,
00130                                      const Resource &ancillary);
00131     virtual void add_regexp_resource(const string &regexp,
00132                                      const ResourceVector &rv);
00133 
00134     virtual bool has_resource(const string &primary) const;
00135 
00136     virtual ResourceVector get_resource(const string &primary);
00137 
00138     virtual void read_database(const string &database);
00139 
00140     virtual void write_database(const string &filename);
00141 };
00142 
00143 #endif // ais_resources_h

Generated on Wed Jun 27 12:56:38 2007 for libdap++ by  doxygen 1.4.7