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.shared.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
024import org.apache.directory.shared.ldap.model.schema.SyntaxChecker;
025import org.apache.directory.shared.util.Strings;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029
030/**
031 * A SyntaxChecker which verifies that a value is a Boolean according to RFC 1778.
032 * 
033 * From RFC 1778 :
034 * 
035 * <mail-preference> ::= "NO-LISTS" | "ANY-LIST" | "PROFESSIONAL-LISTS"
036 * 
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040@SuppressWarnings("serial")
041public class MailPreferenceSyntaxChecker extends SyntaxChecker
042{
043    /** A logger for this class */
044    private static final Logger LOG = LoggerFactory.getLogger( MailPreferenceSyntaxChecker.class );
045
046    /**
047     * Creates a new instance of MailPreferenceSyntaxChecker.
048     */
049    public MailPreferenceSyntaxChecker()
050    {
051        super( SchemaConstants.MAIL_PREFERENCE_SYNTAX );
052    }
053    
054    /**
055     * 
056     * Creates a new instance of MailPreferenceSyntaxChecker.
057     * 
058     * @param oid the oid to associate with this new SyntaxChecker
059     *
060     */
061    protected MailPreferenceSyntaxChecker( String oid )
062    {
063        super( oid );
064    }
065    
066    
067    /**
068     * {@inheritDoc}
069     */
070    public boolean isValidSyntax( Object value )
071    {
072        String strValue =null;
073
074        if ( value == null )
075        {
076            LOG.debug( "Syntax invalid for 'null'" );
077            return false;
078        }
079        
080        if ( value instanceof String )
081        {
082            strValue = ( String ) value;
083        }
084        else if ( value instanceof byte[] )
085        {
086            strValue = Strings.utf8ToString((byte[]) value);
087        }
088        else
089        {
090            strValue = value.toString();
091        }
092
093        if ( ( strValue.length() < 8 ) || ( strValue.length() > 18 ) )
094        {
095            LOG.debug( "Syntax invalid for '{}'", value );
096            return false;
097        }
098        
099        boolean result = ( ( "NO-LISTS".equals( strValue ) ) || ( "ANY-LIST".equals( strValue ) )
100            || ( "PROFESSIONAL-LISTS".equals( strValue ) ) );
101
102        if ( result )
103        {
104            LOG.debug( "Syntax valid for '{}'", value );
105        }
106        else
107        {
108            LOG.debug( "Syntax invalid for '{}'", value );
109        }
110        
111        return result;
112    }
113}