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;
021
022
023import org.apache.directory.shared.ldap.model.exception.LdapException;
024import org.apache.directory.shared.ldap.model.exception.LdapInvalidAttributeValueException;
025import org.apache.directory.shared.ldap.model.message.ResultCodeEnum;
026
027
028/**
029 * Used to validate values of a particular syntax. This interface does not
030 * correlate to any LDAP or X.500 construct. It has been created as a means to
031 * enforce a syntax within the Eve server.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035// super.hashCode is final
036@SuppressWarnings({ "PMD.OverrideBothEqualsAndHashcode", "serial" })
037public abstract class SyntaxChecker extends LoadableSchemaObject
038{
039    /**
040     * The SyntaxChecker base constructor
041     * @param oid The associated OID
042     */
043    protected SyntaxChecker( String oid )
044    {
045        super( SchemaObjectType.SYNTAX_CHECKER, oid );
046    }
047
048
049    /**
050     * The SyntaxChecker default constructor where the oid is set after 
051     * instantiation.
052     */
053    protected SyntaxChecker()
054    {
055        super( SchemaObjectType.SYNTAX_CHECKER );
056    }
057
058
059    /**
060     * Determines if the attribute's value conforms to the attribute syntax.
061     * 
062     * @param value the value of some attribute with the syntax
063     * @return true if the value is in the valid syntax, false otherwise
064     */
065    public abstract boolean isValidSyntax( Object value );
066
067
068    /**
069     * Asserts whether or not the attribute's value conforms to the attribute
070     * syntax.
071     * 
072     * @param value the value of some attribute with the syntax
073     * @throws LdapException if the value does not conform to the attribute syntax.
074     */
075    public void assertSyntax( Object value ) throws LdapException
076    {
077        if ( !isValidSyntax( value ) )
078        {
079            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX );
080        }
081    }
082
083
084    /**
085     * Store the SchemaManager in this instance. It may be necessary for some
086     * syntaxChecker which needs to have access to the oidNormalizer Map.
087     *
088     * @param schemaManager the schemaManager to store
089     */
090    public void setSchemaManager( SchemaManager schemaManager )
091    {
092        // Do nothing (general case).
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public boolean equals( Object o )
101    {
102        if ( !super.equals( o ) )
103        {
104            return false;
105        }
106
107        return o instanceof SyntaxChecker;
108    }
109
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public String toString()
116    {
117        return objectType + " " + DescriptionUtils.getDescription( this );
118    }
119}