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;
021
022
023import org.apache.directory.api.ldap.model.exception.LdapException;
024import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
025import org.apache.directory.api.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 */
035public abstract class SyntaxChecker extends LoadableSchemaObject
036{
037    /** The mandatory serialVersionUID */
038    public static final long serialVersionUID = 1L;
039
040
041    /**
042     * The SyntaxChecker base constructor
043     * @param oid The associated OID
044     */
045    protected SyntaxChecker( String oid )
046    {
047        super( SchemaObjectType.SYNTAX_CHECKER, oid );
048    }
049
050
051    /**
052     * The SyntaxChecker default constructor where the oid is set after 
053     * instantiation.
054     */
055    protected SyntaxChecker()
056    {
057        super( SchemaObjectType.SYNTAX_CHECKER );
058    }
059
060
061    /**
062     * Determines if the attribute's value conforms to the attribute syntax.
063     * 
064     * @param value the value of some attribute with the syntax
065     * @return true if the value is in the valid syntax, false otherwise
066     */
067    public abstract boolean isValidSyntax( Object value );
068
069
070    /**
071     * Asserts whether or not the attribute's value conforms to the attribute
072     * syntax.
073     * 
074     * @param value the value of some attribute with the syntax
075     * @throws LdapException if the value does not conform to the attribute syntax.
076     */
077    public void assertSyntax( Object value ) throws LdapException
078    {
079        if ( !isValidSyntax( value ) )
080        {
081            throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX );
082        }
083    }
084
085
086    /**
087     * Store the SchemaManager in this instance. It may be necessary for some
088     * syntaxChecker which needs to have access to the oidNormalizer Map.
089     *
090     * @param schemaManager the schemaManager to store
091     */
092    public void setSchemaManager( SchemaManager schemaManager )
093    {
094        // Do nothing (general case).
095    }
096
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public boolean equals( Object o )
103    {
104        if ( !super.equals( o ) )
105        {
106            return false;
107        }
108
109        return o instanceof SyntaxChecker;
110    }
111
112
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public String toString()
118    {
119        return objectType + " " + DescriptionUtils.getDescription( this );
120    }
121}