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