View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.filter;
21  
22  
23  import org.apache.directory.api.ldap.model.entry.Value;
24  import org.apache.directory.api.ldap.model.schema.AttributeType;
25  
26  
27  /**
28   * Filter expression tree node for extensible assertions.
29   * 
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   */
32  public class ExtensibleNode extends LeafNode
33  {
34      /** The value of the attribute to match for */
35      private Value value;
36      
37      /** The value as a byte[] */
38      protected byte[] bytes;
39  
40      /** The matching rules id */
41      private String matchingRuleId;
42  
43      /** The name of the dn attributes */
44      private boolean dnAttributes = false;
45  
46  
47      /**
48       * Creates a new emptyExtensibleNode object.
49       * 
50       * @param attributeType the attributeType associated with this node
51       */
52      public ExtensibleNode( AttributeType attributeType )
53      {
54          super( attributeType, AssertionType.EXTENSIBLE );
55  
56          dnAttributes = false;
57      }
58  
59  
60      /**
61       * Creates a new emptyExtensibleNode object.
62       * 
63       * @param attribute the attribute associated with this node
64       */
65      public ExtensibleNode( String attribute )
66      {
67          super( attribute, AssertionType.EXTENSIBLE );
68  
69          dnAttributes = false;
70      }
71  
72  
73      /**
74       * Creates a new ExtensibleNode object.
75       * 
76       * @param attributeType the attributeType used for the extensible assertion
77       * @param value the value to match for
78       * @param matchingRuleId the OID of the matching rule
79       * @param dnAttributes the dn attributes
80       */
81      public ExtensibleNode( AttributeType attributeType, Value value, String matchingRuleId, boolean dnAttributes )
82      {
83          super( attributeType, AssertionType.EXTENSIBLE );
84  
85          this.value = value;
86          this.matchingRuleId = matchingRuleId;
87          this.dnAttributes = dnAttributes;
88      }
89  
90  
91      /**
92       * Creates a new ExtensibleNode object.
93       * 
94       * @param attribute the attribute used for the extensible assertion
95       * @param value the value to match for
96       * @param matchingRuleId the OID of the matching rule
97       * @param dnAttributes the dn attributes
98       */
99      public ExtensibleNode( String attribute, Value value, String matchingRuleId, boolean dnAttributes )
100     {
101         super( attribute, AssertionType.EXTENSIBLE );
102 
103         this.value = value;
104         this.matchingRuleId = matchingRuleId;
105         this.dnAttributes = dnAttributes;
106     }
107 
108 
109     /**
110      * Makes a full clone in new memory space of the current node and children
111      * 
112      * @return the clone
113      */
114     @Override
115     public ExprNode clone()
116     {
117         ExprNode clone = super.clone();
118 
119         // Copy the value
120         if ( value != null )
121         {
122             ( ( ExtensibleNode ) clone ).value = value.clone();
123         }
124 
125         return clone;
126     }
127 
128 
129     /**
130      * Gets the Dn attributes.
131      * 
132      * @return the dn attributes
133      */
134     public boolean hasDnAttributes()
135     {
136         return dnAttributes;
137     }
138 
139 
140     /**
141      * Set the dnAttributes flag
142      *
143      * @param dnAttributes The flag to set
144      */
145     public void setDnAttributes( boolean dnAttributes )
146     {
147         this.dnAttributes = dnAttributes;
148     }
149 
150 
151     /**
152      * Gets the matching rule id as an OID string.
153      * 
154      * @return the OID
155      */
156     public String getMatchingRuleId()
157     {
158         return matchingRuleId;
159     }
160 
161 
162     /**
163      * Sets the matching rule id as an OID string.
164      * 
165      * @param matchingRuleId The maching rule ID
166      */
167     public void setMatchingRuleId( String matchingRuleId )
168     {
169         this.matchingRuleId = matchingRuleId;
170     }
171 
172 
173     /**
174      * Gets the value.
175      * 
176      * @return the value
177      */
178     public final Value getValue()
179     {
180         return value;
181     }
182 
183 
184     /** 
185      * @return representation of value, escaped for use in a filter if required 
186      */
187     public String getEscapedValue()
188     {
189         if ( value.isHumanReadable() )
190         {
191             return escapeFilterValue( value.getString() );
192         }
193         else
194         {
195             return escapeFilterValue( value.getAttributeType(), value.getBytes() );
196         }
197     }
198 
199 
200     /**
201      * Sets the value.
202      * 
203      * @param value the value
204      */
205     public final void setValue( Value value )
206     {
207         this.value = value;
208     }
209 
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     public boolean equals( Object obj )
216     {
217         if ( obj == this )
218         {
219             return true;
220         }
221 
222         if ( !( obj instanceof ExtensibleNode ) )
223         {
224             return false;
225         }
226         ExtensibleNode that = ( ExtensibleNode ) obj;
227 
228         if ( dnAttributes != that.dnAttributes )
229         {
230             return false;
231         }
232         if ( !matchingRuleId.equals( that.matchingRuleId ) )
233         {
234             return false;
235         }
236         if ( !value.equals( that.value ) )
237         {
238             return false;
239         }
240 
241         return super.equals( obj );
242     }
243 
244 
245     /**
246      * @see Object#hashCode()
247      * @return the instance's hash code 
248      */
249     @Override
250     public int hashCode()
251     {
252         int h = 37;
253 
254         h = h * 17 + super.hashCode();
255         h = h * 17 + ( dnAttributes ? 1 : 0 );
256         h = h * 17 + matchingRuleId.hashCode();
257         h = h * 17 + value.hashCode();
258 
259         return h;
260     }
261 
262 
263     /**
264      * @see java.lang.Object#toString()
265      * @return A string representing the AndNode
266      */
267     @Override
268     public String toString()
269     {
270         StringBuilder buf = new StringBuilder();
271 
272         buf.append( '(' );
273 
274         if ( attributeType != null )
275         {
276             buf.append( attributeType.getName() );
277         }
278         else
279         {
280             buf.append( attribute );
281         }
282 
283         buf.append( "-" );
284         buf.append( dnAttributes );
285         buf.append( "-EXTENSIBLE-" );
286         buf.append( matchingRuleId );
287         buf.append( "-" );
288         buf.append( value );
289 
290         buf.append( super.toString() );
291 
292         buf.append( ')' );
293 
294         return buf.toString();
295     }
296 }