2
2
3
3
import com .relogiclabs .json .schema .exception .InvalidDateTimeException ;
4
4
import 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 ;
6
6
import com .relogiclabs .json .schema .internal .util .LexerErrorListener ;
7
+ import com .relogiclabs .json .schema .internal .util .Reference ;
7
8
import org .antlr .v4 .runtime .CharStreams ;
8
9
import org .antlr .v4 .runtime .Token ;
9
10
10
11
import java .util .HashMap ;
11
12
import java .util .List ;
12
13
import java .util .Map ;
13
14
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 ;
14
77
import static com .relogiclabs .json .schema .internal .util .StringHelper .concat ;
15
78
import static com .relogiclabs .json .schema .message .ErrorCode .DINV02 ;
16
79
17
- public class DateTimeValidator {
80
+ public final class DateTimeValidator {
18
81
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 " ;
20
83
21
84
private static final Map <String , SegmentProcessor > PROCESSORS ;
22
85
private final DateTimeLexer dateTimeLexer ;
23
86
private final List <Token > lexerTokens ;
24
87
25
88
static {
26
89
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 );
58
125
}
59
126
60
127
@ SuppressWarnings ("unchecked" )
@@ -74,7 +141,7 @@ private void Validate(String input, DateTimeContext context) {
74
141
concat ("Invalid " , context .getType (), " input format" ));
75
142
76
143
context .validate ();
77
- DebugUtils .print (context );
144
+ DebugUtilities .print (context );
78
145
}
79
146
80
147
public void ValidateDate (String input ) {
@@ -85,22 +152,24 @@ public void ValidateTime(String input) {
85
152
Validate (input , new DateTimeContext (DateTimeType .TIME_TYPE ));
86
153
}
87
154
88
- public boolean IsValidDate (String input ) {
155
+ public boolean IsValidDate (String input , Reference < String > error ) {
89
156
try {
90
157
ValidateDate (input );
91
158
return true ;
92
159
} catch (InvalidDateTimeException e ) {
93
- DebugUtils .print (e );
160
+ DebugUtilities .print (e );
161
+ error .setValue (e .getMessage ());
94
162
return false ;
95
163
}
96
164
}
97
165
98
- public boolean IsValidTime (String input ) {
166
+ public boolean IsValidTime (String input , Reference < String > error ) {
99
167
try {
100
168
ValidateTime (input );
101
169
return true ;
102
170
} catch (InvalidDateTimeException e ) {
103
- DebugUtils .print (e );
171
+ DebugUtilities .print (e );
172
+ error .setValue (e .getMessage ());
104
173
return false ;
105
174
}
106
175
}
0 commit comments