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