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