00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "config.h"
00029
00030 #include <cstdio>
00031
00032 #include "AISMerge.h"
00033 #include "AISExceptions.h"
00034 #include "Response.h"
00035
00036 namespace libdap {
00037
00050 Response *AISMerge::get_ais_resource(const string & res)
00051 {
00052 if (res.find("http:") == 0
00053 || res.find("file:") == 0 || res.find("https:") == 0) {
00054 return d_http.fetch_url(res);
00055 }
00056 else {
00057 FILE *s = fopen(res.c_str(), "r");
00058 if (!s)
00059 throw Error("I could not open local AIS resource '"
00060 + res + "'.");
00061 return new Response(s, 0);
00062 }
00063 }
00064
00078 void AISMerge::merge(const string & primary, DAS & das)
00079 {
00080 if (!d_ais_db.has_resource(primary))
00081 return;
00082
00083 try {
00084 ResourceVector rv = d_ais_db.get_resource(primary);
00085
00086 for (ResourceVectorIter i = rv.begin(); i != rv.end(); ++i) {
00087 Response *ais_resource = get_ais_resource(i->get_url());
00088 switch (i->get_rule()) {
00089 case Resource::overwrite:
00090 das.parse(ais_resource->get_stream());
00091 break;
00092 case Resource::replace:
00093 das.erase();
00094 das.parse(ais_resource->get_stream());
00095 break;
00096 case Resource::fallback:
00097 if (das.get_size() == 0)
00098 das.parse(ais_resource->get_stream());
00099 break;
00100 }
00101 delete ais_resource;
00102 ais_resource = 0;
00103 }
00104 }
00105 catch (NoSuchPrimaryResource & e) {
00106 throw
00107 InternalErr(string
00108 ("I caught a 'NoSuchPrimaryResource' exception, it said:\n")
00109 + e.get_error_message() + string("\n"));
00110 }
00111 }
00112
00113 }