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