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.ldap.model.constants.SchemaConstants;
24 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25 import org.apache.directory.api.util.Chars;
26 import org.apache.directory.api.util.Strings;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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 class JavaIntegerSyntaxChecker extends SyntaxChecker
51 {
52
53 private static final Logger LOG = LoggerFactory.getLogger( JavaIntegerSyntaxChecker.class );
54
55
56
57
58
59 public JavaIntegerSyntaxChecker()
60 {
61 super( SchemaConstants.JAVA_INT_SYNTAX );
62 }
63
64
65
66
67
68 public boolean isValidSyntax( Object value )
69 {
70 String strValue = null;
71
72 if ( value == null )
73 {
74 LOG.debug( "Syntax invalid for 'null'" );
75 return false;
76 }
77
78 if ( value instanceof String )
79 {
80 strValue = ( String ) value;
81 }
82 else if ( value instanceof byte[] )
83 {
84 strValue = Strings.utf8ToString( ( byte[] ) value );
85 }
86 else
87 {
88 strValue = value.toString();
89 }
90
91 if ( strValue.length() == 0 )
92 {
93 LOG.debug( "Syntax invalid for '{}'", value );
94 return false;
95 }
96
97
98
99 int pos = 0;
100 char c = strValue.charAt( pos );
101
102 if ( c == '-' )
103 {
104 pos = 1;
105 }
106 else if ( !Chars.isDigit( c ) )
107 {
108 LOG.debug( "Syntax invalid for '{}'", value );
109 return false;
110 }
111 else if ( c == '0' )
112 {
113 boolean result = strValue.length() <= 1;
114
115 if ( result )
116 {
117 LOG.debug( "Syntax valid for '{}'", value );
118 }
119 else
120 {
121 LOG.debug( "Syntax invalid for '{}'", value );
122 }
123
124 return result;
125 }
126
127
128 if ( !Chars.isDigit( strValue, pos ) )
129 {
130 LOG.debug( "Syntax invalid for '{}'", value );
131 return false;
132 }
133 else if ( Strings.isCharASCII( strValue, pos, '0' ) )
134 {
135 LOG.debug( "Syntax invalid for '{}'", value );
136 return false;
137 }
138 else
139 {
140 pos++;
141 }
142
143 while ( Chars.isDigit( strValue, pos ) )
144 {
145 pos++;
146 }
147
148 if ( pos != strValue.length() )
149 {
150 LOG.debug( "Syntax invalid for '{}'", value );
151 return false;
152 }
153
154 try
155 {
156 Integer.valueOf( strValue );
157 LOG.debug( "Syntax valid for '{}'", value );
158 return true;
159 }
160 catch ( NumberFormatException e )
161 {
162 LOG.debug( "Syntax invalid for '{}'", value );
163 return false;
164 }
165 }
166 }