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 public Builder setDefaultRegexp( String regexp )
103 {
104 defaultRegexp = regexp;
105
106 try
107 {
108 defaultPattern = Pattern.compile( regexp );
109 }
110 catch ( PatternSyntaxException pse )
111 {
112
113 defaultPattern = Pattern.compile( DEFAULT_REGEXP );
114 }
115
116 return this;
117 }
118 }
119
120
121
122
123
124
125
126 private TelephoneNumberSyntaxChecker( String oid )
127 {
128 this( oid, DEFAULT_REGEXP, Pattern.compile( DEFAULT_REGEXP ) );
129 }
130
131
132
133
134
135
136
137
138
139 private TelephoneNumberSyntaxChecker( String oid, String defaultRegexp, Pattern defaultPattern )
140 {
141 super( oid );
142
143 this.defaultPattern = defaultPattern;
144 this.defaultRegexp = defaultRegexp;
145 }
146
147
148
149
150
151 public static Builder builder()
152 {
153 return new Builder();
154 }
155
156
157
158
159
160
161
162 public String getRegexp()
163 {
164 return defaultRegexp;
165 }
166
167
168
169
170
171 @Override
172 public boolean isValidSyntax( Object value )
173 {
174 String strValue;
175
176 if ( value == null )
177 {
178 if ( LOG.isDebugEnabled() )
179 {
180 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
181 }
182
183 return false;
184 }
185
186 if ( value instanceof String )
187 {
188 strValue = ( String ) value;
189 }
190 else if ( value instanceof byte[] )
191 {
192 strValue = Strings.utf8ToString( ( byte[] ) value );
193 }
194 else
195 {
196 strValue = value.toString();
197 }
198
199 if ( strValue.length() == 0 )
200 {
201 if ( LOG.isDebugEnabled() )
202 {
203 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
204 }
205
206 return false;
207 }
208
209
210 boolean result;
211
212
213 synchronized ( defaultPattern )
214 {
215 result = defaultPattern.matcher( strValue ).matches();
216 }
217
218 if ( LOG.isDebugEnabled() )
219 {
220 if ( result )
221 {
222 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
223 }
224 else
225 {
226 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
227 }
228 }
229
230 return result;
231 }
232 }