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
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.Chars;
30 import org.apache.directory.api.util.Strings;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 @SuppressWarnings("serial")
47 public final class DseTypeSyntaxChecker extends SyntaxChecker
48 {
49
50 private static final String[] DSE_BITS_STRINGS =
51 {
52 "root", "glue", "cp", "entry", "alias", "subr",
53 "nssr", "supr", "xr", "admPoint", "subentry",
54 "shadow", "zombie", "immSupr", "rhob", "sa"
55 };
56
57
58 private static final Set<String> DSE_BITS = new HashSet<>();
59
60
61 static
62 {
63 for ( String country : DSE_BITS_STRINGS )
64 {
65 DSE_BITS.add( country );
66 }
67 }
68
69
70
71
72 public static final DseTypeSyntaxChecker INSTANCE = new DseTypeSyntaxChecker( SchemaConstants.DSE_TYPE_SYNTAX );
73
74
75
76
77 public static final class Builder extends SCBuilder<DseTypeSyntaxChecker>
78 {
79
80
81
82 private Builder()
83 {
84 super( SchemaConstants.DSE_TYPE_SYNTAX );
85 }
86
87
88
89
90
91
92 @Override
93 public DseTypeSyntaxChecker build()
94 {
95 return new DseTypeSyntaxChecker( oid );
96 }
97 }
98
99
100
101 static
102 {
103 for ( String country : DSE_BITS_STRINGS )
104 {
105 DSE_BITS.add( country );
106 }
107 }
108
109
110
111
112
113
114
115 private DseTypeSyntaxChecker( String oid )
116 {
117 super( oid );
118 }
119
120
121
122
123
124 public static Builder builder()
125 {
126 return new Builder();
127 }
128
129
130
131
132
133 @Override
134 public boolean isValidSyntax( Object value )
135 {
136 String strValue;
137
138 if ( value == null )
139 {
140 if ( LOG.isDebugEnabled() )
141 {
142 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
143 }
144
145 return false;
146 }
147
148 if ( value instanceof String )
149 {
150 strValue = ( String ) value;
151 }
152 else if ( value instanceof byte[] )
153 {
154 strValue = Strings.utf8ToString( ( byte[] ) value );
155 }
156 else
157 {
158 strValue = value.toString();
159 }
160
161
162 if ( strValue.length() < 4 )
163 {
164 if ( LOG.isDebugEnabled() )
165 {
166 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
167 }
168
169 return false;
170 }
171
172
173 if ( ( strValue.charAt( 0 ) != '(' )
174 || ( strValue.charAt( strValue.length() - 1 ) != ')' ) )
175 {
176 if ( LOG.isDebugEnabled() )
177 {
178 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
179 }
180
181 return false;
182 }
183
184 Set<String> keywords = new HashSet<>();
185 int len = strValue.length() - 1;
186 boolean needKeyword = true;
187
188
189 for ( int i = 1; i < len; )
190 {
191
192 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
193 {
194 i++;
195 }
196
197 int pos = i;
198
199
200 while ( ( i < len ) && Chars.isAlphaASCII( strValue, pos ) )
201 {
202 pos++;
203 }
204
205 if ( pos == i )
206 {
207
208 if ( LOG.isDebugEnabled() )
209 {
210 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
211 }
212
213 return false;
214 }
215
216 String keyword = strValue.substring( i, pos );
217 i = pos;
218
219 if ( !DSE_BITS.contains( keyword ) )
220 {
221
222 if ( LOG.isDebugEnabled() )
223 {
224 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
225 }
226
227 return false;
228 }
229
230
231 if ( keywords.contains( keyword ) )
232 {
233 if ( LOG.isDebugEnabled() )
234 {
235 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
236 }
237
238 return false;
239 }
240
241 keywords.add( keyword );
242 needKeyword = false;
243
244
245 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
246 {
247 i++;
248 }
249
250
251 if ( ( i < len ) && ( strValue.charAt( i ) == '$' ) )
252 {
253
254 i++;
255 needKeyword = true;
256 }
257 }
258
259
260 if ( LOG.isDebugEnabled() )
261 {
262 if ( needKeyword )
263 {
264 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
265 }
266 else
267 {
268 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
269 }
270 }
271
272 return !needKeyword;
273 }
274 }