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