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