Goby v2
xml_parser.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 // based initially on work in C++ Cookbook by D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, and Jeff Cogswell. Copyright 2006 O'Reilly Media, INc., 0-596-00761-2.
24 
25 #ifndef XML_PARSER20091211H
26 #define XML_PARSER20091211H
27 
28 #include <exception>
29 #include <fstream>
30 #include <iostream> // cout
31 #include <memory> // auto_ptr
32 #include <vector>
33 
34 #include <xercesc/sax2/DefaultHandler.hpp>
35 #include <xercesc/sax2/SAX2XMLReader.hpp>
36 #include <xercesc/sax2/XMLReaderFactory.hpp>
37 #include <xercesc/util/PlatformUtils.hpp>
38 
39 #include "message_schema.xsd.h"
40 #include "xerces_strings.h"
41 
42 // RAII utility that initializes the parser and frees resources
43 // when it goes out of scope
45 {
46  public:
47  XercesInitializer() { xercesc::XMLPlatformUtils::Initialize(); }
48  ~XercesInitializer() { xercesc::XMLPlatformUtils::Terminate(); }
49 
50  private:
51  // Prohibit copying and assignment
53  XercesInitializer& operator=(const XercesInitializer&);
54 };
55 
56 class XMLParser
57 {
58  public:
59  XMLParser(xercesc::DefaultHandler& content, xercesc::DefaultHandler& error)
60  : content_(content), error_(error)
61  {
62  }
63 
64  bool parse(const std::string& file)
65  {
66  // Initialize Xerces and obtain parser
67  XercesInitializer init;
68  std::auto_ptr<xercesc::SAX2XMLReader> parser(xercesc::XMLReaderFactory::createXMLReader());
69 
70  // write XML schema to file
71  const std::string schema = "/tmp/dccl_message_schema.xsd";
72  std::ofstream fout;
73  fout.open(schema.c_str());
74  for (unsigned i = 0; i < message_schema_xsd_len; ++i) fout << message_schema_xsd[i];
75  fout.close();
76 
77  const XMLCh* xs_schema = fromNative(schema.c_str());
78  const XMLCh* const schema_location = xs_schema;
79  parser->setFeature(xercesc::XMLUni::fgSAX2CoreValidation, true);
80  parser->setProperty(xercesc::XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,
81  (void*)schema_location);
82 
83  parser->setContentHandler(&content_);
84  parser->setErrorHandler(&error_);
85 
86  // Parse the XML document
87  parser->parse(file.c_str());
88 
89  return true;
90  }
91 
92  private:
93  xercesc::DefaultHandler& content_;
94  xercesc::DefaultHandler& error_;
95 };
96 
97 #endif