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 syntax checker which checks to see if an attributeType's type is either: 
031 * <ul>
032 *   <li>userApplications</li>
033 *   <lidirectoryOperation</li>
034 *   <lidistributedOperation</li>
035 *   <lidSAOperation</li>
036 * </ul>
037.* 
038 * The case is NOT ignored.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042@SuppressWarnings("serial")
043public final class AttributeTypeUsageSyntaxChecker extends SyntaxChecker
044{
045    /**
046     * A static instance of AttributeTypeUsageSyntaxChecker
047     */
048    public static final AttributeTypeUsageSyntaxChecker INSTANCE = 
049        new AttributeTypeUsageSyntaxChecker( SchemaConstants.ATTRIBUTE_TYPE_USAGE_SYNTAX );
050
051    /**
052     * A static Builder for this class
053     */
054    public static final class Builder extends SCBuilder<AttributeTypeUsageSyntaxChecker>
055    {
056        /**
057         * The Builder constructor
058         */
059        private Builder()
060        {
061            super( SchemaConstants.ATTRIBUTE_TYPE_USAGE_SYNTAX );
062        }
063        
064        
065        /**
066         * Create a new instance of AttributeTypeUsageSyntaxChecker
067         * @return A new instance of AttributeTypeUsageSyntaxChecker
068         */
069        @Override
070        public AttributeTypeUsageSyntaxChecker build()
071        {
072            return new AttributeTypeUsageSyntaxChecker( oid );
073        }
074    }
075
076    
077    /**
078     * Creates a new instance of AttributeTypeUsageSyntaxChecker.
079     * 
080     * @param oid The OID to use for this SyntaxChecker
081     */
082    private AttributeTypeUsageSyntaxChecker( String oid )
083    {
084        super( oid );
085    }
086
087
088    /**
089     * @return An instance of the Builder for this class
090     */
091    public static Builder builder()
092    {
093        return new Builder();
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    @Override
101    public boolean isValidSyntax( Object value )
102    {
103        String strValue;
104
105        if ( value == null )
106        {
107            if ( LOG.isDebugEnabled() )
108            {
109                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
110            }
111            
112            return false;
113        }
114
115        if ( value instanceof String )
116        {
117            strValue = ( String ) value;
118        }
119        else if ( value instanceof byte[] )
120        {
121            strValue = Strings.utf8ToString( ( byte[] ) value );
122        }
123        else
124        {
125            strValue = value.toString();
126        }
127
128        switch ( strValue )
129        {
130            case "dSAOperation" :
131            case "directoryOperation" :
132            case "distributedOperation" :
133            case "userApplications" :
134                if ( LOG.isDebugEnabled() )
135                {
136                    LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
137                }
138                
139                return true;
140
141            default :
142                if ( LOG.isDebugEnabled() )
143                {
144                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
145                }
146                
147                return false;
148        }
149    }
150}