]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Validators/SaxSchemaValidator.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Validators / SaxSchemaValidator.cpp
1 /************************************************************************\r
2 **\r
3 **  Copyright (C) 2010  Strahinja Markovic\r
4 **\r
5 **  This file is part of FlightCrew.\r
6 **\r
7 **  FlightCrew is free software: you can redistribute it and/or modify\r
8 **  it under the terms of the GNU Lesser General Public License as published\r
9 **  by the Free Software Foundation, either version 3 of the License, or\r
10 **  (at your option) any later version.\r
11 **\r
12 **  FlightCrew is distributed in the hope that it will be useful,\r
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 **  GNU Lesser General Public License for more details.\r
16 **\r
17 **  You should have received a copy of the GNU Lesser General Public License\r
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.\r
19 **\r
20 *************************************************************************/\r
21 \r
22 #include <stdafx.h>\r
23 #include "SaxSchemaValidator.h"\r
24 #include "Misc/ErrorResultCollector.h"\r
25 #include <ToXercesStringConverter.h>\r
26 #include <xercesc/sax2/SAX2XMLReader.hpp>\r
27 #include <xercesc/sax2/XMLReaderFactory.hpp>\r
28 #include "Misc/Utilities.h"\r
29 \r
30 namespace FlightCrew\r
31 {\r
32     \r
33 std::vector< Result > SaxSchemaValidator::ValidateAgainstSchema(\r
34     const fs::path &filepath,\r
35     const std::string &external_schema_location,\r
36     const std::vector< const xc::MemBufInputSource* > &schemas )\r
37 {\r
38     boost::scoped_ptr< xc::SAX2XMLReader > parser( xc::XMLReaderFactory::createXMLReader() );\r
39 \r
40     parser->setFeature( xc::XMLUni::fgSAX2CoreValidation,            true  );\r
41     parser->setFeature( xc::XMLUni::fgXercesLoadSchema,              false );\r
42     parser->setFeature( xc::XMLUni::fgXercesUseCachedGrammarInParse, true  );\r
43     parser->setFeature( xc::XMLUni::fgXercesSkipDTDValidation,       true  );\r
44 \r
45     // We don't need DTD validation\r
46     parser->setProperty( xc::XMLUni::fgXercesScannerName, \r
47                          (void*) xc::XMLUni::fgSGXMLScanner );    \r
48 \r
49     LoadSchemas( *parser, external_schema_location, schemas );\r
50 \r
51     ErrorResultCollector collector;\r
52     parser->setErrorHandler( &collector );\r
53 \r
54     try\r
55     {\r
56         parser->parse( filepath.string().c_str() );\r
57     }\r
58 \r
59     catch ( xc::SAXException& exception )\r
60     {\r
61         collector.AddNewExceptionAsResult( exception );\r
62     }\r
63 \r
64     catch ( xc::XMLException& exception )\r
65     {\r
66         collector.AddNewExceptionAsResult( exception );\r
67     }\r
68 \r
69     return Util::AddPathToResults( collector.GetResults(), filepath );\r
70 }\r
71 \r
72 \r
73 void SaxSchemaValidator::LoadSchemas( \r
74     xc::SAX2XMLReader &parser,\r
75     const std::string &external_schema_location,\r
76     const std::vector< const xc::MemBufInputSource* > &schemas )\r
77 {\r
78    \r
79     foreach( const xc::MemBufInputSource *input, schemas )\r
80     {\r
81         parser.loadGrammar( *input, xc::Grammar::SchemaGrammarType, true );  \r
82     }\r
83 \r
84     parser.setProperty( xc::XMLUni::fgXercesSchemaExternalSchemaLocation, \r
85                         (void*) toX( external_schema_location ) );\r
86 }\r
87 \r
88 } //namespace FlightCrew\r