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
41
42
43 @SuppressWarnings("serial")
44 public final class TeletexTerminalIdentifierSyntaxChecker extends SyntaxChecker
45 {
46
47
48
49 public static final TeletexTerminalIdentifierSyntaxChecker INSTANCE =
50 new TeletexTerminalIdentifierSyntaxChecker( SchemaConstants.TELETEX_TERMINAL_IDENTIFIER_SYNTAX );
51
52
53
54
55 public static final class Builder extends SCBuilder<TeletexTerminalIdentifierSyntaxChecker>
56 {
57
58
59
60 private Builder()
61 {
62 super( SchemaConstants.TELETEX_TERMINAL_IDENTIFIER_SYNTAX );
63 }
64
65
66
67
68
69
70 @Override
71 public TeletexTerminalIdentifierSyntaxChecker build()
72 {
73 return new TeletexTerminalIdentifierSyntaxChecker( oid );
74 }
75 }
76
77
78
79
80
81
82
83 private TeletexTerminalIdentifierSyntaxChecker( String oid )
84 {
85 super( oid );
86 }
87
88
89
90
91
92 public static Builder builder()
93 {
94 return new Builder();
95 }
96
97
98
99
100
101 @Override
102 public boolean isValidSyntax( Object value )
103 {
104 String strValue;
105
106 if ( value == null )
107 {
108 if ( LOG.isDebugEnabled() )
109 {
110 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
111 }
112
113 return false;
114 }
115
116 if ( value instanceof String )
117 {
118 strValue = ( String ) value;
119 }
120 else if ( value instanceof byte[] )
121 {
122 strValue = Strings.utf8ToString( ( byte[] ) value );
123 }
124 else
125 {
126 strValue = value.toString();
127 }
128
129 if ( strValue.length() == 0 )
130 {
131 if ( LOG.isDebugEnabled() )
132 {
133 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
134 }
135
136 return false;
137 }
138
139
140 int dollar = strValue.indexOf( '$' );
141
142 String terminalIdentifier = ( dollar == -1 ) ? strValue : strValue.substring( 0, dollar );
143
144 if ( terminalIdentifier.length() == 0 )
145 {
146
147 if ( LOG.isDebugEnabled() )
148 {
149 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
150 }
151
152 return false;
153 }
154
155 if ( !Strings.isPrintableString( terminalIdentifier ) )
156 {
157
158 if ( LOG.isDebugEnabled() )
159 {
160 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
161 }
162
163 return false;
164 }
165
166 if ( dollar == -1 )
167 {
168
169 if ( LOG.isDebugEnabled() )
170 {
171 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
172 }
173
174 return true;
175 }
176
177
178 String[] ttxParams = strValue.substring( dollar + 1 ).split( "\\$" );
179
180 if ( ttxParams.length == 0 )
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 for ( String ttxParam : ttxParams )
191 {
192 int colon = ttxParam.indexOf( ':' );
193
194 if ( colon == -1 )
195 {
196
197 if ( LOG.isDebugEnabled() )
198 {
199 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
200 }
201
202 return false;
203 }
204
205 String key = ttxParam.substring( 0, colon );
206
207 if ( key.startsWith( "graphic" )
208 || key.startsWith( "control" )
209 || key.startsWith( "misc" )
210 || key.startsWith( "page" )
211 || key.startsWith( "private" ) )
212 {
213 if ( colon + 1 == ttxParam.length() )
214 {
215 if ( LOG.isDebugEnabled() )
216 {
217 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
218 }
219
220 return false;
221 }
222
223 boolean hasEsc = false;
224
225 for ( byte b : Strings.getBytesUtf8( ttxParam ) )
226 {
227 switch ( b )
228 {
229 case 0x24:
230
231 if ( LOG.isDebugEnabled() )
232 {
233 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
234 }
235
236 return false;
237
238 case 0x5c:
239 if ( hasEsc )
240 {
241
242 if ( LOG.isDebugEnabled() )
243 {
244 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
245 }
246
247 return false;
248 }
249 else
250 {
251 hasEsc = true;
252 }
253
254 continue;
255
256 case '2':
257 continue;
258
259 case '4':
260
261 hasEsc = false;
262 continue;
263
264 case '5':
265 continue;
266
267 case 'c':
268 case 'C':
269
270 hasEsc = false;
271 continue;
272
273 default:
274 if ( hasEsc )
275 {
276
277 return false;
278 }
279
280 continue;
281 }
282 }
283 }
284 else
285 {
286 if ( LOG.isDebugEnabled() )
287 {
288 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
289 }
290
291 return false;
292 }
293 }
294
295 if ( LOG.isDebugEnabled() )
296 {
297 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
298 }
299
300 return true;
301 }
302 }