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