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 implementation of the StyleSplitter class and the
27 // SplitStyles function.
28
29 #include "kml/engine/style_splitter.h"
30 #include "kml/engine/style_splitter_internal.h"
31 #include <map>
32 #include "kml/base/string_util.h"
33 #include "kml/dom.h"
34 #include "kml/dom/parser_observer.h"
35 #include "kml/engine/engine_types.h"
36 #include "kml/engine/merge.h"
37
38 namespace kmlengine {
39
NewElement(const kmldom::ElementPtr & element)40 bool StyleSplitter::NewElement(const kmldom::ElementPtr& element) {
41 // Use the first <Document> found as the home of all shared styles.
42 if (!document_ && element->IsA(kmldom::Type_Document)) {
43 document_ = AsDocument(element);
44 }
45 // Don't touch anything inside an <Update>.
46 if (element->IsA(kmldom::Type_Update)) {
47 in_update_ = true;
48 }
49 return true;
50 }
51
52 // static
AsNonDocumentFeature(const kmldom::ElementPtr & element)53 kmldom::FeaturePtr StyleSplitter::AsNonDocumentFeature(
54 const kmldom::ElementPtr& element) {
55 if (kmldom::FeaturePtr feature = kmldom::AsFeature(element)) {
56 return feature->IsA(kmldom::Type_Document) ? NULL : feature;
57 }
58 return NULL;
59 }
60
61 // static
CreateStyleSelector(kmldom::KmlDomType type_id)62 kmldom::StyleSelectorPtr StyleSplitter::CreateStyleSelector(
63 kmldom::KmlDomType type_id) {
64 if (type_id == kmldom::Type_Style) {
65 return kmldom::KmlFactory::GetFactory()->CreateStyle();
66 } else if (type_id == kmldom::Type_StyleMap) {
67 return kmldom::KmlFactory::GetFactory()->CreateStyleMap();
68 } else {
69 return NULL;
70 }
71 }
72
EndElement(const kmldom::ElementPtr & parent,const kmldom::ElementPtr & child)73 bool StyleSplitter::EndElement(const kmldom::ElementPtr& parent,
74 const kmldom::ElementPtr& child) {
75 // If we're not in <Update> and the child is a StyleSelector...
76 if (!in_update_ && document_ && child->IsA(kmldom::Type_StyleSelector)) {
77 // ...and the parent is a Feature, but not a <Document>...
78 if (kmldom::FeaturePtr feature = AsNonDocumentFeature(parent)) {
79 // ...and the feature does _not_ have a <styleUrl>...
80 if (!feature->has_styleurl()) {
81 const string style_id(
82 CreateUniqueId(*shared_style_map_, id_counter_));
83 // ...and this id does not collide:
84 if (shared_style_map_->find(style_id) == shared_style_map_->end()) {
85 ++id_counter_; // Bump the id counter only if it was used.
86 // Create an empty StyleSelector of the child's type and set the id.
87 kmldom::StyleSelectorPtr shared =
88 CreateStyleSelector(child->Type());
89 shared->set_id(style_id);
90 // Merge the children from child into the new StyleSelector.
91 kmlengine::MergeElements(child, shared);
92 // Add the StyleSelector to the Document and the shared style map.
93 document_->add_styleselector(shared);
94 (*shared_style_map_)[shared->get_id()] = shared;
95 // Set the feature's <styleUrl> to this new shared style.
96 feature->set_styleurl(string("#") + style_id);
97 return false; // Do _not_ add this child to this parent.
98 }
99 }
100 }
101 }
102 // Falling through any of the conditions above means no action is taken
103 // to interfere with this child being added to this parent.
104 // Parsing of <Update> is complete so disable guard.
105 if (child->IsA(kmldom::Type_Update)) {
106 in_update_ = false;
107 }
108 return true; // Proceed to add this child to this parent.
109 }
110
SplitStyles(const string & input_kml,string * errors)111 kmldom::ElementPtr SplitStyles(const string& input_kml,
112 string* errors) {
113 SharedStyleMap shared_style_map;
114 StyleSplitter style_splitter(&shared_style_map);
115 kmldom::Parser parser;
116 parser.AddObserver(&style_splitter);
117 return parser.Parse(input_kml, errors);
118 }
119
120 } // end namespace kmlengine
121