1 /************************************************************************
\r
3 ** Copyright (C) 2010 Strahinja Markovic
\r
5 ** This file is part of FlightCrew.
\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
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
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
20 *************************************************************************/
\r
23 #include "Misc/Utilities.h"
\r
25 #include "UsesCorrectDtd.h"
\r
28 namespace FlightCrew
\r
31 static const int NUM_PEEK_CHARS_FOR_DTD = 300;
\r
32 const std::string XHTML11_SYSTEM_ID = "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";
\r
33 const std::string XHTML11_PUBLIC_ID = "-//W3C//DTD XHTML 1.1//EN";
\r
36 std::vector< Result > UsesCorrectDtd::ValidateFile( const fs::path &filepath )
\r
38 std::vector< Result > results;
\r
42 std::string inital_chars = Util::GetFirstNumCharsFromFile( filepath, NUM_PEEK_CHARS_FOR_DTD );
\r
44 if ( !DtdCorrect( inital_chars ) )
\r
46 int dtd_start = DtdStartLocation( inital_chars );
\r
47 int error_line = dtd_start == -1 ? -1 : Util::LineOfCharIndex( inital_chars, dtd_start );
\r
49 results.push_back( Result( ERROR_XHTML_BAD_DTD )
\r
50 .SetErrorLine( error_line ) );
\r
54 catch ( ExceptionBase& )
\r
57 results.push_back( Result( UNABLE_TO_PERFORM_VALIDATION ) );
\r
60 return Util::AddPathToResults( results, filepath );
\r
64 bool UsesCorrectDtd::DtdCorrect( const std::string &inital_chars )
\r
66 boost::regex expression( "<!DOCTYPE[^\"']+(?:\"|')([^\"']+)(?:\"|')\\s*(?:\"|')([^\"']+)(?:\"|')" );
\r
67 boost::match_results< std::string::const_iterator > matches;
\r
69 // If no dtd was found, we return true... the standard
\r
70 // says the dtd is optional.
\r
71 if ( !boost::regex_search( inital_chars, matches, expression ) )
\r
75 if ( matches[ 1 ] == XHTML11_PUBLIC_ID &&
\r
76 matches[ 2 ] == XHTML11_SYSTEM_ID )
\r
85 int UsesCorrectDtd::DtdStartLocation( const std::string &inital_chars )
\r
87 return static_cast< int >( inital_chars.find( "<!DOCTYPE" ) );
\r
90 } // namespace FlightCrew
\r