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 "DateValid.h"
\r
24 #include <ToXercesStringConverter.h>
\r
25 #include <FromXercesStringConverter.h>
\r
26 #include <XmlUtils.h>
\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
32 static const std::string DATE_YM
\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
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
51 namespace FlightCrew
\r
54 std::vector< Result > DateValid::ValidateXml(
\r
55 const xc::DOMDocument &document,
\r
58 std::vector< xc::DOMElement* > dates = xe::GetElementsByQName(
\r
59 document, QName( "date", DC_XML_NAMESPACE ) );
\r
61 std::vector< Result > results;
\r
63 foreach( xc::DOMElement* date, dates )
\r
65 std::string date_string = fromX( date->getTextContent() );
\r
67 if ( date_string.empty() || !ValidDateString( date_string ) )
\r
70 ResultWithNodeLocation( ERROR_OPF_BAD_DATE_VALUE, *date )
\r
71 .AddMessageArgument( date_string )
\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
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
92 } // namespace FlightCrew
\r