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 import java.util.regex.PatternSyntaxException;
25
26 import org.apache.directory.api.i18n.I18n;
27 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
28 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
29 import org.apache.directory.api.util.Strings;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @SuppressWarnings("serial")
48 public final class TelephoneNumberSyntaxChecker extends SyntaxChecker
49 {
50
51 private static final String DEFAULT_REGEXP = "^ *[+]? *((\\([0-9- ,;/#*]+\\))|[0-9- ,;/#*]+)+$";
52
53
54 private final String defaultRegexp;
55
56
57 private final Pattern defaultPattern;
58
59
60
61
62 public static final TelephoneNumberSyntaxChecker INSTANCE =
63 new TelephoneNumberSyntaxChecker( SchemaConstants.TELEPHONE_NUMBER_SYNTAX );
64
65
66
67
68 public static final class Builder extends SCBuilder<TelephoneNumberSyntaxChecker>
69 {
70
71 private String defaultRegexp;
72
73
74 private Pattern defaultPattern;
75
76
77
78
79 private Builder()
80 {
81 super( SchemaConstants.TELEPHONE_NUMBER_SYNTAX );
82 setDefaultRegexp( DEFAULT_REGEXP );
83 }
84
85
86
87
88
89
90 @Override
91 public TelephoneNumberSyntaxChecker build()
92 {
93 return new TelephoneNumberSyntaxChecker( oid, defaultRegexp, defaultPattern );
94 }
95
96
97
98
99
100
101
102
103 public Builder setDefaultRegexp( String regexp )
104 {
105 defaultRegexp = regexp;
106
107 try
108 {
109 defaultPattern = Pattern.compile( regexp );
110 }
111 catch ( PatternSyntaxException pse )
112 {
113
114 defaultPattern = Pattern.compile( DEFAULT_REGEXP );
115 }
116
117 return this;
118 }
119 }
120
121
122
123
124
125
126
127 private TelephoneNumberSyntaxChecker( String oid )
128 {
129 this( oid, DEFAULT_REGEXP, Pattern.compile( DEFAULT_REGEXP ) );
130 }
131
132
133
134
135
136
137
138
139
140 private TelephoneNumberSyntaxChecker( String oid, String defaultRegexp, Pattern defaultPattern )
141 {
142 super( oid );
143
144 this.defaultPattern = defaultPattern;
145 this.defaultRegexp = defaultRegexp;
146 }
147
148
149
150
151
152 public static Builder builder()
153 {
154 return new Builder();
155 }
156
157
158
159
160
161
162
163 public String getRegexp()
164 {
165 return defaultRegexp;
166 }
167
168
169
170
171
172 @Override
173 public boolean isValidSyntax( Object value )
174 {
175 String strValue;
176
177 if ( value == null )
178 {
179 if ( LOG.isDebugEnabled() )
180 {
181 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
182 }
183
184 return false;
185 }
186
187 if ( value instanceof String )
188 {
189 strValue = ( String ) value;
190 }
191 else if ( value instanceof byte[] )
192 {
193 strValue = Strings.utf8ToString( ( byte[] ) value );
194 }
195 else
196 {
197 strValue = value.toString();
198 }
199
200 if ( strValue.length() == 0 )
201 {
202 if ( LOG.isDebugEnabled() )
203 {
204 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
205 }
206
207 return false;
208 }
209
210
211 boolean result;
212
213
214 synchronized ( defaultPattern )
215 {
216 result = defaultPattern.matcher( strValue ).matches();
217 }
218
219 if ( LOG.isDebugEnabled() )
220 {
221 if ( result )
222 {
223 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
224 }
225 else
226 {
227 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
228 }
229 }
230
231 return result;
232 }
233 }