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.Chars;
27 import org.apache.directory.api.util.Strings;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 @SuppressWarnings("serial")
43 public final class BitStringSyntaxChecker extends SyntaxChecker
44 {
45
46
47
48 public static final BitStringSyntaxChecker INSTANCE = new BitStringSyntaxChecker( SchemaConstants.BIT_STRING_SYNTAX );
49
50
51
52
53 public static final class Builder extends SCBuilder<BitStringSyntaxChecker>
54 {
55
56
57
58 private Builder()
59 {
60 super( SchemaConstants.BIT_STRING_SYNTAX );
61 }
62
63
64
65
66
67
68 @Override
69 public BitStringSyntaxChecker build()
70 {
71 return new BitStringSyntaxChecker( oid );
72 }
73 }
74
75
76
77
78
79
80
81 private BitStringSyntaxChecker( String oid )
82 {
83 super( oid );
84 }
85
86
87
88
89
90 public static Builder builder()
91 {
92 return new Builder();
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public static boolean isValid( String strValue )
106 {
107 if ( strValue.length() == 0 )
108 {
109 if ( LOG.isDebugEnabled() )
110 {
111 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
112 }
113
114 return false;
115 }
116
117 int pos = 0;
118
119
120 if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
121 {
122 if ( LOG.isDebugEnabled() )
123 {
124 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
125 }
126
127 return false;
128 }
129
130
131 if ( !Chars.isBit( strValue, pos++ ) )
132 {
133 if ( LOG.isDebugEnabled() )
134 {
135 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
136 }
137
138 return false;
139 }
140
141 while ( Chars.isBit( strValue, pos ) )
142 {
143
144 pos++;
145 }
146
147
148 if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
149 {
150 if ( LOG.isDebugEnabled() )
151 {
152 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
153 }
154
155 return false;
156 }
157
158
159 if ( !Strings.isCharASCII( strValue, pos, 'B' ) )
160 {
161 if ( LOG.isDebugEnabled() )
162 {
163 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, strValue ) );
164 }
165
166 return false;
167 }
168
169 if ( LOG.isDebugEnabled() )
170 {
171 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, strValue ) );
172 }
173
174 return true;
175 }
176
177
178
179
180
181 @Override
182 public boolean isValidSyntax( Object value )
183 {
184 String strValue;
185
186 if ( value == null )
187 {
188 if ( LOG.isDebugEnabled() )
189 {
190 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
191 }
192
193 return false;
194 }
195
196 if ( value instanceof String )
197 {
198 strValue = ( String ) value;
199 }
200 else if ( value instanceof byte[] )
201 {
202 strValue = Strings.utf8ToString( ( byte[] ) value );
203 }
204 else
205 {
206 strValue = value.toString();
207 }
208
209 return isValid( strValue );
210 }
211 }