View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.model.message;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  
25  
26  /**
27   * A search scope enumerated type.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public enum SearchScope
32  {
33      /** Base scope */
34      OBJECT(0, "base"),
35      
36      /** One Level scope */
37      ONELEVEL(1, "one"),
38      
39      /** Subtree scope */
40      SUBTREE(2, "sub");
41  
42      /** 
43       * The corresponding LDAP scope constant value as defined in 
44       * RFC 4511
45       */
46      private final int scope;
47  
48      /**
49       * The LDAP URL string value of either base, one or sub as defined in RFC
50       * 2255.
51       * 
52       * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
53       */
54      private final String ldapUrlValue;
55  
56  
57      /**
58       * Creates a new instance of SearchScope based on the respective 
59       * scope constant.
60       *
61       * @param scope the scope constant
62       * @param ldapUrlValue LDAP URL scope string value: base, one, or sub
63       */
64      SearchScope( int scope, String ldapUrlValue )
65      {
66          this.scope = scope;
67          this.ldapUrlValue = ldapUrlValue;
68      }
69  
70  
71      /**
72       * Gets the LDAP URL value for the scope: according to RFC 2255 this is 
73       * either base, one, or sub.
74       * 
75       * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
76       * 
77       * @return the LDAP URL value
78       */
79      public String getLdapUrlValue()
80      {
81          return ldapUrlValue;
82      }
83  
84  
85      /**
86       * Gets the corresponding scope constant value as defined in 
87       * RFC 4511.
88       * 
89       * @return the scope
90       */
91      public int getScope()
92      {
93          return scope;
94      }
95  
96  
97      /**
98       * Gets the SearchScope enumerated type for the corresponding 
99       * scope numeric value.
100      *
101      * @param scope the numeric value to get SearchScope for
102      * @return the SearchScope enumerated type for the scope numeric value
103      */
104     public static SearchScope getSearchScope( int scope )
105     {
106         switch ( scope )
107         {
108             case 0:
109                 return OBJECT;
110 
111             case 1:
112                 return ONELEVEL;
113 
114             case 2:
115                 return SUBTREE;
116 
117             default:
118                 throw new IllegalArgumentException( I18n.err( I18n.ERR_04160, scope ) );
119         }
120     }
121 
122 
123     /**
124      * Gets the SearchScope associated with a scope String
125      *
126      * @param scope The scope we are looking for
127      * @return the scope
128      */
129     public SearchScope getScope( String scope )
130     {
131         if ( "base".equalsIgnoreCase( scope ) )
132         {
133             return OBJECT;
134         }
135         else if ( "one".equalsIgnoreCase( scope ) )
136         {
137             return ONELEVEL;
138         }
139         else if ( "sub".equalsIgnoreCase( scope ) )
140         {
141             return SUBTREE;
142         }
143         else
144         {
145             throw new IllegalArgumentException( I18n.err( I18n.ERR_04161, scope ) );
146         }
147     }
148 
149 
150     /**
151      * Gets the SearchScope enumerated type for the corresponding 
152      * scope value of either base, one or sub.
153      *
154      * @param scope the scope value to get SearchScope for
155      * @return the SearchScope enumerated type for the LDAP URL scope value
156      */
157     public static int getSearchScope( String scope )
158     {
159         if ( "base".equalsIgnoreCase( scope ) )
160         {
161             return OBJECT.getScope();
162         }
163         else if ( "one".equalsIgnoreCase( scope ) )
164         {
165             return ONELEVEL.getScope();
166         }
167         else if ( "sub".equalsIgnoreCase( scope ) )
168         {
169             return SUBTREE.getScope();
170         }
171         else
172         {
173             throw new IllegalArgumentException( I18n.err( I18n.ERR_04161, scope ) );
174         }
175     }
176 
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public String toString()
183     {
184         return ldapUrlValue;
185     }
186 }