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 // This file contains the implementation of the CreateResolvedStyle() function.
27 
28 #include "kml/engine/style_resolver.h"
29 #include "kml/dom.h"
30 #include "kml/engine/id_mapper.h"
31 #include "kml/engine/kml_file.h"
32 #include "kml/engine/style_merger.h"
33 
34 using kmldom::FeaturePtr;
35 using kmldom::KmlFactory;
36 using kmldom::PairPtr;
37 using kmldom::StylePtr;
38 using kmldom::StyleMapPtr;
39 using kmldom::StyleSelectorPtr;
40 
41 namespace kmlengine {
42 
43 // This is the implementation of the public API function to compute the
44 // resolved style for a given Feature within a KML file.  See the header
45 // for the full description of usage.
CreateResolvedStyle(const FeaturePtr & feature,const KmlFilePtr & kml_file,kmldom::StyleStateEnum style_state)46 StylePtr CreateResolvedStyle(const FeaturePtr& feature,
47                              const KmlFilePtr& kml_file,
48                              kmldom::StyleStateEnum style_state) {
49   return StyleResolver::CreateResolvedStyle(feature->get_styleurl(),
50                                             feature->get_styleselector(),
51                                             kml_file->get_shared_style_map(),
52                                             kml_file->get_url(),
53                                             kml_file->get_kml_cache(),
54                                             style_state);
55 }
56 
57 // static
CreateResolvedStyle(const string & styleurl,const StyleSelectorPtr & styleselector,const SharedStyleMap & shared_style_map,const string & base_url,KmlCache * kml_cache,kmldom::StyleStateEnum style_state)58 StylePtr StyleResolver::CreateResolvedStyle(
59     const string& styleurl,
60     const StyleSelectorPtr& styleselector,
61     const SharedStyleMap& shared_style_map,
62     const string& base_url,
63     KmlCache* kml_cache,
64     kmldom::StyleStateEnum style_state) {
65   StyleMerger style_merger(shared_style_map, kml_cache, base_url, style_state);
66   style_merger.MergeStyle(styleurl, styleselector);
67   // This always returns the thus-far resolved <Style> even if the nesting
68   // level was reached.
69   return style_merger.GetResolvedStyle();
70 }
71 
72 // static
CreateResolvedStyleSelector(const string & styleurl,const SharedStyleMap & shared_style_map)73 kmldom::StyleSelectorPtr StyleResolver::CreateResolvedStyleSelector(
74     const string& styleurl, const SharedStyleMap& shared_style_map) {
75   const string empty;
76   StyleMapPtr stylemap = KmlFactory::GetFactory()->CreateStyleMap();
77   PairPtr normal = KmlFactory::GetFactory()->CreatePair();
78   normal->set_key(kmldom::STYLESTATE_NORMAL);
79   StylePtr style = CreateResolvedStyle(styleurl, NULL, shared_style_map, empty,
80                                        NULL, kmldom::STYLESTATE_NORMAL);
81   normal->set_styleselector(AsStyleSelector(ClearIds(style)));
82   stylemap->add_pair(normal);
83   PairPtr highlight = KmlFactory::GetFactory()->CreatePair();
84   highlight->set_key(kmldom::STYLESTATE_HIGHLIGHT);
85   style = CreateResolvedStyle(styleurl, NULL, shared_style_map, empty,
86                               NULL, kmldom::STYLESTATE_HIGHLIGHT);
87   highlight->set_styleselector(AsStyleSelector(ClearIds(style)));
88   stylemap->add_pair(highlight);
89   return stylemap;
90 }
91 
92 }  // endnamespace kmlengine
93