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