1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef KML_ENGINE_KML_CACHE_H__
27 #define KML_ENGINE_KML_CACHE_H__
28 
29 #include "kml/base/net_cache.h"
30 #include "boost/scoped_ptr.hpp"
31 #include "kml/engine/kml_file.h"
32 #include "kml/engine/kmz_cache.h"
33 
34 namespace kmlengine {
35 
36 // A cache of KmlFile's (parse of a KML file of a given URL).
37 typedef kmlbase::NetCache<KmlFile> KmlFileNetCache;
38 
39 // This class is the main public API for networked KML.  Overall usage is as
40 // follows:
41 //   class YourNetFetcher : public kmlbase::NetFetcher {
42 //     // see kmlbase::NetCache
43 //   };
44 //   YourNetFetcher your_net_fetcher;
45 //   KmlCache kml_cache(&your_net_fetcher, cache_size);
46 //   KmlFilePtr k0 = kml_cache.FetchKmlAbsolute("http://host.com/file.kml");
47 //   KmlFilePtr k1 = kml_cache.FetchKmlAbsolute("http://host.com/file.kmz");
48 //   KmlFilePtr k2 =
49 //        kml_cache.FetchKmlAbsolute("http://host.com/file.kmz/foo.kml");
50 //   KmlFilePtr k3 =
51 //        kml_cache.FetchKmlRelative("http://host.com/file.kmz/doc.kml",
52 //                                   "link.kml");
53 //   string data;
54 //   bool status = kml_cache.FetchDataRelative("http://host.com/overlay.kml",
55 //                                             "image.jpg", &data);
56 //   bool status =
57 //       kml_cache.FetchDataRelative("http://host.com/file.kmz/doc.kml"
58 //                                   "image.jpg", &data);
59 // As the "cache" name suggests subsequent fetches for a given URL will
60 // potentially hit the cache.
61 class KmlCache {
62  public:
63   KmlCache(kmlbase::NetFetcher* net_fetcher, size_t max_size);
64 
65   // Any caller expecting to fetch and parse KML data should use this method.
66   // Use this with the raw content of a NetworkLink/Link/href, styleUrl, or
67   // schemaUrl.  A given parse of a local or remote StyleSelector or Schema
68   // referenced by a styleUrl/schemaUrl is thus cached.  The returned KmlFile
69   // is marked with a pointer back to this cache such that other internal
70   // KML Engine algorithms can fetch (and cache) shared styles and schemas.
71   // The base_url is typically that of the file containing the target_href.
72   KmlFilePtr FetchKmlRelative(const string& base_url,
73                               const string& target_href);
74 
75   // This method is used to fetch a remote KML or KMZ file with an absolute URL.
76   // If the fetch or parse fails NULL is returned.
77   KmlFilePtr FetchKmlAbsolute(const string& kml_url);
78 
79   // Any caller expecting to fetch data which _may_ be within a KMZ should use
80   // this method.  If the data is within a remote KMZ file that KMZ file is
81   // first fetched and cached such that subsequent access to this or other files
82   // within that KMZ file are out of the locally cached KMZ file.  Such content
83   // includes Model/Link/href (COLLADA geometry) and images for icons, overlays
84   // or model textures.  The target_href here typically is the content of an
85   // Overlay Icon's href, or Model's Link href.  The base_url is typically that
86   // of the file containing the target_href.
87   bool FetchDataRelative(const string& base_url,
88                          const string& target_href,
89                          string* content);
90 
91  private:
92   boost::scoped_ptr<KmzCache> kmz_file_cache_;
93   boost::scoped_ptr<KmlFileNetCache> kml_file_cache_;
94 };
95 
96 }  // end namespace kmlengine
97 
98 #endif  // KML_ENGINE_KML_CACHE_H__
99