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.Chars;
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 @SuppressWarnings("serial")
49 public final class JavaShortSyntaxChecker extends SyntaxChecker
50 {
51
52
53
54 public static final JavaShortSyntaxChecker INSTANCE = new JavaShortSyntaxChecker( SchemaConstants.JAVA_SHORT_SYNTAX );
55
56
57
58
59 public static final class Builder extends SCBuilder<JavaShortSyntaxChecker>
60 {
61
62
63
64 private Builder()
65 {
66 super( SchemaConstants.JAVA_SHORT_SYNTAX );
67 }
68
69
70
71
72
73
74 @Override
75 public JavaShortSyntaxChecker build()
76 {
77 return new JavaShortSyntaxChecker( oid );
78 }
79 }
80
81
82
83
84
85
86
87 private JavaShortSyntaxChecker( String oid )
88 {
89 super( oid );
90 }
91
92
93
94
95
96 public static Builder builder()
97 {
98 return new Builder();
99 }
100
101
102
103
104
105 @Override
106 public boolean isValidSyntax( Object value )
107 {
108 String strValue;
109
110 if ( value == null )
111 {
112 if ( LOG.isDebugEnabled() )
113 {
114 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
115 }
116
117 return false;
118 }
119
120 if ( value instanceof String )
121 {
122 strValue = ( String ) value;
123 }
124 else if ( value instanceof byte[] )
125 {
126 strValue = Strings.utf8ToString( ( byte[] ) value );
127 }
128 else
129 {
130 strValue = value.toString();
131 }
132
133 if ( strValue.length() == 0 )
134 {
135 if ( LOG.isDebugEnabled() )
136 {
137 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
138 }
139
140 return false;
141 }
142
143
144
145 int pos = 0;
146 char c = strValue.charAt( pos );
147
148 if ( c == '-' )
149 {
150 pos = 1;
151 }
152 else if ( !Chars.isDigit( c ) )
153 {
154 if ( LOG.isDebugEnabled() )
155 {
156 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
157 }
158
159 return false;
160 }
161 else if ( c == '0' )
162 {
163 boolean result = strValue.length() <= 1;
164
165 if ( LOG.isDebugEnabled() )
166 {
167 if ( result )
168 {
169 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
170 return false;
171 }
172 else
173 {
174 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
175 return true;
176 }
177 }
178
179 return result;
180 }
181
182
183 if ( !Chars.isDigit( strValue, pos ) || Strings.isCharASCII( strValue, pos, '0' ) )
184 {
185 if ( LOG.isDebugEnabled() )
186 {
187 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
188 }
189
190 return false;
191 }
192 else
193 {
194 pos++;
195 }
196
197 while ( Chars.isDigit( strValue, pos ) )
198 {
199 pos++;
200 }
201
202 if ( pos != strValue.length() )
203 {
204 if ( LOG.isDebugEnabled() )
205 {
206 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
207 }
208
209 return false;
210 }
211
212
213 try
214 {
215 Short.valueOf( strValue );
216
217 if ( LOG.isDebugEnabled() )
218 {
219 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
220 }
221
222 return true;
223 }
224 catch ( NumberFormatException e )
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 }