]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Validators/Xhtml/UsesCorrectDtd.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Validators / Xhtml / UsesCorrectDtd.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 "Misc/Utilities.h"\r
24 #include "Result.h"\r
25 #include "UsesCorrectDtd.h"\r
26 \r
27 \r
28 namespace FlightCrew\r
29 {\r
30 \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
34 \r
35 \r
36 std::vector< Result > UsesCorrectDtd::ValidateFile( const fs::path &filepath )\r
37 {\r
38     std::vector< Result > results;\r
39 \r
40     try\r
41     {\r
42         std::string inital_chars = Util::GetFirstNumCharsFromFile( filepath, NUM_PEEK_CHARS_FOR_DTD );\r
43 \r
44         if ( !DtdCorrect( inital_chars ) )\r
45         {\r
46             int dtd_start  = DtdStartLocation( inital_chars );\r
47             int error_line = dtd_start == -1 ? -1 : Util::LineOfCharIndex( inital_chars, dtd_start ); \r
48 \r
49             results.push_back( Result( ERROR_XHTML_BAD_DTD )\r
50                 .SetErrorLine( error_line ) );\r
51         }\r
52     }\r
53 \r
54     catch ( ExceptionBase& )\r
55     {\r
56         results.clear();\r
57         results.push_back( Result( UNABLE_TO_PERFORM_VALIDATION ) );        \r
58     }  \r
59 \r
60     return Util::AddPathToResults( results, filepath );\r
61 }\r
62 \r
63 \r
64 bool UsesCorrectDtd::DtdCorrect( const std::string &inital_chars )\r
65 {\r
66     boost::regex expression( "<!DOCTYPE[^\"']+(?:\"|')([^\"']+)(?:\"|')\\s*(?:\"|')([^\"']+)(?:\"|')" );\r
67     boost::match_results< std::string::const_iterator > matches;\r
68 \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
72 \r
73         return true;\r
74     \r
75     if ( matches[ 1 ] == XHTML11_PUBLIC_ID &&\r
76          matches[ 2 ] == XHTML11_SYSTEM_ID )\r
77     {\r
78         return true;\r
79     }\r
80 \r
81     return false;    \r
82 }\r
83 \r
84 \r
85 int UsesCorrectDtd::DtdStartLocation( const std::string &inital_chars )\r
86 {\r
87     return static_cast< int >( inital_chars.find( "<!DOCTYPE" ) );\r
88 }\r
89 \r
90 } // namespace FlightCrew\r