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 declaration of the GetLinks() function and the
27 // definition of the GetLinksParserObserver.
28 
29 #ifndef KML_ENGINE_GET_LINKS_H__
30 #define KML_ENGINE_GET_LINKS_H__
31 
32 #include <vector>
33 #include "kml/dom.h"
34 #include "kml/dom/parser_observer.h"
35 
36 namespace kmlengine {
37 
38 typedef std::vector<string> href_vector_t;
39 
40 // This ParserObserver looks for all elements with an "href" and saves the
41 // content of each to the passed vector.
42 class GetLinksParserObserver : public kmldom::ParserObserver {
43  public:
GetLinksParserObserver(href_vector_t * href_vector)44   GetLinksParserObserver(href_vector_t* href_vector)
45       : href_vector_(href_vector) {}
46 
~GetLinksParserObserver()47   virtual ~GetLinksParserObserver() {}
48 
AddChild(const kmldom::ElementPtr & parent,const kmldom::ElementPtr & child)49   virtual bool AddChild(const kmldom::ElementPtr& parent,
50                         const kmldom::ElementPtr& child) {
51     switch (child->Type()) {
52       default:
53         break;
54       case kmldom::Type_href:
55         // NetworkLink/Link/href, Overlay/Icon/href, ItemIcon/href
56         // Model/Link/href, IconStyle/Icon/href
57         href_vector_->push_back(child->get_char_data());
58         break;
59       case kmldom::Type_targetHref:
60         if (kmldom::Type_Alias == parent->Type()) {
61           href_vector_->push_back(child->get_char_data());
62         }
63         break;
64       case kmldom::Type_styleUrl:
65         href_vector_->push_back(child->get_char_data());
66         break;
67       case kmldom::Type_SchemaData:
68         kmldom::SchemaDataPtr schemadata = kmldom::AsSchemaData(child);
69         if (schemadata->has_schemaurl()) {
70           href_vector_->push_back(schemadata->get_schemaurl());
71         }
72       // TODO: HTML links in description and BalloonStyle/text
73         break;
74     }
75     return true;
76   }
77 
78  private:
79   href_vector_t* href_vector_;
80 };
81 
82 // This function saves to the vector all href's found in the given KML.
83 // This returns false if the vector is NULL or on any parse error. This does
84 // not search the balloon text for links.
85 bool GetLinks(const string& kml, href_vector_t* href_vector);
86 
87 // As GetLinks, but considers only those href's that are relative (local) to
88 // the given KML. This does not search the balloon text for links.
89 bool GetRelativeLinks(const string& kml, href_vector_t* href_vector);
90 
91 }  // end namespace kmlengine
92 
93 #endif  // KML_ENGINE_GET_LINKS_H__
94