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 DerefAlias. We
031 * have four possible values :
032 * <ul>
033 * <li>NEVER</li>
034 * <li>SEARCHING</li>
035 * <li>FINDING</li>
036 * <li>ALWAYS</li>
037 * </ul>
038 * The value is case insensitive
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042@SuppressWarnings("serial")
043public final class DerefAliasSyntaxChecker extends SyntaxChecker
044{
045    /**
046     * A static instance of DerefAliasSyntaxChecker
047     */
048    public static final DerefAliasSyntaxChecker INSTANCE = 
049        new DerefAliasSyntaxChecker( SchemaConstants.DEREF_ALIAS_SYNTAX );
050    
051    /**
052     * A static Builder for this class
053     */
054    public static final class Builder extends SCBuilder<DerefAliasSyntaxChecker>
055    {
056        /**
057         * The Builder constructor
058         */
059        private Builder()
060        {
061            super( SchemaConstants.DEREF_ALIAS_SYNTAX );
062        }
063        
064        
065        /**
066         * Create a new instance of DerefAliasSyntaxChecker
067         * @return A new instance of DerefAliasSyntaxChecker
068         */
069        @Override
070        public DerefAliasSyntaxChecker build()
071        {
072            return new DerefAliasSyntaxChecker( oid );
073        }
074    }
075
076    /**
077     * Creates a new instance of DerefAliasSyntaxChecker.
078     * 
079     * @param oid The OID to use for this SyntaxChecker
080     */
081    private DerefAliasSyntaxChecker( 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 "never".equals( strValue ) || "finding".equals( strValue ) || "searching".equals( strValue ) || "always"
130            .equals( strValue );
131    }
132}