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
62
63 public static final DseTypeSyntaxChecker INSTANCE = new DseTypeSyntaxChecker( SchemaConstants.DSE_TYPE_SYNTAX );
64
65
66
67
68 public static final class Builder extends SCBuilder<DseTypeSyntaxChecker>
69 {
70
71
72
73 private Builder()
74 {
75 super( SchemaConstants.DSE_TYPE_SYNTAX );
76 }
77
78
79
80
81
82
83 @Override
84 public DseTypeSyntaxChecker build()
85 {
86 return new DseTypeSyntaxChecker( oid );
87 }
88 }
89
90
91
92 static
93 {
94 for ( String country : DSE_BITS_STRINGS )
95 {
96 DSE_BITS.add( country );
97 }
98 }
99
100
101
102
103
104
105
106 private DseTypeSyntaxChecker( String oid )
107 {
108 super( oid );
109 }
110
111
112
113
114
115 public static Builder builder()
116 {
117 return new Builder();
118 }
119
120
121
122
123
124 @Override
125 public boolean isValidSyntax( Object value )
126 {
127 String strValue;
128
129 if ( value == null )
130 {
131 if ( LOG.isDebugEnabled() )
132 {
133 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
134 }
135
136 return false;
137 }
138
139 if ( value instanceof String )
140 {
141 strValue = ( String ) value;
142 }
143 else if ( value instanceof byte[] )
144 {
145 strValue = Strings.utf8ToString( ( byte[] ) value );
146 }
147 else
148 {
149 strValue = value.toString();
150 }
151
152
153 if ( strValue.length() < 4 )
154 {
155 if ( LOG.isDebugEnabled() )
156 {
157 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
158 }
159
160 return false;
161 }
162
163
164 if ( ( strValue.charAt( 0 ) != '(' )
165 || ( strValue.charAt( strValue.length() - 1 ) != ')' ) )
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 Set<String> keywords = new HashSet<>();
176 int len = strValue.length() - 1;
177 boolean needKeyword = true;
178
179
180 for ( int i = 1; i < len; )
181 {
182
183 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
184 {
185 i++;
186 }
187
188 int pos = i;
189
190
191 while ( ( i < len ) && Chars.isAlphaASCII( strValue, pos ) )
192 {
193 pos++;
194 }
195
196 if ( pos == i )
197 {
198
199 if ( LOG.isDebugEnabled() )
200 {
201 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
202 }
203
204 return false;
205 }
206
207 String keyword = strValue.substring( i, pos );
208 i = pos;
209
210 if ( !DSE_BITS.contains( keyword ) )
211 {
212
213 if ( LOG.isDebugEnabled() )
214 {
215 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
216 }
217
218 return false;
219 }
220
221
222 if ( keywords.contains( keyword ) )
223 {
224 if ( LOG.isDebugEnabled() )
225 {
226 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
227 }
228
229 return false;
230 }
231
232 keywords.add( keyword );
233 needKeyword = false;
234
235
236 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
237 {
238 i++;
239 }
240
241
242 if ( ( i < len ) && ( strValue.charAt( i ) == '$' ) )
243 {
244
245 i++;
246 needKeyword = true;
247 continue;
248 }
249 }
250
251
252 if ( LOG.isDebugEnabled() )
253 {
254 if ( needKeyword )
255 {
256 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
257 }
258 else
259 {
260 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
261 }
262 }
263
264 return !needKeyword;
265 }
266 }