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      OBJECT(0, "base"),
34      ONELEVEL(1, "one"),
35      SUBTREE(2, "sub");
36  
37      /** 
38       * The corresponding LDAP scope constant value as defined in 
39       * RFC 4511
40       */
41      private final int scope;
42  
43      /**
44       * The LDAP URL string value of either base, one or sub as defined in RFC
45       * 2255.
46       * 
47       * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
48       */
49      private final String ldapUrlValue;
50  
51  
52      /**
53       * Creates a new instance of SearchScope based on the respective 
54       * scope constant.
55       *
56       * @param scope the scope constant
57       * @param ldapUrlValue LDAP URL scope string value: base, one, or sub
58       */
59      private SearchScope( int scope, String ldapUrlValue )
60      {
61          this.scope = scope;
62          this.ldapUrlValue = ldapUrlValue;
63      }
64  
65  
66      /**
67       * Gets the LDAP URL value for the scope: according to RFC 2255 this is 
68       * either base, one, or sub.
69       * 
70       * @see <a href="http://www.faqs.org/rfcs/rfc2255.html">RFC 2255</a>
71       */
72      public String getLdapUrlValue()
73      {
74          return ldapUrlValue;
75      }
76  
77  
78      /**
79       * Gets the corresponding scope constant value as defined in 
80       * RFC 4511.
81       * 
82       * @return the scope
83       */
84      public int getScope()
85      {
86          return scope;
87      }
88  
89  
90      /**
91       * Gets the SearchScope enumerated type for the corresponding 
92       * scope numeric value.
93       *
94       * @param scope the numeric value to get SearchScope for
95       * @return the SearchScope enumerated type for the scope numeric value
96       */
97      public static SearchScope getSearchScope( int scope )
98      {
99          switch ( scope )
100         {
101             case 0:
102                 return OBJECT;
103 
104             case 1:
105                 return ONELEVEL;
106 
107             case 2:
108                 return SUBTREE;
109 
110             default:
111                 throw new IllegalArgumentException( I18n.err( I18n.ERR_04160, scope ) );
112         }
113     }
114 
115 
116     /**
117      * Gets the SearchScope enumerated type for the corresponding 
118      * LDAP URL scope value of either base, one or sub.
119      *
120      * @param ldapUrlValue the LDAP URL scope value to get SearchScope for
121      * @return the SearchScope enumerated type for the LDAP URL scope value
122      */
123     public static int getSearchScope( String ldapUrlValue )
124     {
125         if ( "base".equalsIgnoreCase( ldapUrlValue ) )
126         {
127             return OBJECT.getScope();
128         }
129         else if ( "one".equalsIgnoreCase( ldapUrlValue ) )
130         {
131             return ONELEVEL.getScope();
132         }
133         else if ( "sub".equalsIgnoreCase( ldapUrlValue ) )
134         {
135             return SUBTREE.getScope();
136         }
137         else
138         {
139             throw new IllegalArgumentException( I18n.err( I18n.ERR_04161, ldapUrlValue ) );
140         }
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     public String toString()
148     {
149         return ldapUrlValue;
150     }
151 }