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.api.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026import org.apache.directory.api.util.Strings;
027
028
029/**
030 * A SyntaxChecker which verifies that a value is a valid Search Scope. We
031 * have three possible values :
032 * <ul>
033 * <li>OBJECT</li>
034 * <li>ONE</li>
035 * <li>SUBTREE</li>
036 * </ul>
037 * The value is case insensitive
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041@SuppressWarnings("serial")
042public final class SearchScopeSyntaxChecker extends SyntaxChecker
043{
044    /**
045     * A static instance of SearchScopeSyntaxChecker
046     */
047    public static final SearchScopeSyntaxChecker INSTANCE = 
048        new SearchScopeSyntaxChecker( SchemaConstants.SEARCH_SCOPE_SYNTAX );
049    
050    /**
051     * A static Builder for this class
052     */
053    public static final class Builder extends SCBuilder<SearchScopeSyntaxChecker>
054    {
055        /**
056         * The Builder constructor
057         */
058        private Builder()
059        {
060            super( SchemaConstants.SEARCH_SCOPE_SYNTAX );
061        }
062        
063        
064        /**
065         * Create a new instance of SearchScopeSyntaxChecker
066         * @return A new instance of SearchScopeSyntaxChecker
067         */
068        @Override
069        public SearchScopeSyntaxChecker build()
070        {
071            return new SearchScopeSyntaxChecker( oid );
072        }
073    }
074
075    
076    /**
077     * Creates a new instance of SearchScopeSyntaxChecker.
078     * 
079     * @param oid The OID to use for this SyntaxChecker
080     */
081    private SearchScopeSyntaxChecker( String oid )
082    {
083        super( oid );
084    }
085
086    
087    /**
088     * @return An instance of the Builder for this class
089     */
090    public static Builder builder()
091    {
092        return new Builder();
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @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}