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 XsdHandler class.
27 
28 #ifndef KML_XSD_XSD_HANDLER_H__
29 #define KML_XSD_XSD_HANDLER_H__
30 
31 #include <stack>
32 #include "boost/scoped_ptr.hpp"
33 #include "kml/base/expat_handler.h"
34 #include "kml/xsd/xsd_type.h"
35 
36 namespace kmlbase {
37 class Attributes;
38 }
39 
40 namespace kmlxsd {
41 
42 class XsdFile;
43 
44 // This ExpatHandler specialization parses an XSD file.  Overall usage is:
45 //   // Read the XSD file.
46 //   string xsd_data;
47 //   File::ReadFileToString("my.xsd", &xsd_data);
48 //   // Parse it.
49 //   XsdHandler my_xsd_handler;
50 //   ExpatParser(xsd_data, &my_xsd_handler);
51 class XsdHandler : public kmlbase::ExpatHandler {
52  public:
XsdHandler(XsdFile * xsd_file)53   XsdHandler(XsdFile* xsd_file)
54     : xsd_file_(xsd_file),
55       current_type_(NULL) {
56   }
~XsdHandler()57   virtual ~XsdHandler() {}
58 
59   // ExpatHandler::StartElement.
60   virtual void StartElement(const string& element_name,
61                             const kmlbase::StringVector& atts);
62 
63   // ExpatHandler::EndElement.
64   virtual void EndElement(const string& element_name);
65 
66   // ExpatHandler::CharData.  No XSD element has character data.
CharData(const string & s)67   virtual void CharData(const string& s) {}
68 
69  private:
70   XsdFile* xsd_file_;
71 
72   // <xs:element> processing.
73   void StartXsElement(const kmlbase::Attributes& attributes);
74 
75   // <xs:complexType> processing.
76   void StartComplexType(const kmlbase::Attributes& attributes);
77   void StartExtension(const kmlbase::Attributes& attributes);
78 
79   // <xs:simpleType> processing.
80   void StartSimpleType(const kmlbase::Attributes& attributes);
81   void StartRestriction(const kmlbase::Attributes& attributes);
82   void StartEnumeration(const kmlbase::Attributes& attributes);
83 
84   void EndType();
85   XsdTypePtr current_type_;
86 
87   std::stack<string> parse_;
88 };
89 
90 }  // end namespace kmlxsd
91 
92 #endif // KML_XSD_XSD_HANDLER_H__
93