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.filter;
21
22
23 import java.util.Arrays;
24
25 import org.apache.directory.api.i18n.I18n;
26 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27 import org.apache.directory.api.ldap.model.entry.Value;
28 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
29 import org.apache.directory.api.ldap.model.schema.AttributeType;
30 import org.apache.directory.api.util.Strings;
31
32
33
34
35
36
37
38
39
40 public abstract class SimpleNode<T> extends LeafNode
41 {
42
43 protected Value value;
44
45
46 protected byte[] bytes;
47
48
49 public static final boolean EVAL_GREATER = true;
50
51
52 public static final boolean EVAL_LESSER = false;
53
54
55
56
57
58
59
60
61
62 protected SimpleNode( String attribute, byte[] bytes, AssertionType assertionType )
63 {
64 super( attribute, assertionType );
65 this.bytes = bytes;
66 }
67
68
69
70
71
72
73
74
75
76 protected SimpleNode( AttributeType attributeType, byte[] bytes, AssertionType assertionType )
77 {
78 super( attributeType, assertionType );
79 this.bytes = bytes;
80
81 try
82 {
83 if ( attributeType.isHR() )
84 {
85 value = new Value( attributeType, Strings.utf8ToString( bytes ) );
86 }
87 else
88 {
89 value = new Value( attributeType, bytes );
90 }
91 }
92 catch ( LdapInvalidAttributeValueException e )
93 {
94 throw new RuntimeException( e.getMessage() );
95 }
96 }
97
98
99
100
101
102
103
104
105
106 protected SimpleNode( String attribute, String string, AssertionType assertionType )
107 {
108 super( attribute, assertionType );
109 bytes = Strings.getBytesUtf8( string );
110
111 if ( attributeType != null )
112 {
113 try
114 {
115 if ( attributeType.isHR() )
116 {
117 value = new Value( attributeType, string );
118 }
119 else
120 {
121 value = new Value( attributeType, bytes );
122 }
123 }
124 catch ( LdapInvalidAttributeValueException e )
125 {
126 throw new RuntimeException( e.getMessage() );
127 }
128 }
129 }
130
131
132
133
134
135
136
137
138
139 protected SimpleNode( AttributeType attributeType, Value value, AssertionType assertionType )
140 {
141 super( attributeType, assertionType );
142 this.value = value;
143 }
144
145
146
147
148
149 @SuppressWarnings("unchecked")
150 @Override
151 public ExprNode clone()
152 {
153 ExprNode clone = super.clone();
154
155
156 if ( value != null )
157 {
158 ( ( SimpleNode<T> ) clone ).value = value.clone();
159 }
160 else
161 {
162
163 if ( bytes != null )
164 {
165 ( ( SimpleNode<T> ) clone ).bytes = new byte[bytes.length];
166 System.arraycopy( bytes, 0, ( ( SimpleNode<T> ) clone ).bytes, 0, bytes.length );
167 }
168 }
169
170 return clone;
171 }
172
173
174
175
176
177
178
179 public final Value getValue()
180 {
181 if ( value == null )
182 {
183 return new Value( bytes );
184 }
185 else
186 {
187 return value;
188 }
189 }
190
191
192
193
194
195 public String getEscapedValue()
196 {
197 if ( value != null )
198 {
199 return escapeFilterValue( value.getAttributeType(), value.getBytes() );
200 }
201 else
202 {
203 return escapeFilterValue( null, bytes );
204 }
205 }
206
207
208
209
210
211
212
213 public void setValue( Value value )
214 {
215 this.value = value;
216 this.bytes = value.getBytes();
217 }
218
219
220
221
222
223
224
225
226 public StringBuilder printToBuffer( StringBuilder buf )
227 {
228 if ( ( null != getAnnotations() ) && getAnnotations().containsKey( "count" ) )
229 {
230 buf.append( ":[" );
231 buf.append( getAnnotations().get( "count" ).toString() );
232 buf.append( "] " );
233 }
234
235 buf.append( ')' );
236
237 return buf;
238 }
239
240
241
242
243
244
245
246 @Override
247 public StringBuilder printRefinementToBuffer( StringBuilder buf )
248 {
249 if ( isSchemaAware() )
250 {
251 if ( !attributeType.getOid().equals( SchemaConstants.OBJECT_CLASS_AT_OID ) )
252 {
253 throw new UnsupportedOperationException( I18n.err( I18n.ERR_13318_INVALID_ATTR_IN_REFINEMENT, attribute ) );
254 }
255 }
256 else
257 {
258 if ( ( attribute == null )
259 || !( SchemaConstants.OBJECT_CLASS_AT.equalsIgnoreCase( attribute )
260 || SchemaConstants.OBJECT_CLASS_AT_OID.equalsIgnoreCase( attribute ) ) )
261 {
262 throw new UnsupportedOperationException( I18n.err( I18n.ERR_13318_INVALID_ATTR_IN_REFINEMENT, attribute ) );
263 }
264 }
265
266 buf.append( "item: " ).append( value );
267
268 return buf;
269 }
270
271
272
273
274
275
276 @Override
277 public int hashCode()
278 {
279 int h = 37;
280
281 h = h * 17 + super.hashCode();
282 h = h * 17 + ( value == null ? 0 : value.hashCode() );
283
284 return h;
285 }
286
287
288
289
290
291 @Override
292 public boolean equals( Object other )
293 {
294 if ( this == other )
295 {
296 return true;
297 }
298
299 if ( !( other instanceof SimpleNode<?> ) )
300 {
301 return false;
302 }
303
304 if ( other.getClass() != this.getClass() )
305 {
306 return false;
307 }
308
309 if ( !super.equals( other ) )
310 {
311 return false;
312 }
313
314 SimpleNode<?> otherNode = ( SimpleNode<?> ) other;
315
316 if ( value == null )
317 {
318 if ( bytes == null )
319 {
320 return otherNode.bytes == null;
321 }
322 else
323 {
324 return Arrays.equals( bytes, otherNode.bytes );
325 }
326 }
327 else
328 {
329 return value.equals( otherNode.value );
330 }
331 }
332 }