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.Chars;
26 import org.apache.directory.api.util.Strings;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 @SuppressWarnings("serial")
44 public class BitStringSyntaxChecker extends SyntaxChecker
45 {
46
47 private static final Logger LOG = LoggerFactory.getLogger( BitStringSyntaxChecker.class );
48
49
50
51
52
53
54
55 public BitStringSyntaxChecker()
56 {
57 super( SchemaConstants.BIT_STRING_SYNTAX );
58 }
59
60
61
62
63
64
65
66
67
68
69
70 public static boolean isValid( String strValue )
71 {
72 if ( strValue.length() == 0 )
73 {
74 LOG.debug( "Syntax invalid for '{}'", strValue );
75 return false;
76 }
77
78 int pos = 0;
79
80
81 if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
82 {
83 LOG.debug( "Syntax invalid for '{}'", strValue );
84 return false;
85 }
86
87
88 if ( !Chars.isBit( strValue, pos++ ) )
89 {
90 LOG.debug( "Syntax invalid for '{}'", strValue );
91 return false;
92 }
93
94 while ( Chars.isBit( strValue, pos ) )
95 {
96
97 pos++;
98 }
99
100
101 if ( !Strings.isCharASCII( strValue, pos++, '\'' ) )
102 {
103 LOG.debug( "Syntax invalid for '{}'", strValue );
104 return false;
105 }
106
107
108 if ( !Strings.isCharASCII( strValue, pos, 'B' ) )
109 {
110 LOG.debug( "Syntax invalid for '{}'", strValue );
111 return false;
112 }
113
114 LOG.debug( "Syntax valid for '{}'", strValue );
115 return true;
116 }
117
118
119
120
121
122 public boolean isValidSyntax( Object value )
123 {
124 String strValue = null;
125
126 if ( value == null )
127 {
128 LOG.debug( "Syntax invalid for 'null'" );
129 return false;
130 }
131
132 if ( value instanceof String )
133 {
134 strValue = ( String ) value;
135 }
136 else if ( value instanceof byte[] )
137 {
138 strValue = Strings.utf8ToString( ( byte[] ) value );
139 }
140 else
141 {
142 strValue = value.toString();
143 }
144
145 return isValid( strValue );
146 }
147 }