libdap  Updated for version 3.20.6
libdap4 is an implementation of OPeNDAP's DAP protocol.
AISMerge.cc
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 // Dan Holloway <dan@hollywood.gso.uri.edu>
10 // Reza Nekovei <reza@intcomm.net>
11 //
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 //
26 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
27 
28 #include "config.h"
29 
30 #include <cstdio>
31 #include <fstream>
32 
33 #include "AISMerge.h"
34 #include "AISExceptions.h"
35 #include "Response.h"
36 
37 namespace libdap {
38 
52 {
53  if (res.find("http:") == 0
54  || res.find("file:") == 0 || res.find("https:") == 0) {
55  return d_http.fetch_url(res);
56  }
57  else {
58 #if 0
59  ifstream s(res);
60 #endif
61  FILE *s = fopen(res.c_str(), "r");
62  if (!s)
63  throw Error("I could not open local AIS resource '"
64  + res + "'.");
65  return new Response(s, 0);
66  }
67 }
68 
82 void AISMerge::merge(const string & primary, DAS & das)
83 {
84  if (!d_ais_db.has_resource(primary))
85  return;
86 
87  try {
88  ResourceVector rv = d_ais_db.get_resource(primary);
89 
90  for (ResourceVectorIter i = rv.begin(); i != rv.end(); ++i) {
91  Response *ais_resource = get_ais_resource(i->get_url());
92  switch (i->get_rule()) {
93  case Resource::overwrite:
94  das.parse(ais_resource->get_stream());
95  break;
96  case Resource::replace:
97  das.erase();
98  das.parse(ais_resource->get_stream());
99  break;
100  case Resource::fallback:
101  if (das.get_size() == 0)
102  das.parse(ais_resource->get_stream());
103  break;
104  }
105  delete ais_resource;
106  ais_resource = 0;
107  }
108  }
109  catch (NoSuchPrimaryResource & e) {
110  throw
111  InternalErr(string
112  ("I caught a 'NoSuchPrimaryResource' exception, it said:\n")
113  + e.get_error_message() + string("\n"));
114  }
115 }
116 
117 } // namespace libdap
virtual Response * get_ais_resource(const string &res)
Definition: AISMerge.cc:51
virtual bool has_resource(const string &primary) const
virtual ResourceVector get_resource(const string &primary)
HTTPResponse * fetch_url(const string &url)
Definition: HTTPConnect.cc:627
top level DAP object to house generic methods
Definition: AISConnect.cc:30
A class for software fault reporting.
Definition: InternalErr.h:64
virtual void merge(const string &primary, DAS &das)
Definition: AISMerge.cc:82
std::string get_error_message() const
Definition: Error.cc:278
virtual unsigned int get_size() const
Returns the number of attributes in the current attribute table.
Definition: DAS.cc:125
virtual void erase()
erase all attributes in this DAS
Definition: DAS.cc:135
virtual void parse(string fname)
Reads a DAS from the named file.
Definition: DAS.cc:232
Hold attribute data for a DAP2 dataset.
Definition: DAS.h:121
A class for error processing.
Definition: Error.h:92