AISMerge.cc

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 //         Dan Holloway <dan@hollywood.gso.uri.edu>
00010 //         Reza Nekovei <reza@intcomm.net>
00011 //
00012 // This library is free software; you can redistribute it and/or
00013 // modify it under the terms of the GNU Lesser General Public
00014 // License as published by the Free Software Foundation; either
00015 // version 2.1 of the License, or (at your option) any later version.
00016 //
00017 // This library is distributed in the hope that it will be useful,
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 //
00026 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00027 
00028 #include "config.h"
00029 
00030 #include <cstdio>
00031 #include <fstream>
00032 
00033 #include "AISMerge.h"
00034 #include "AISExceptions.h"
00035 #include "Response.h"
00036 
00037 namespace libdap {
00038 
00051 Response *AISMerge::get_ais_resource(const string & res)
00052 {
00053     if (res.find("http:") == 0
00054         || res.find("file:") == 0 || res.find("https:") == 0) {
00055         return d_http.fetch_url(res);
00056     }
00057     else {
00058 #if 0
00059         ifstream s(res);
00060 #endif
00061         FILE *s = fopen(res.c_str(), "r");
00062         if (!s)
00063             throw Error("I could not open local AIS resource '"
00064                         + res + "'.");
00065         return new Response(s, 0);
00066     }
00067 }
00068 
00082 void AISMerge::merge(const string & primary, DAS & das)
00083 {
00084     if (!d_ais_db.has_resource(primary))
00085         return;
00086 
00087     try {
00088         ResourceVector rv = d_ais_db.get_resource(primary);
00089 
00090         for (ResourceVectorIter i = rv.begin(); i != rv.end(); ++i) {
00091             Response *ais_resource = get_ais_resource(i->get_url());
00092             switch (i->get_rule()) {
00093             case Resource::overwrite:
00094                 das.parse(ais_resource->get_stream());
00095                 break;
00096             case Resource::replace:
00097                 das.erase();
00098                 das.parse(ais_resource->get_stream());
00099                 break;
00100             case Resource::fallback:
00101                 if (das.get_size() == 0)
00102                     das.parse(ais_resource->get_stream());
00103                 break;
00104             }
00105             delete ais_resource;
00106             ais_resource = 0;
00107         }
00108     }
00109     catch (NoSuchPrimaryResource & e) {
00110         throw
00111         InternalErr(string
00112                     ("I caught a 'NoSuchPrimaryResource' exception, it said:\n")
00113                     + e.get_error_message() + string("\n"));
00114     }
00115 }
00116 
00117 } // namespace libdap

Generated on Wed Apr 15 19:20:22 2009 for libdap++ by  doxygen 1.4.7