1 // Copyright 2009, 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 // This file contains the declaration of the internal KmlUri class.  Do not use
27 // this class in application code.  Use the functions declared in kml_uri.h.
28 
29 
30 #ifndef KML_ENGINE_KML_URI_INTERNAL_H__
31 #define KML_ENGINE_KML_URI_INTERNAL_H__
32 
33 #include "boost/scoped_ptr.hpp"
34 #include "kml/base/util.h"
35 
36 // Forward declare to avoid including uri_parser.h in app code.
37 namespace kmlbase {
38 class UriParser;
39 }
40 
41 namespace kmlengine {
42 
43 // The main purpose of the KmlUri class is to hold the URI state for a given
44 // fetch.  This state is principally a base url and a relative target to fetch.
45 // Ideally any URI stands alone, however, the two-level fetch system used for
46 // relative KMZ references requires the target reference to be retained
47 // to be resolved against either the full URI of the base (typically that of
48 // the KmlFile in the KML Engine) or the URI of the KMZ archive containing
49 // the KML file, in that order.  For more details and examples see kml_uri.cc
50 // NOTE: This is an internal class.  Do not use in application code.
51 // Applications should use KmlFile (where the API provides the means to pass
52 // a base URI and target URI for relative fetches).
53 class KmlUri {
54  public:
55   // The base is a full absolute URI including scheme.  The base is typically
56   // the URI of a KML file as maintained in KmlFile::get_url().  For example,
57   // http://host.com/dir/path.kml, or http://host.com/dir/path.kmz/doc.kml.
58   // (Note that a "bare" KMZ reference here does _not_ automatically imply
59   // "the KML file" within the KMZ.  See the note above about this being and
60   // internal class).  The target is a relative or abstolue URI typically the
61   // raw content of any <href>, <styleUrl>, schemaUrl=, <targetHref>,
62   // <a href="...">, or <img href="..."> within the KmlFile.  However, there
63   // is no specific knowlege of any KML or HTML element within this class.
64   static KmlUri* CreateRelative(const string& base,
65                                 const string& target);
66 
67   ~KmlUri();
68 
is_kmz()69   bool is_kmz() const {
70     return is_kmz_;
71   }
72 
get_target()73   const string& get_target() const {
74     return target_;
75   }
76 
get_url()77   const string& get_url() const {
78     return url_;
79   }
80 
get_kmz_url()81   const string& get_kmz_url() const {
82     return kmz_url_;
83   }
84 
get_path_in_kmz()85   const string& get_path_in_kmz() const {
86     return path_in_kmz_;
87   }
88 
89   // TODO Ideally this class has no non-const methods.  No module should alter
90   // a KmlUri.  Instead a new one should be created as needed.
set_path_in_kmz(const string path_in_kmz)91   void set_path_in_kmz(const string path_in_kmz) {
92     path_in_kmz_ = path_in_kmz;
93     url_ = kmz_url_ + "/" + path_in_kmz;
94   }
95 
96  private:
97   // Private constructor.  Use static Create() method.
98   // TODO streamline this with the Create method.
99   KmlUri(const string& base, const string& target);
100 
101   bool is_kmz_;  // TODO should this be is_relative_kmz_?
102   const string base_;
103   const string target_;
104   // TODO use UriParser's throughout _or_ string, not both.
105   boost::scoped_ptr<kmlbase::UriParser> target_uri_;
106 
107   string url_;
108 
109   // TODO this is too complex.  Better might be to create a new KmlUri for
110   // a new fetch.
111   string kmz_url_;
112   string path_in_kmz_;
113 
114   // No copy construction or assignment please.
115   LIBKML_DISALLOW_EVIL_CONSTRUCTORS(KmlUri);
116 };
117 
118 }  // end namespace kmlengine
119 
120 #endif  // KML_ENGINE_KML_URI_INTERNAL_H__
121