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 @SuppressWarnings("serial")
65 public final class UtcTimeSyntaxChecker extends SyntaxChecker
66 {
67
68 private static final String UTC_TIME_PATTERN =
69
70 "^\\d{2}"
71
72 + "(0[1-9]|1[0-2])"
73
74 + "(0[1-9]|[12]\\d|3[01])"
75
76 + "([01]\\d|2[0-3])"
77
78 + "([0-5]\\d)"
79 + "("
80
81 + "([0-5]\\d)?"
82
83 + "(Z|([+-]([01]\\d|2[0-3])[0-5]\\d))?"
84 + ")$";
85
86
87 private static final Pattern DATE_PATTERN = Pattern.compile( UTC_TIME_PATTERN );
88
89
90
91
92 public static final UtcTimeSyntaxChecker INSTANCE = new UtcTimeSyntaxChecker( SchemaConstants.UTC_TIME_SYNTAX );
93
94
95
96
97 public static final class Builder extends SCBuilder<UtcTimeSyntaxChecker>
98 {
99
100
101
102 private Builder()
103 {
104 super( SchemaConstants.UTC_TIME_SYNTAX );
105 }
106
107
108
109
110
111
112 @Override
113 public UtcTimeSyntaxChecker build()
114 {
115 return new UtcTimeSyntaxChecker( oid );
116 }
117 }
118
119
120
121
122
123
124
125 private UtcTimeSyntaxChecker( String oid )
126 {
127 super( oid );
128 }
129
130
131
132
133
134 public static Builder builder()
135 {
136 return new Builder();
137 }
138
139
140
141
142
143 @Override
144 public boolean isValidSyntax( Object value )
145 {
146 String strValue;
147
148 if ( value == null )
149 {
150 if ( LOG.isDebugEnabled() )
151 {
152 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
153 }
154
155 return false;
156 }
157
158 if ( value instanceof String )
159 {
160 strValue = ( String ) value;
161 }
162 else if ( value instanceof byte[] )
163 {
164 strValue = Strings.utf8ToString( ( byte[] ) value );
165 }
166 else
167 {
168 strValue = value.toString();
169 }
170
171
172 if ( strValue.length() < 11 )
173 {
174 if ( LOG.isDebugEnabled() )
175 {
176 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
177 }
178
179 return false;
180 }
181
182
183 boolean result;
184
185 synchronized ( DATE_PATTERN )
186 {
187 result = DATE_PATTERN.matcher( strValue ).find();
188 }
189
190 if ( result )
191 {
192 if ( LOG.isDebugEnabled() )
193 {
194 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
195 }
196 }
197 else
198 {
199 if ( LOG.isDebugEnabled() )
200 {
201 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
202 }
203 }
204
205 return result;
206 }
207 }