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.HashSet;
24 import java.util.Set;
25 import java.util.regex.Pattern;
26 import java.util.regex.PatternSyntaxException;
27
28 import org.apache.directory.api.i18n.I18n;
29 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
30 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
31 import org.apache.directory.api.util.Strings;
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 @SuppressWarnings("serial")
57 public final class FacsimileTelephoneNumberSyntaxChecker extends SyntaxChecker
58 {
59
60 private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*]+)+$";
61
62
63 private String defaultRegexp;
64
65
66 private Pattern defaultPattern = Pattern.compile( DEFAULT_REGEXP );
67
68
69 private static final String TWO_DIMENSIONAL = "twoDimensional";
70 private static final String FINE_RESOLUTION = "fineResolution";
71 private static final String UNLIMITED_LENGTH = "unlimitedLength";
72 private static final String B4_LENGTH = "b4Length";
73 private static final String A3_LENGTH = "a3Width";
74 private static final String B4_WIDTH = "b4Width";
75 private static final String UNCOMPRESSED = "uncompressed";
76
77
78 private static Set<String> faxParameters = new HashSet<>();
79
80
81 static
82 {
83 faxParameters.add( Strings.toLowerCaseAscii( TWO_DIMENSIONAL ) );
84 faxParameters.add( Strings.toLowerCaseAscii( FINE_RESOLUTION ) );
85 faxParameters.add( Strings.toLowerCaseAscii( UNLIMITED_LENGTH ) );
86 faxParameters.add( Strings.toLowerCaseAscii( B4_LENGTH ) );
87 faxParameters.add( Strings.toLowerCaseAscii( A3_LENGTH ) );
88 faxParameters.add( Strings.toLowerCaseAscii( B4_WIDTH ) );
89 faxParameters.add( Strings.toLowerCaseAscii( UNCOMPRESSED ) );
90 }
91
92
93
94
95 public static final FacsimileTelephoneNumberSyntaxChecker INSTANCE =
96 new FacsimileTelephoneNumberSyntaxChecker( SchemaConstants.FACSIMILE_TELEPHONE_NUMBER_SYNTAX );
97
98
99
100
101 public static final class Builder extends SCBuilder<FacsimileTelephoneNumberSyntaxChecker>
102 {
103
104 private String defaultRegexp;
105
106
107 private Pattern defaultPattern;
108
109
110
111
112 private Builder()
113 {
114 super( SchemaConstants.FACSIMILE_TELEPHONE_NUMBER_SYNTAX );
115 setDefaultRegexp( DEFAULT_REGEXP );
116 }
117
118
119
120
121
122
123
124 @Override
125 public FacsimileTelephoneNumberSyntaxChecker build()
126 {
127 return new FacsimileTelephoneNumberSyntaxChecker( oid, defaultRegexp, defaultPattern );
128 }
129
130
131
132
133
134
135
136
137 public Builder setDefaultRegexp( String regexp )
138 {
139 defaultRegexp = regexp;
140
141 try
142 {
143 defaultPattern = Pattern.compile( regexp );
144 }
145 catch ( PatternSyntaxException pse )
146 {
147
148 defaultPattern = Pattern.compile( DEFAULT_REGEXP );
149 }
150
151 return this;
152 }
153 }
154
155
156
157
158
159
160
161 private FacsimileTelephoneNumberSyntaxChecker( String oid )
162 {
163 super( oid );
164 }
165
166
167
168
169
170
171
172
173
174 private FacsimileTelephoneNumberSyntaxChecker( String oid, String defaultRegexp, Pattern defaultPattern )
175 {
176 super( oid );
177
178 this.defaultPattern = defaultPattern;
179 this.defaultRegexp = defaultRegexp;
180 }
181
182
183
184
185
186 public static Builder builder()
187 {
188 return new Builder();
189 }
190
191
192
193
194
195
196
197 public String getRegexp()
198 {
199 if ( defaultRegexp == null )
200 {
201 return DEFAULT_REGEXP;
202 }
203 else
204 {
205 return defaultRegexp;
206 }
207 }
208
209
210
211
212
213 @Override
214 public boolean isValidSyntax( Object value )
215 {
216 String strValue;
217
218 if ( value == null )
219 {
220 if ( LOG.isDebugEnabled() )
221 {
222 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
223 }
224
225 return false;
226 }
227
228 if ( value instanceof String )
229 {
230 strValue = ( String ) value;
231 }
232 else if ( value instanceof byte[] )
233 {
234 strValue = Strings.utf8ToString( ( byte[] ) value );
235 }
236 else
237 {
238 strValue = value.toString();
239 }
240
241 if ( strValue.length() == 0 )
242 {
243 if ( LOG.isDebugEnabled() )
244 {
245 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
246 }
247
248 return false;
249 }
250
251
252
253 int dollarPos = strValue.indexOf( '$' );
254
255 if ( dollarPos == -1 )
256 {
257
258 boolean result = defaultPattern.matcher( strValue ).matches();
259
260 if ( LOG.isDebugEnabled() )
261 {
262 if ( result )
263 {
264 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
265 }
266 else
267 {
268 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
269 }
270 }
271
272 return result;
273 }
274
275
276 if ( dollarPos > 0 )
277 {
278 boolean result = defaultPattern.matcher( strValue.substring( 0, dollarPos - 1 ) ).matches();
279
280 if ( LOG.isDebugEnabled() )
281 {
282 if ( result )
283 {
284 LOG.debug( I18n.err( I18n.MSG_13701_SYNTAX_VALID, value ) );
285 }
286 else
287 {
288 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
289
290 return false;
291 }
292 }
293
294
295
296
297 Set<String> paramsSeen = new HashSet<>();
298
299 while ( dollarPos > 0 )
300 {
301 String faxParam;
302 int newDollar = strValue.indexOf( '$', dollarPos + 1 );
303
304 if ( newDollar == -1 )
305 {
306 faxParam = strValue.substring( dollarPos + 1 );
307 }
308 else
309 {
310 faxParam = strValue.substring( dollarPos + 1, newDollar );
311 }
312
313 if ( faxParam.length() == 0 )
314 {
315
316 if ( LOG.isDebugEnabled() )
317 {
318 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
319 }
320
321 return false;
322 }
323
324
325 faxParam = Strings.toLowerCaseAscii( faxParam );
326
327 if ( !faxParameters.contains( faxParam ) || paramsSeen.contains( faxParam ) )
328 {
329
330 if ( LOG.isDebugEnabled() )
331 {
332 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
333 }
334
335 return false;
336 }
337 else
338 {
339
340
341 paramsSeen.add( faxParam );
342 }
343
344 dollarPos = newDollar;
345 }
346
347 if ( LOG.isDebugEnabled() )
348 {
349 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
350 }
351
352 return true;
353 }
354
355
356 if ( LOG.isDebugEnabled() )
357 {
358 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
359 }
360
361 return false;
362 }
363 }