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 org.apache.directory.shared.ldap.message.AliasDerefMode;
024    
025    
026    /**
027     * Node used not to represent a published assertion but an assertion on the
028     * scope of the search.
029     * 
030     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031     * @version $Rev: 915599 $
032     */
033    public class ScopeNode extends AbstractExprNode
034    {
035        /** the scope of this node */
036        private final SearchScope scope;
037    
038        /** the search base */
039        private final String baseDn;
040    
041        /** the alias dereferencing mode */
042        private final AliasDerefMode aliasDerefAliases;
043    
044    
045        /**
046         * Creates a new ScopeNode object.
047         * 
048         * @param aliasDerefAliases the alias dereferencing mode
049         * @param baseDn the search base
050         * @param scope the search scope
051         */
052        public ScopeNode( AliasDerefMode aliasDerefAliases, String baseDn, SearchScope scope )
053        {
054            super( AssertionType.SCOPE );
055            this.scope = scope;
056            this.baseDn = baseDn;
057            this.aliasDerefAliases = aliasDerefAliases;
058        }
059    
060        /**
061         * Makes a full clone in new memory space of the current node and children
062         * 
063         * @return the clone
064         */
065        @Override public ExprNode clone()
066        {
067            return super.clone();
068        }    
069    
070    
071        /**
072         * Always returns true since a scope node has no children.
073         * 
074         * @see ExprNode#isLeaf()
075         * @return <code>true</code>
076         */
077        public boolean isLeaf()
078        {
079            return true;
080        }
081    
082    
083        /**
084         * Gets the search scope.
085         * 
086         * @return the search scope 
087         */
088        public SearchScope getScope()
089        {
090            return scope;
091        }
092    
093    
094        /**
095         * Gets the base dn.
096         * 
097         * @return the base dn
098         */
099        public String getBaseDn()
100        {
101            return baseDn;
102        }
103    
104    
105        /**
106         * Gets the alias dereferencing mode type safe enumeration.
107         * 
108         * @return the alias dereferencing enumeration constant.
109         */
110        public AliasDerefMode getDerefAliases()
111        {
112            return aliasDerefAliases;
113        }
114    
115    
116        /**
117         * @see org.apache.directory.shared.ldap.filter.ExprNode#accept(
118         *      org.apache.directory.shared.ldap.filter.FilterVisitor)
119         * 
120         * @param visitor the filter expression tree structure visitor
121         * @return The modified element
122         */
123        public Object accept( FilterVisitor visitor )
124        {
125            if ( visitor.canVisit( this ) )
126            {
127                return visitor.visit( this );
128            }
129            else
130            {
131                return null;
132            }
133        }
134    
135    
136        /**
137         * @see Object#hashCode()
138         * @return the instance's hash code 
139         */
140        public int hashCode()
141        {
142            int h = 37;
143            
144            h = h*17 + super.hashCode();
145            h = h*17 + ( aliasDerefAliases != null ? aliasDerefAliases.hashCode() : 0 );
146            h = h*17 + ( baseDn != null ? baseDn.hashCode() : 0 );
147            h = h*17 + scope.getScope();
148            
149            return h;
150        }
151    
152    
153        /**
154         * @see Object#toString()
155         * @return A string representing the AndNode
156         */
157        public String toString()
158        {
159            StringBuilder buf = new StringBuilder();
160            
161            buf.append( "(#{" );
162    
163            switch ( scope )
164            {
165                case OBJECT:
166                    buf.append( "OBJECT_SCOPE" );
167    
168                    break;
169    
170                case ONELEVEL:
171                    buf.append( "ONE_LEVEL_SCOPE" );
172    
173                    break;
174    
175                case SUBTREE:
176                    buf.append( "SUBTREE_SCOPE (Estimated)" );
177    
178                    break;
179    
180                default:
181                    buf.append( "UNKNOWN" );
182                    break;
183            }
184            
185            buf.append( ", '" );
186            buf.append( baseDn );
187            buf.append( "', " );
188            buf.append( aliasDerefAliases );
189            buf.append( "}" );
190            buf.append( super.toString() );
191            buf.append( ')' );
192            
193            return buf.toString();
194        }
195    }