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