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.parsers;
021
022
023import java.text.ParseException;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.schema.AttributeType;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import antlr.RecognitionException;
031import antlr.TokenStreamException;
032import antlr.TokenStreamRecognitionException;
033
034
035/**
036 * A parser for RFC 4512 attribute type descriptions.
037 * 
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class AttributeTypeDescriptionSchemaParser extends AbstractSchemaParser
041{
042    /** The LoggerFactory used by this class */
043    protected static final Logger LOG = LoggerFactory.getLogger( AttributeTypeDescriptionSchemaParser.class );
044
045
046    /**
047     * Creates a schema parser instance.
048     */
049    public AttributeTypeDescriptionSchemaParser()
050    {
051    }
052
053
054    /**
055     * Parses a attribute type description according to RFC 4512:
056     * 
057     * <pre>
058     * AttributeTypeDescription = LPAREN WSP
059     *     numericoid                    ; object identifier
060     *     [ SP "NAME" SP qdescrs ]      ; short names (descriptors)
061     *     [ SP "DESC" SP qdstring ]     ; description
062     *     [ SP "OBSOLETE" ]             ; not active
063     *     [ SP "SUP" SP oid ]           ; supertype
064     *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
065     *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
066     *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
067     *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
068     *     [ SP "SINGLE-VALUE" ]         ; single-value
069     *     [ SP "COLLECTIVE" ]           ; collective
070     *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
071     *     [ SP "USAGE" SP usage ]       ; usage
072     *     extensions WSP RPAREN         ; extensions
073     * 
074     * usage = "userApplications"     /  ; user
075     *         "directoryOperation"   /  ; directory operational
076     *         "distributedOperation" /  ; DSA-shared operational
077     *         "dSAOperation"            ; DSA-specific operational     
078     * 
079     * extensions = *( SP xstring SP qdstrings )
080     * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
081     * </pre>
082     * 
083     * @param attributeTypeDescription the attribute type description to be parsed
084     * @return the parsed AttributeTypeDescription bean
085     * @throws ParseException if there are any recognition errors (bad syntax)
086     */
087    public synchronized AttributeType parseAttributeTypeDescription( String attributeTypeDescription )
088        throws ParseException
089    {
090
091        LOG.debug( "Parsing an AttributeType : {}", attributeTypeDescription );
092
093        if ( attributeTypeDescription == null )
094        {
095            LOG.error( I18n.err( I18n.ERR_04227 ) );
096            throw new ParseException( "Null", 0 );
097        }
098
099        reset( attributeTypeDescription ); // reset and initialize the parser / lexer pair
100
101        try
102        {
103            AttributeType attributeType = parser.attributeTypeDescription();
104
105            // Update the schemaName
106            updateSchemaName( attributeType );
107
108            return attributeType;
109        }
110        catch ( RecognitionException re )
111        {
112            String msg = I18n.err( I18n.ERR_04228, attributeTypeDescription, re.getMessage(), re.getColumn() );
113            LOG.error( msg );
114            throw new ParseException( msg, re.getColumn() );
115        }
116        catch ( TokenStreamRecognitionException tsre )
117        {
118            String msg = I18n.err( I18n.ERR_04229, attributeTypeDescription, tsre.getMessage() );
119            LOG.error( msg );
120            throw new ParseException( msg, 0 );
121        }
122        catch ( TokenStreamException tse )
123        {
124            String msg = I18n.err( I18n.ERR_04229, attributeTypeDescription, tse.getMessage() );
125            LOG.error( msg );
126            throw new ParseException( msg, 0 );
127        }
128
129    }
130
131
132    /**
133     * Parses a AttributeType description.
134     * 
135     * @param schemaDescription The AttributeType description to parse
136     * @return An instance of AttributeType
137     * @throws ParseException {@inheritDoc}
138     */
139    public AttributeType parse( String schemaDescription ) throws ParseException
140    {
141        return parseAttributeTypeDescription( schemaDescription );
142    }
143}