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