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 @SuppressWarnings("serial")
42 public final class NisNetgroupTripleSyntaxChecker extends SyntaxChecker
43 {
44
45
46
47 public static final NisNetgroupTripleSyntaxChecker INSTANCE =
48 new NisNetgroupTripleSyntaxChecker( SchemaConstants.NIS_NETGROUP_TRIPLE_SYNTAX );
49
50
51
52
53 public static final class Builder extends SCBuilder<NisNetgroupTripleSyntaxChecker>
54 {
55
56
57
58 private Builder()
59 {
60 super( SchemaConstants.NIS_NETGROUP_TRIPLE_SYNTAX );
61 }
62
63
64
65
66
67
68 @Override
69 public NisNetgroupTripleSyntaxChecker build()
70 {
71 return new NisNetgroupTripleSyntaxChecker( oid );
72 }
73 }
74
75
76
77
78
79
80
81 private NisNetgroupTripleSyntaxChecker( String oid )
82 {
83 super( oid );
84 }
85
86
87
88
89
90 public static Builder builder()
91 {
92 return new Builder();
93 }
94
95
96 private int parseKeyString( String strValue, int pos )
97 {
98 char c = strValue.charAt( pos );
99
100
101 if ( ( c == ',' ) || ( c == ')' ) )
102 {
103 return pos;
104 }
105
106
107 if ( Character.isUpperCase( c ) || Character.isLowerCase( c ) )
108 {
109 pos++;
110 }
111 else
112 {
113 return -1;
114 }
115
116 try
117 {
118 c = strValue.charAt( pos );
119
120 while ( ( c != ',' ) && ( c != ')' ) )
121 {
122 if ( Character.isUpperCase( c ) || Character.isLowerCase( c ) || Character.isDigit( c )
123 || ( c == '-' ) || ( c == ';' ) || ( c == '_' ) )
124 {
125 pos++;
126 }
127 else
128 {
129 return -1;
130 }
131
132 c = strValue.charAt( pos );
133 }
134
135 return pos;
136 }
137 catch ( IndexOutOfBoundsException ioobe )
138 {
139 return -1;
140 }
141 }
142
143
144
145
146
147 @Override
148 public boolean isValidSyntax( Object value )
149 {
150 String strValue;
151
152 if ( value == null )
153 {
154 if ( LOG.isDebugEnabled() )
155 {
156 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
157 }
158
159 return false;
160 }
161
162 if ( value instanceof String )
163 {
164 strValue = ( String ) value;
165 }
166 else if ( value instanceof byte[] )
167 {
168 strValue = Strings.utf8ToString( ( byte[] ) value );
169 }
170 else
171 {
172 strValue = value.toString();
173 }
174
175
176 if ( strValue.length() < 4 )
177 {
178 if ( LOG.isDebugEnabled() )
179 {
180 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
181 }
182
183 return false;
184 }
185
186
187 int pos = parseKeyString( strValue, 1 );
188
189 if ( pos == -1 )
190 {
191 if ( LOG.isDebugEnabled() )
192 {
193 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
194 }
195
196 return false;
197 }
198
199 if ( strValue.charAt( pos ) != ',' )
200 {
201 if ( LOG.isDebugEnabled() )
202 {
203 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
204 }
205
206 return false;
207 }
208 else
209 {
210 pos++;
211 }
212
213
214 pos = parseKeyString( strValue, pos );
215
216 if ( pos == -1 )
217 {
218 if ( LOG.isDebugEnabled() )
219 {
220 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
221 }
222
223 return false;
224 }
225
226 if ( strValue.charAt( pos ) != ',' )
227 {
228 if ( LOG.isDebugEnabled() )
229 {
230 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
231 }
232
233 return false;
234 }
235 else
236 {
237 pos++;
238 }
239
240
241 pos = parseKeyString( strValue, pos );
242
243 if ( pos == -1 )
244 {
245 if ( LOG.isDebugEnabled() )
246 {
247 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
248 }
249
250 return false;
251 }
252
253 if ( strValue.charAt( pos ) != ')' )
254 {
255 if ( LOG.isDebugEnabled() )
256 {
257 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
258 }
259
260 return false;
261 }
262
263 return true;
264 }
265 }