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 org.apache.directory.shared.i18n.I18n;
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( 0, "base" ), 
033        ONELEVEL( 1, "one" ), 
034        SUBTREE( 2, "sub" );
035        
036        /** 
037         * The corresponding LDAP scope constant value as defined in 
038         * RFC 4511
039         */ 
040        private final int scope;
041        
042        /**
043         * The LDAP URL string value of either base, one or sub as defined in RFC
044         * 2255.
045         * 
046         * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
047         */
048        private final String ldapUrlValue;
049        
050    
051        /**
052         * Creates a new instance of SearchScope based on the respective 
053         * scope constant.
054         *
055         * @param scope the scope constant
056         * @param ldapUrlValue LDAP URL scope string value: base, one, or sub
057         */
058        private SearchScope( int scope, String ldapUrlValue )
059        {
060            this.scope = scope;
061            this.ldapUrlValue = ldapUrlValue;
062        }
063    
064        
065        /**
066         * Gets the LDAP URL value for the scope: according to RFC 2255 this is 
067         * either base, one, or sub.
068         * 
069         * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
070         */
071        public String getLdapUrlValue()
072        {
073            return ldapUrlValue;
074        }
075        
076    
077        /**
078         * Gets the corresponding scope constant value as defined in 
079         * RFC 4511.
080         * 
081         * @return the scope
082         */
083        public int getScope()
084        {
085            return scope;
086        }
087        
088        
089        /**
090         * Gets the SearchScope enumerated type for the corresponding 
091         * scope numeric value.
092         *
093         * @param scope the numeric value to get SearchScope for
094         * @return the SearchScope enumerated type for the scope numeric value
095         */
096        public static SearchScope getSearchScope( int scope )
097        {
098            switch( scope )
099            {
100                case 0 : 
101                    return OBJECT;
102                
103                case 1 :
104                    return ONELEVEL;
105                    
106                case 2 :
107                    return SUBTREE;
108                    
109                default:
110                    throw new IllegalArgumentException( I18n.err( I18n.ERR_04160, scope ) );
111            }
112        }
113    
114    
115        /**
116         * Gets the SearchScope enumerated type for the corresponding 
117         * LDAP URL scope value of either base, one or sub.
118         *
119         * @param ldapUrlValue the LDAP URL scope value to get SearchScope for
120         * @return the SearchScope enumerated type for the LDAP URL scope value
121         */
122        public static int getSearchScope( String ldapUrlValue )
123        {
124            if ( "base".equalsIgnoreCase( ldapUrlValue ) )
125            {
126                return OBJECT.getScope();
127            }
128            else if ( "one".equalsIgnoreCase( ldapUrlValue ) )
129            {
130                return ONELEVEL.getScope();
131            }
132            else if ( "sub".equalsIgnoreCase( ldapUrlValue ) )
133            {
134                return SUBTREE.getScope();
135            }
136            else
137            {
138                throw new IllegalArgumentException( I18n.err( I18n.ERR_04161, ldapUrlValue ) );
139            }
140        }
141        
142        
143        /**
144         * {@inheritDoc}
145         */
146        public String toString()
147        {
148            return ldapUrlValue;
149        }
150    }