22
33import com .relogiclabs .json .schema .exception .InvalidDateTimeException ;
44import com .relogiclabs .json .schema .internal .antlr .DateTimeLexer ;
5- import com .relogiclabs .json .schema .internal .util .DebugUtils ;
5+ import com .relogiclabs .json .schema .internal .util .DebugUtilities ;
66import com .relogiclabs .json .schema .internal .util .LexerErrorListener ;
7+ import com .relogiclabs .json .schema .internal .util .Reference ;
78import org .antlr .v4 .runtime .CharStreams ;
89import org .antlr .v4 .runtime .Token ;
910
1011import java .util .HashMap ;
1112import java .util .List ;
1213import java .util .Map ;
1314
15+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .CLOCK_AM_PM ;
16+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .DAY_NUMBER ;
17+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .DAY_NUMBER2 ;
18+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .ERA ;
19+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER ;
20+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER1 ;
21+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER2 ;
22+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER3 ;
23+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER4 ;
24+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER5 ;
25+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .FRACTION_NUMBER6 ;
26+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .HOUR_NUMBER ;
27+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .HOUR_NUMBER2 ;
28+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .MINUTE_NUMBER ;
29+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .MINUTE_NUMBER2 ;
30+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .MONTH_NAME ;
31+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .MONTH_NUMBER ;
32+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .MONTH_NUMBER2 ;
33+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .MONTH_SHORT_NAME ;
34+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .SECOND_NUMBER ;
35+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .SECOND_NUMBER2 ;
36+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .SYMBOL ;
37+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .TEXT ;
38+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .UTC_OFFSET_HOUR ;
39+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .UTC_OFFSET_TIME1 ;
40+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .UTC_OFFSET_TIME2 ;
41+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .WEEKDAY_NAME ;
42+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .WEEKDAY_SHORT_NAME ;
43+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .WHITESPACE ;
44+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .YEAR_NUMBER2 ;
45+ import static com .relogiclabs .json .schema .internal .antlr .DateTimeLexer .YEAR_NUMBER4 ;
46+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .ClockAmPm ;
47+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .DayNumber ;
48+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .DayNumber2 ;
49+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .Era ;
50+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber ;
51+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber1 ;
52+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber2 ;
53+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber3 ;
54+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber4 ;
55+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber5 ;
56+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .FractionNumber6 ;
57+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .HourNumber ;
58+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .HourNumber2 ;
59+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .MinuteNumber ;
60+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .MinuteNumber2 ;
61+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .MonthName ;
62+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .MonthNumber ;
63+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .MonthNumber2 ;
64+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .MonthShortName ;
65+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .SecondNumber ;
66+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .SecondNumber2 ;
67+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .Symbol ;
68+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .Text ;
69+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .UtcOffsetHour ;
70+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .UtcOffsetTime1 ;
71+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .UtcOffsetTime2 ;
72+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .WeekdayName ;
73+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .WeekdayShortName ;
74+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .Whitespace ;
75+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .YearNumber2 ;
76+ import static com .relogiclabs .json .schema .internal .time .SegmentProcessor .YearNumber4 ;
1477import static com .relogiclabs .json .schema .internal .util .StringHelper .concat ;
1578import static com .relogiclabs .json .schema .message .ErrorCode .DINV02 ;
1679
17- public class DateTimeValidator {
80+ public final class DateTimeValidator {
1881 public static final String ISO_8601_DATE = "YYYY-MM-DD" ;
19- public static final String ISO_8601_TIME = "YYYY-MM-DD'T'hh:mm:ss.fffZZ " ;
82+ public static final String ISO_8601_TIME = "YYYY-MM-DD'T'hh:mm:ss.FZZ " ;
2083
2184 private static final Map <String , SegmentProcessor > PROCESSORS ;
2285 private final DateTimeLexer dateTimeLexer ;
2386 private final List <Token > lexerTokens ;
2487
2588 static {
2689 PROCESSORS = new HashMap <>(50 );
27- PROCESSORS .put ("TEXT" , SegmentProcessor .Text );
28- PROCESSORS .put ("SYMBOL" , SegmentProcessor .Symbol );
29- PROCESSORS .put ("WHITESPACE" , SegmentProcessor .Whitespace );
30- PROCESSORS .put ("ERA" , SegmentProcessor .Era );
31- PROCESSORS .put ("YEAR_NUM4" , SegmentProcessor .YearNum4 );
32- PROCESSORS .put ("YEAR_NUM2" , SegmentProcessor .YearNum2 );
33- PROCESSORS .put ("MONTH_NAME" , SegmentProcessor .MonthName );
34- PROCESSORS .put ("MONTH_SHORT_NAME" , SegmentProcessor .MonthShortName );
35- PROCESSORS .put ("MONTH_NUM2" , SegmentProcessor .MonthNum2 );
36- PROCESSORS .put ("MONTH_NUM" , SegmentProcessor .MonthNum );
37- PROCESSORS .put ("WEEKDAY_NAME" , SegmentProcessor .WeekdayName );
38- PROCESSORS .put ("WEEKDAY_SHORT_NAME" , SegmentProcessor .WeekdayShortName );
39- PROCESSORS .put ("DAY_NUM2" , SegmentProcessor .DayNum2 );
40- PROCESSORS .put ("DAY_NUM" , SegmentProcessor .DayNum );
41- PROCESSORS .put ("AM_PM" , SegmentProcessor .AmPm );
42- PROCESSORS .put ("HOUR_NUM2" , SegmentProcessor .HourNum2 );
43- PROCESSORS .put ("HOUR_NUM" , SegmentProcessor .HourNum );
44- PROCESSORS .put ("MINUTE_NUM2" , SegmentProcessor .MinuteNum2 );
45- PROCESSORS .put ("MINUTE_NUM" , SegmentProcessor .MinuteNum );
46- PROCESSORS .put ("SECOND_NUM2" , SegmentProcessor .SecondNum2 );
47- PROCESSORS .put ("SECOND_NUM" , SegmentProcessor .SecondNum );
48- PROCESSORS .put ("FRACTION_NUM" , SegmentProcessor .FractionNum );
49- PROCESSORS .put ("FRACTION_NUM01" , SegmentProcessor .FractionNum01 );
50- PROCESSORS .put ("FRACTION_NUM02" , SegmentProcessor .FractionNum02 );
51- PROCESSORS .put ("FRACTION_NUM03" , SegmentProcessor .FractionNum03 );
52- PROCESSORS .put ("FRACTION_NUM04" , SegmentProcessor .FractionNum04 );
53- PROCESSORS .put ("FRACTION_NUM05" , SegmentProcessor .FractionNum05 );
54- PROCESSORS .put ("FRACTION_NUM06" , SegmentProcessor .FractionNum06 );
55- PROCESSORS .put ("UTC_OFFSET_HOUR" , SegmentProcessor .UtcOffsetHour );
56- PROCESSORS .put ("UTC_OFFSET_TIME1" , SegmentProcessor .UtcOffsetTime1 );
57- PROCESSORS .put ("UTC_OFFSET_TIME2" , SegmentProcessor .UtcOffsetTime2 );
90+ addProcessor (TEXT , Text );
91+ addProcessor (SYMBOL , Symbol );
92+ addProcessor (WHITESPACE , Whitespace );
93+ addProcessor (ERA , Era );
94+ addProcessor (YEAR_NUMBER4 , YearNumber4 );
95+ addProcessor (YEAR_NUMBER2 , YearNumber2 );
96+ addProcessor (MONTH_NAME , MonthName );
97+ addProcessor (MONTH_SHORT_NAME , MonthShortName );
98+ addProcessor (MONTH_NUMBER2 , MonthNumber2 );
99+ addProcessor (MONTH_NUMBER , MonthNumber );
100+ addProcessor (WEEKDAY_NAME , WeekdayName );
101+ addProcessor (WEEKDAY_SHORT_NAME , WeekdayShortName );
102+ addProcessor (DAY_NUMBER2 , DayNumber2 );
103+ addProcessor (DAY_NUMBER , DayNumber );
104+ addProcessor (CLOCK_AM_PM , ClockAmPm );
105+ addProcessor (HOUR_NUMBER2 , HourNumber2 );
106+ addProcessor (HOUR_NUMBER , HourNumber );
107+ addProcessor (MINUTE_NUMBER2 , MinuteNumber2 );
108+ addProcessor (MINUTE_NUMBER , MinuteNumber );
109+ addProcessor (SECOND_NUMBER2 , SecondNumber2 );
110+ addProcessor (SECOND_NUMBER , SecondNumber );
111+ addProcessor (FRACTION_NUMBER , FractionNumber );
112+ addProcessor (FRACTION_NUMBER1 , FractionNumber1 );
113+ addProcessor (FRACTION_NUMBER2 , FractionNumber2 );
114+ addProcessor (FRACTION_NUMBER3 , FractionNumber3 );
115+ addProcessor (FRACTION_NUMBER4 , FractionNumber4 );
116+ addProcessor (FRACTION_NUMBER5 , FractionNumber5 );
117+ addProcessor (FRACTION_NUMBER6 , FractionNumber6 );
118+ addProcessor (UTC_OFFSET_HOUR , UtcOffsetHour );
119+ addProcessor (UTC_OFFSET_TIME1 , UtcOffsetTime1 );
120+ addProcessor (UTC_OFFSET_TIME2 , UtcOffsetTime2 );
121+ }
122+
123+ private static void addProcessor (int index , SegmentProcessor processor ) {
124+ PROCESSORS .put (DateTimeLexer .ruleNames [index - 1 ], processor );
58125 }
59126
60127 @ SuppressWarnings ("unchecked" )
@@ -74,7 +141,7 @@ private void Validate(String input, DateTimeContext context) {
74141 concat ("Invalid " , context .getType (), " input format" ));
75142
76143 context .validate ();
77- DebugUtils .print (context );
144+ DebugUtilities .print (context );
78145 }
79146
80147 public void ValidateDate (String input ) {
@@ -85,22 +152,24 @@ public void ValidateTime(String input) {
85152 Validate (input , new DateTimeContext (DateTimeType .TIME_TYPE ));
86153 }
87154
88- public boolean IsValidDate (String input ) {
155+ public boolean IsValidDate (String input , Reference < String > error ) {
89156 try {
90157 ValidateDate (input );
91158 return true ;
92159 } catch (InvalidDateTimeException e ) {
93- DebugUtils .print (e );
160+ DebugUtilities .print (e );
161+ error .setValue (e .getMessage ());
94162 return false ;
95163 }
96164 }
97165
98- public boolean IsValidTime (String input ) {
166+ public boolean IsValidTime (String input , Reference < String > error ) {
99167 try {
100168 ValidateTime (input );
101169 return true ;
102170 } catch (InvalidDateTimeException e ) {
103- DebugUtils .print (e );
171+ DebugUtilities .print (e );
172+ error .setValue (e .getMessage ());
104173 return false ;
105174 }
106175 }
0 commit comments