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.Chars;
28 import org.apache.directory.api.util.Strings;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 @SuppressWarnings("serial")
53 public final class OidSyntaxChecker extends SyntaxChecker
54 {
55
56
57
58 public static final OidSyntaxChecker INSTANCE = new OidSyntaxChecker( SchemaConstants.OID_SYNTAX );
59
60
61
62
63 public static final class Builder extends SCBuilder<OidSyntaxChecker>
64 {
65
66
67
68 private Builder()
69 {
70 super( SchemaConstants.OID_SYNTAX );
71 }
72
73
74
75
76
77
78 @Override
79 public OidSyntaxChecker build()
80 {
81 return new OidSyntaxChecker( oid );
82 }
83 }
84
85
86
87
88
89
90
91 private OidSyntaxChecker( String oid )
92 {
93 super( oid );
94 }
95
96
97
98
99
100 public static Builder builder()
101 {
102 return new Builder();
103 }
104
105
106
107
108
109 @Override
110 public boolean isValidSyntax( Object value )
111 {
112 String strValue;
113
114 if ( value == null )
115 {
116 if ( LOG.isDebugEnabled() )
117 {
118 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
119 }
120
121 return false;
122 }
123
124 if ( value instanceof String )
125 {
126 strValue = ( String ) value;
127 }
128 else if ( value instanceof byte[] )
129 {
130 strValue = Strings.utf8ToString( ( byte[] ) value );
131 }
132 else
133 {
134 strValue = value.toString();
135 }
136
137 if ( strValue.length() == 0 )
138 {
139 if ( LOG.isDebugEnabled() )
140 {
141 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
142 }
143
144 return false;
145 }
146
147
148
149 if ( Chars.isDigit( strValue.charAt( 0 ) ) )
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
168
169
170 if ( Chars.isAlphaASCII( strValue, 0 ) )
171 {
172 for ( int index = 0; index < strValue.length(); index++ )
173 {
174 char c = strValue.charAt( index );
175
176 if ( !Chars.isAlphaDigitMinus( c ) )
177 {
178 if ( LOG.isDebugEnabled() )
179 {
180 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
181 }
182
183 return false;
184 }
185 }
186
187 if ( LOG.isDebugEnabled() )
188 {
189 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
190 }
191
192 return true;
193 }
194 else
195 {
196 if ( LOG.isDebugEnabled() )
197 {
198 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
199 }
200
201 return false;
202 }
203 }
204 }