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 Boolean according to RFC 1778.
031 * 
032 * From RFC 1778 :
033 * <pre>
034 * &lt;mail-preference&gt; ::= "NO-LISTS" | "ANY-LIST" | "PROFESSIONAL-LISTS"
035 * </pre>
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039@SuppressWarnings("serial")
040public final class MailPreferenceSyntaxChecker extends SyntaxChecker
041{
042    /**
043     * A static instance of MailPreferenceSyntaxChecker
044     */
045    public static final MailPreferenceSyntaxChecker INSTANCE = 
046        new MailPreferenceSyntaxChecker( SchemaConstants.MAIL_PREFERENCE_SYNTAX );
047    
048    /**
049     * A static Builder for this class
050     */
051    public static final class Builder extends SCBuilder<MailPreferenceSyntaxChecker>
052    {
053        /**
054         * The Builder constructor
055         */
056        private Builder()
057        {
058            super( SchemaConstants.MAIL_PREFERENCE_SYNTAX );
059        }
060        
061        
062        /**
063         * Create a new instance of MailPreferenceSyntaxChecker
064         * @return A new instance of MailPreferenceSyntaxChecker
065         */
066        @Override
067        public MailPreferenceSyntaxChecker build()
068        {
069            return new MailPreferenceSyntaxChecker( oid );
070        }
071    }
072
073    
074    /**
075     * 
076     * Creates a new instance of MailPreferenceSyntaxChecker.
077     * 
078     * @param oid the oid to associate with this new SyntaxChecker
079     *
080     */
081    private MailPreferenceSyntaxChecker( 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        if ( ( strValue.length() < 8 ) || ( strValue.length() > 18 ) )
128        {
129            if ( LOG.isDebugEnabled() )
130            {
131                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
132            }
133            
134            return false;
135        }
136
137        boolean result = ( "NO-LISTS".equals( strValue ) ) || ( "ANY-LIST".equals( strValue ) )
138            || ( "PROFESSIONAL-LISTS".equals( strValue ) );
139
140        if ( LOG.isDebugEnabled() )
141        {
142            if ( result )
143            {
144                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
145            }
146            else
147            {
148                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
149            }
150        }
151
152        return result;
153    }
154}