001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.filter;
021    
022    
023    import java.util.Arrays;
024    
025    import org.apache.directory.shared.ldap.entry.Value;
026    import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
027    import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
028    import org.apache.directory.shared.ldap.util.StringTools;
029    
030    
031    /**
032     * Filter expression tree node for extensible assertions.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Revision: 798550 $
036     */
037    public class ExtensibleNode extends LeafNode
038    {
039        /** The value of the attribute to match for */
040        private Value<?> value;
041    
042        /** The matching rules id */
043        private String matchingRuleId;
044    
045        /** The name of the dn attributes */
046        private boolean dnAttributes = false;
047    
048    
049        /**
050         * Creates a new emptyExtensibleNode object.
051         * 
052         * @param attribute the attribute associated with this node
053         */
054        public ExtensibleNode( String attribute )
055        {
056            super( attribute, AssertionType.EXTENSIBLE );
057            
058            dnAttributes = false;
059        }
060    
061        /**
062         * Creates a new ExtensibleNode object.
063         * 
064         * @param attribute the attribute used for the extensible assertion
065         * @param value the value to match for
066         * @param matchingRuleId the OID of the matching rule
067         * @param dnAttributes the dn attributes
068         */
069        public ExtensibleNode( String attribute, Value<?> value, String matchingRuleId, boolean dnAttributes )
070        {
071            super( attribute, AssertionType.EXTENSIBLE );
072    
073            this.value = value;
074            this.matchingRuleId = matchingRuleId;
075            this.dnAttributes = dnAttributes;
076        }
077    
078        /**
079         * Makes a full clone in new memory space of the current node and children
080         * 
081         * @return the clone
082         */
083        @Override public ExprNode clone()
084        {
085            ExprNode clone = (ExprNode)super.clone();
086            
087            // Copy the value
088            if ( value != null )
089            {
090                ((ExtensibleNode)clone).value = value.clone();
091            }
092            
093            return clone;
094        }
095    
096        /**
097         * Gets the Dn attributes.
098         * 
099         * @return the dn attributes
100         */
101        public boolean hasDnAttributes()
102        {
103            return dnAttributes;
104        }
105        
106        
107        /**
108         * Set the dnAttributes flag
109         *
110         * @param dnAttributes The flag to set
111         */
112        public void setDnAttributes( boolean dnAttributes )
113        {
114            this.dnAttributes = dnAttributes;
115        }
116    
117    
118        /**
119         * Gets the matching rule id as an OID string.
120         * 
121         * @return the OID
122         */
123        public String getMatchingRuleId()
124        {
125            return matchingRuleId;
126        }
127    
128    
129        /**
130         * Sets the matching rule id as an OID string.
131         * 
132         * @param matchingRuleId The maching rule ID
133         */
134        public void setMatchingRuleId( String matchingRuleId )
135        {
136            this.matchingRuleId = matchingRuleId;
137        }
138    
139    
140        /**
141         * Gets the value.
142         * 
143         * @return the value
144         */
145        public final Value<?> getValue()
146        {
147            return value;
148        }
149    
150    
151        /** 
152         * @return representation of value, escaped for use in a filter if required 
153         */
154        public Value<?> getEscapedValue()
155        {
156            if ( !value.isBinary() )
157            {
158                return AbstractExprNode.escapeFilterValue( value );
159            }
160            
161            return value;
162        }
163    
164        
165        /**
166         * Sets the value.
167         * 
168         * @param value the value
169         */
170        public final void setValue( Value<?> value)
171        {
172            this.value = value;
173        }
174    
175        
176        /**
177         * @see Object#hashCode()
178         * @return the instance's hash code 
179         */
180        public int hashCode()
181        {
182            int h = 37;
183            
184            h = h*17 + super.hashCode();
185            h = h*17 + ( dnAttributes ? 1 : 0 );
186            h = h*17 + matchingRuleId.hashCode();
187            h = h*17 + value.hashCode();
188            
189            return h;
190        }
191    
192    
193        /**
194         * @see java.lang.Object#toString()
195         * @return A string representing the AndNode
196         */
197        public String toString()
198        {
199            StringBuilder buf = new StringBuilder();
200            
201            buf.append( '(' ).append( getAttribute() );
202            buf.append( "-" );
203            buf.append( dnAttributes );
204            buf.append( "-EXTENSIBLE-" );
205            buf.append( matchingRuleId );
206            buf.append( "-" );
207            buf.append( value );
208    
209            buf.append( super.toString() );
210            
211            buf.append( ')' );
212            
213            return buf.toString();
214        }
215    }