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 java.text.ParseException;
024
025import org.apache.directory.api.ldap.model.constants.SchemaConstants;
026import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
027import org.apache.directory.api.ldap.model.schema.parsers.AttributeTypeDescriptionSchemaParser;
028import org.apache.directory.api.util.Strings;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032
033/**
034 * A SyntaxChecker which verifies that a value follows the
035 * attribute type descripton syntax according to RFC 4512, par 4.2.2:
036 * 
037*  <pre>
038 * AttributeTypeDescription = LPAREN WSP
039 *     numericoid                    ; object identifier
040 *     [ SP "NAME" SP qdescrs ]      ; short names (descriptors)
041 *     [ SP "DESC" SP qdstring ]     ; description
042 *     [ SP "OBSOLETE" ]             ; not active
043 *     [ SP "SUP" SP oid ]           ; supertype
044 *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
045 *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
046 *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
047 *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
048 *     [ SP "SINGLE-VALUE" ]         ; single-value
049 *     [ SP "COLLECTIVE" ]           ; collective
050 *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
051 *     [ SP "USAGE" SP usage ]       ; usage
052 *     extensions WSP RPAREN         ; extensions
053 * 
054 * usage = "userApplications"     /  ; user
055 *         "directoryOperation"   /  ; directory operational
056 *         "distributedOperation" /  ; DSA-shared operational
057 *         "dSAOperation"            ; DSA-specific operational     
058 * 
059 * extensions = *( SP xstring SP qdstrings )
060 * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
061 * 
062 * Each attribute type description must contain at least one of the SUP
063 * or SYNTAX fields. 
064 * 
065 * COLLECTIVE requires usage userApplications.
066 * 
067 * NO-USER-MODIFICATION requires an operational usage.
068 * 
069 * 
070 * </pre>
071 * 
072 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
073 */
074@SuppressWarnings("serial")
075public class AttributeTypeDescriptionSyntaxChecker extends SyntaxChecker
076{
077    /** A logger for this class */
078    private static final Logger LOG = LoggerFactory.getLogger( AttributeTypeDescriptionSyntaxChecker.class );
079
080    /** The schema parser used to parse the AttributeTypeDescription Syntax */
081    private AttributeTypeDescriptionSchemaParser schemaParser = new AttributeTypeDescriptionSchemaParser();
082
083
084    /**
085     * 
086     * Creates a new instance of AttributeTypeDescriptionSchemaParser.
087     *
088     */
089    public AttributeTypeDescriptionSyntaxChecker()
090    {
091        super( SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX );
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    public boolean isValidSyntax( Object value )
099    {
100        String strValue = null;
101
102        if ( value == null )
103        {
104            LOG.debug( "Syntax invalid for 'null'" );
105            return false;
106        }
107
108        if ( value instanceof String )
109        {
110            strValue = ( String ) value;
111        }
112        else if ( value instanceof byte[] )
113        {
114            strValue = Strings.utf8ToString( ( byte[] ) value );
115        }
116        else
117        {
118            strValue = value.toString();
119        }
120
121        try
122        {
123            schemaParser.parseAttributeTypeDescription( strValue );
124            LOG.debug( "Syntax valid for '{}'", value );
125            return true;
126        }
127        catch ( ParseException pe )
128        {
129            LOG.debug( "Syntax invalid for '{}'", value );
130            return false;
131        }
132    }
133}