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.Strings;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @SuppressWarnings("serial")
42 public final class NumberSyntaxChecker extends SyntaxChecker
43 {
44
45
46
47 public static final NumberSyntaxChecker INSTANCE = new NumberSyntaxChecker( SchemaConstants.NUMBER_SYNTAX );
48
49
50
51
52 public static final class Builder extends SCBuilder<NumberSyntaxChecker>
53 {
54
55
56
57 private Builder()
58 {
59 super( SchemaConstants.NUMBER_SYNTAX );
60 }
61
62
63
64
65
66
67 @Override
68 public NumberSyntaxChecker build()
69 {
70 return new NumberSyntaxChecker( oid );
71 }
72 }
73
74
75
76
77
78
79
80 private NumberSyntaxChecker( String oid )
81 {
82 super( oid );
83 }
84
85
86
87
88
89 public static Builder builder()
90 {
91 return new Builder();
92 }
93
94
95
96
97
98 @Override
99 public boolean isValidSyntax( Object value )
100 {
101 String strValue;
102
103 if ( value == null )
104 {
105 if ( LOG.isDebugEnabled() )
106 {
107 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
108 }
109
110 return false;
111 }
112
113 if ( value instanceof String )
114 {
115 strValue = ( String ) value;
116 }
117 else if ( value instanceof byte[] )
118 {
119 strValue = Strings.utf8ToString( ( byte[] ) value );
120 }
121 else
122 {
123 strValue = value.toString();
124 }
125
126
127 if ( strValue.length() == 0 )
128 {
129 if ( LOG.isDebugEnabled() )
130 {
131 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
132 }
133
134 return false;
135 }
136
137
138 for ( int i = 0; i < strValue.length(); i++ )
139 {
140 switch ( strValue.charAt( i ) )
141 {
142 case '0':
143 case '1':
144 case '2':
145 case '3':
146 case '4':
147 case '5':
148 case '6':
149 case '7':
150 case '8':
151 case '9':
152 continue;
153
154 default:
155 if ( LOG.isDebugEnabled() )
156 {
157 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
158 }
159
160 return false;
161 }
162 }
163
164 if ( ( strValue.charAt( 0 ) == '0' ) && strValue.length() > 1 )
165 {
166
167
168 if ( LOG.isDebugEnabled() )
169 {
170 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
171 }
172
173 return false;
174 }
175
176 if ( LOG.isDebugEnabled() )
177 {
178 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
179 }
180
181 return true;
182 }
183 }