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