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    import javax.naming.directory.SearchControls;
023    
024    /**
025     * A search scope enumerated type.
026     *
027     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
028     * @version $Rev$, $Date$
029     */
030    public enum SearchScope
031    {
032        OBJECT( SearchControls.OBJECT_SCOPE, "base" ), 
033        ONELEVEL( SearchControls.ONELEVEL_SCOPE, "one" ), 
034        SUBTREE( SearchControls.SUBTREE_SCOPE, "sub" );
035        
036        /** 
037         * The corresponding JNDI scope constant value as defined in 
038         * SearchControls.
039         * 
040         * @see javax.naming.directory.SearchControls#OBJECT_SCOPE
041         * @see javax.naming.directory.SearchControls#ONELEVEL_SCOPE
042         * @see javax.naming.directory.SearchControls#SUBTREE_SCOPE
043         */ 
044        private final int jndiScope;
045        
046        /**
047         * The LDAP URL string value of either base, one or sub as defined in RFC
048         * 2255.
049         * 
050         * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
051         */
052        private final String ldapUrlValue;
053        
054    
055        /**
056         * Creates a new instance of SearchScope based on the respective 
057         * SearchControls scope constant.
058         *
059         * @param jndiScope the JNDI scope constant
060         * @param ldapUrlValue LDAP URL scope string value: base, one, or sub
061         */
062        private SearchScope( int jndiScope, String ldapUrlValue )
063        {
064            this.jndiScope = jndiScope;
065            this.ldapUrlValue = ldapUrlValue;
066        }
067    
068        
069        /**
070         * Gets the LDAP URL value for the scope: according to RFC 2255 this is 
071         * either base, one, or sub.
072         * 
073         * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
074         */
075        public String getLdapUrlValue()
076        {
077            return ldapUrlValue;
078        }
079        
080    
081        /**
082         * Gets the corresponding JNDI scope constant value as defined in 
083         * SearchControls.
084         * 
085         * @return the jndiScope
086         * @see javax.naming.directory.SearchControls#OBJECT_SCOPE
087         * @see javax.naming.directory.SearchControls#ONELEVEL_SCOPE
088         * @see javax.naming.directory.SearchControls#SUBTREE_SCOPE
089         */
090        public int getJndiScope()
091        {
092            return jndiScope;
093        }
094        
095        
096        /**
097         * Gets the SearchScope enumerated type for the corresponding 
098         * SearchControls scope setting.
099         *
100         * @param searchControls the search controls to get SearchScope for
101         * @return the SearchScope enumerated type for the SearchControls
102         */
103        public static SearchScope getSearchScope( SearchControls searchControls )
104        {
105            return getSearchScope( searchControls.getSearchScope() );
106        }
107        
108        
109        /**
110         * Gets the SearchScope enumerated type for the corresponding 
111         * JNDI numeric value.
112         *
113         * @param jndiScope the JNDI numeric value to get SearchScope for
114         * @return the SearchScope enumerated type for JNDI numeric value
115         */
116        public static SearchScope getSearchScope( int jndiScope )
117        {
118            switch( jndiScope )
119            {
120                case( SearchControls.OBJECT_SCOPE ): 
121                    return OBJECT;
122                case( SearchControls.ONELEVEL_SCOPE ):
123                    return ONELEVEL;
124                case( SearchControls.SUBTREE_SCOPE ):
125                    return SUBTREE;
126                default:
127                    throw new IllegalArgumentException( "Unknown JNDI scope constant value: " + jndiScope );
128            }
129        }
130    
131    
132        /**
133         * Gets the SearchScope enumerated type for the corresponding 
134         * LDAP URL scope value of either base, one or sub.
135         *
136         * @param ldapUrlValue the LDAP URL scope value to get SearchScope for
137         * @return the SearchScope enumerated type for the LDAP URL scope value
138         */
139        public static SearchScope getSearchScope( String ldapUrlValue )
140        {
141            if ( "base".equalsIgnoreCase( ldapUrlValue ) )
142            {
143                return OBJECT;
144            }
145            else if ( "one".equalsIgnoreCase( ldapUrlValue ) )
146            {
147                return ONELEVEL;
148            }
149            else if ( "sub".equalsIgnoreCase( ldapUrlValue ) )
150            {
151                return SUBTREE;
152            }
153            else
154            {
155                throw new IllegalArgumentException( "Unknown LDAP URL scope value: " + ldapUrlValue );
156            }
157        }
158    
159    }