1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21
22
23 import java.util.regex.Pattern;
24
25 import org.apache.directory.api.i18n.I18n;
26 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28 import org.apache.directory.api.util.Strings;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @SuppressWarnings("serial")
71 public final class GeneralizedTimeSyntaxChecker extends SyntaxChecker
72 {
73
74 private static final String GENERALIZED_TIME_PATTERN =
75
76 "^\\d{4}"
77
78 + "(0[1-9]|1[0-2])"
79
80 + "(0[1-9]|[12]\\d|3[01])"
81
82 + "([01]\\d|2[0-3])"
83 + "("
84
85 + "([0-5]\\d)"
86
87 + "([0-5]\\d|60)?"
88 + ")?"
89
90 + "([.,]\\d+)?"
91
92 + "(Z|[+-]([01]\\d|2[0-3])([0-5]\\d)?)$";
93
94
95 private static final Pattern DATE_PATTERN = Pattern.compile( GENERALIZED_TIME_PATTERN );
96
97
98
99
100 public static final GeneralizedTimeSyntaxChecker INSTANCE =
101 new GeneralizedTimeSyntaxChecker( SchemaConstants.GENERALIZED_TIME_SYNTAX );
102
103
104
105
106 public static final class Builder extends SCBuilder<GeneralizedTimeSyntaxChecker>
107 {
108
109
110
111 private Builder()
112 {
113 super( SchemaConstants.GENERALIZED_TIME_SYNTAX );
114 }
115
116
117
118
119
120
121 @Override
122 public GeneralizedTimeSyntaxChecker build()
123 {
124 return new GeneralizedTimeSyntaxChecker( oid );
125 }
126 }
127
128
129
130
131
132
133
134 private GeneralizedTimeSyntaxChecker( String oid )
135 {
136 super( oid );
137 }
138
139
140
141
142
143 public static Builder builder()
144 {
145 return new Builder();
146 }
147
148
149
150
151
152 @Override
153 public boolean isValidSyntax( Object value )
154 {
155 String strValue;
156
157 if ( value == null )
158 {
159 if ( LOG.isDebugEnabled() )
160 {
161 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
162 }
163
164 return false;
165 }
166
167 if ( value instanceof String )
168 {
169 strValue = ( String ) value;
170 }
171 else if ( value instanceof byte[] )
172 {
173 strValue = Strings.utf8ToString( ( byte[] ) value );
174 }
175 else
176 {
177 strValue = value.toString();
178 }
179
180
181 if ( strValue.length() < 11 )
182 {
183 if ( LOG.isDebugEnabled() )
184 {
185 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
186 }
187
188 return false;
189 }
190
191
192 boolean result = DATE_PATTERN.matcher( strValue ).find();
193
194 if ( LOG.isDebugEnabled() )
195 {
196 if ( result )
197 {
198 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
199 }
200 else
201 {
202 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
203 }
204 }
205
206 return result;
207 }
208 }