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.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
025import org.apache.directory.api.util.Strings;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029
030/**
031 * A syntax checker which checks to see if an attributeType's type is either: 
032 * userApplications
033 * directoryOperation
034 * distributedOperation
035 * dSAOperation
036.*  The case is NOT ignored.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040@SuppressWarnings("serial")
041public class AttributeTypeUsageSyntaxChecker extends SyntaxChecker
042{
043    /** A logger for this class */
044    private static final Logger LOG = LoggerFactory.getLogger( AttributeTypeUsageSyntaxChecker.class );
045
046
047    /**
048     * 
049     * Creates a new instance of AttributeTypeUsageSyntaxChecker.
050     *
051     */
052    public AttributeTypeUsageSyntaxChecker()
053    {
054        super( SchemaConstants.ATTRIBUTE_TYPE_USAGE_SYNTAX );
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    public boolean isValidSyntax( Object value )
062    {
063        String strValue = null;
064
065        if ( value == null )
066        {
067            LOG.debug( "Syntax invalid for 'null'" );
068            return false;
069        }
070
071        if ( value instanceof String )
072        {
073            strValue = ( String ) value;
074        }
075        else if ( value instanceof byte[] )
076        {
077            strValue = Strings.utf8ToString( ( byte[] ) value );
078        }
079        else
080        {
081            strValue = value.toString();
082        }
083
084        if ( ( strValue.length() < "userApplications".length() )
085            || ( strValue.length() > "userApplications".length() ) )
086        {
087            LOG.debug( "Syntax invalid for '{}'", value );
088            return false;
089        }
090
091        char ch = strValue.charAt( 0 );
092
093        switch ( ch )
094        {
095            case ( 'd' ):
096                if ( "dSAOperation".equals( strValue )
097                    || "directoryOperation".equals( strValue )
098                    || "distributedOperation".equals( strValue ) )
099                {
100                    LOG.debug( "Syntax valid for '{}'", value );
101                    return true;
102                }
103
104                LOG.debug( "Syntax invalid for '{}'", value );
105                return false;
106
107            case ( 'u' ):
108                boolean comp = "userApplications".equals( strValue );
109
110                if ( comp )
111                {
112                    LOG.debug( "Syntax valid for '{}'", value );
113                }
114                else
115                {
116                    LOG.debug( "Syntax invalid for '{}'", value );
117
118                }
119
120                return comp;
121
122            default:
123                LOG.debug( "Syntax invalid for '{}'", value );
124                return false;
125        }
126    }
127}