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.Strings;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35
36
37
38
39
40
41 @SuppressWarnings("serial")
42 public class NumberSyntaxChecker extends SyntaxChecker
43 {
44
45 private static final Logger LOG = LoggerFactory.getLogger( NumberSyntaxChecker.class );
46
47
48
49
50
51 public NumberSyntaxChecker()
52 {
53 super( SchemaConstants.NUMBER_SYNTAX );
54 }
55
56
57
58
59
60 public boolean isValidSyntax( Object value )
61 {
62 String strValue = null;
63
64 if ( value == null )
65 {
66 LOG.debug( "Syntax invalid for 'null'" );
67 return false;
68 }
69
70 if ( value instanceof String )
71 {
72 strValue = ( String ) value;
73 }
74 else if ( value instanceof byte[] )
75 {
76 strValue = Strings.utf8ToString( ( byte[] ) value );
77 }
78 else
79 {
80 strValue = value.toString();
81 }
82
83
84 if ( strValue.length() == 0 )
85 {
86 LOG.debug( "Syntax invalid for '{}'", value );
87 return false;
88 }
89
90
91 for ( int i = 0; i < strValue.length(); i++ )
92 {
93 switch ( strValue.charAt( i ) )
94 {
95 case '0':
96 case '1':
97 case '2':
98 case '3':
99 case '4':
100 case '5':
101 case '6':
102 case '7':
103 case '8':
104 case '9':
105 continue;
106
107 default:
108 LOG.debug( "Syntax invalid for '{}'", value );
109 return false;
110 }
111 }
112
113 if ( ( strValue.charAt( 0 ) == '0' ) && strValue.length() > 1 )
114 {
115
116
117 LOG.debug( "Syntax invalid for '{}'", value );
118 return false;
119 }
120
121 LOG.debug( "Syntax valid for '{}'", value );
122 return true;
123 }
124 }