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.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * A SyntaxChecker which verifies that a value is a valid Search Scope. We
31   * have three possible values :
32   * <ul>
33   * <li>OBJECT</li>
34   * <li>ONE</li>
35   * <li>SUBTREE</li>
36   * </ul>
37   * The value is case insensitive
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  @SuppressWarnings("serial")
42  public final class SearchScopeSyntaxChecker extends SyntaxChecker
43  {
44      /**
45       * A static instance of SearchScopeSyntaxChecker
46       */
47      public static final SearchScopeSyntaxChecker INSTANCE = 
48          new SearchScopeSyntaxChecker( SchemaConstants.SEARCH_SCOPE_SYNTAX );
49      
50      /**
51       * A static Builder for this class
52       */
53      public static final class Builder extends SCBuilder<SearchScopeSyntaxChecker>
54      {
55          /**
56           * The Builder constructor
57           */
58          private Builder()
59          {
60              super( SchemaConstants.SEARCH_SCOPE_SYNTAX );
61          }
62          
63          
64          /**
65           * Create a new instance of SearchScopeSyntaxChecker
66           * @return A new instance of SearchScopeSyntaxChecker
67           */
68          @Override
69          public SearchScopeSyntaxChecker build()
70          {
71              return new SearchScopeSyntaxChecker( oid );
72          }
73      }
74  
75      
76      /**
77       * Creates a new instance of SearchScopeSyntaxChecker.
78       * 
79       * @param oid The OID to use for this SyntaxChecker
80       */
81      private SearchScopeSyntaxChecker( String oid )
82      {
83          super( oid );
84      }
85  
86      
87      /**
88       * @return An instance of the Builder for this class
89       */
90      public static Builder builder()
91      {
92          return new Builder();
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public boolean isValidSyntax( Object value )
101     {
102         String strValue;
103 
104         if ( value == null )
105         {
106             if ( LOG.isDebugEnabled() )
107             {
108                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
109             }
110             
111             return false;
112         }
113 
114         if ( value instanceof String )
115         {
116             strValue = ( String ) value;
117         }
118         else if ( value instanceof byte[] )
119         {
120             strValue = Strings.utf8ToString( ( byte[] ) value );
121         }
122         else
123         {
124             strValue = value.toString();
125         }
126 
127         strValue = Strings.trim( Strings.toLowerCaseAscii( strValue ) );
128 
129         return "base".equals( strValue ) || "one".equals( strValue ) || "sub".equals( strValue );
130     }
131 }