]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Validators/Opf/DateValid.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Validators / Opf / DateValid.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 "DateValid.h"\r
24 #include <ToXercesStringConverter.h>\r
25 #include <FromXercesStringConverter.h>\r
26 #include <XmlUtils.h>\r
27 \r
28 // We could make it so that some of these use the previous ones,\r
29 // but this is more readable.\r
30 static const std::string DATE_Y        \r
31     = "\\d{4}";\r
32 static const std::string DATE_YM      \r
33     = "\\d{4}-\\d{2}";\r
34 static const std::string DATE_YMD   \r
35     = "\\d{4}-\\d{2}-\\d{2}";\r
36 static const std::string DATE_YMD_HM\r
37     = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(((\\+|-)\\d{2}:\\d{2})|Z)";\r
38 static const std::string DATE_YMD_HMS\r
39     = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(((\\+|-)\\d{2}:\\d{2})|Z)";\r
40 static const std::string DATE_YMD_HMSF\r
41     = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+(((\\+|-)\\d{2}:\\d{2})|Z)";\r
42 \r
43 static const boost::regex DATE_Y_REGEX( DATE_Y );\r
44 static const boost::regex DATE_YM_REGEX( DATE_YM );\r
45 static const boost::regex DATE_YMD_REGEX( DATE_YMD );\r
46 static const boost::regex DATE_YMD_HM_REGEX( DATE_YMD_HM );\r
47 static const boost::regex DATE_YMD_HMS_REGEX( DATE_YMD_HMS );\r
48 static const boost::regex DATE_YMD_HMSF_REGEX( DATE_YMD_HMSF );\r
49 \r
50 \r
51 namespace FlightCrew\r
52 {\r
53 \r
54 std::vector< Result > DateValid::ValidateXml(\r
55     const xc::DOMDocument &document,\r
56     const fs::path& )\r
57 {\r
58     std::vector< xc::DOMElement* > dates = xe::GetElementsByQName( \r
59         document, QName( "date", DC_XML_NAMESPACE ) );\r
60 \r
61     std::vector< Result > results;\r
62 \r
63     foreach( xc::DOMElement* date, dates )\r
64     {\r
65         std::string date_string = fromX( date->getTextContent() );\r
66 \r
67         if ( date_string.empty() || !ValidDateString( date_string ) )\r
68         {\r
69             results.push_back(  \r
70                 ResultWithNodeLocation( ERROR_OPF_BAD_DATE_VALUE, *date )\r
71                 .AddMessageArgument( date_string )\r
72                 );\r
73         }\r
74     }\r
75 \r
76     return results;\r
77 }\r
78 \r
79 \r
80 // For the date format specification, see here:\r
81 // http://www.w3.org/TR/NOTE-datetime\r
82 bool DateValid::ValidDateString( const std::string &date_string )\r
83 {\r
84     return boost::regex_match( date_string, DATE_Y_REGEX )       ||\r
85            boost::regex_match( date_string, DATE_YM_REGEX )      ||\r
86            boost::regex_match( date_string, DATE_YMD_REGEX )     ||\r
87            boost::regex_match( date_string, DATE_YMD_HM_REGEX )  ||\r
88            boost::regex_match( date_string, DATE_YMD_HMS_REGEX ) ||\r
89            boost::regex_match( date_string, DATE_YMD_HMSF_REGEX );\r
90 }\r
91 \r
92 } // namespace FlightCrew\r
93 \r