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.ObjectClass;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import antlr.RecognitionException;
031import antlr.TokenStreamException;
032
033
034/**
035 * A parser for RFC 4512 object class descriptons
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class ObjectClassDescriptionSchemaParser extends AbstractSchemaParser
040{
041    /** The LoggerFactory used by this class */
042    protected static final Logger LOG = LoggerFactory.getLogger( ObjectClassDescriptionSchemaParser.class );
043
044
045    /**
046     * Creates a schema parser instance.
047     */
048    public ObjectClassDescriptionSchemaParser()
049    {
050        // Nothing to do
051    }
052
053
054    /**
055     * Parses a object class definition according to RFC 4512:
056     * 
057     * <pre>
058     * ObjectClassDescription = 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 oids ]       ; superior object classes
064     *     [ SP kind ]                ; kind of class
065     *     [ SP "MUST" SP oids ]      ; attribute types
066     *     [ SP "MAY" SP oids ]       ; attribute types
067     *     extensions WSP RPAREN
068     *
069     * kind = "ABSTRACT" / "STRUCTURAL" / "AUXILIARY"
070     * 
071     * extensions = *( SP xstring SP qdstrings )
072     * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
073     * </pre>
074     * 
075     * @param objectClassDescription the object class description to be parsed
076     * @return the parsed ObjectClassDescription bean
077     * @throws ParseException if there are any recognition errors (bad syntax)
078     */
079    public synchronized ObjectClass parseObjectClassDescription( String objectClassDescription )
080        throws ParseException
081    {
082        LOG.debug( "Parsing an ObjectClass : {}", objectClassDescription );
083
084        if ( objectClassDescription == null )
085        {
086            LOG.error( I18n.err( I18n.ERR_04254 ) );
087            throw new ParseException( "Null", 0 );
088        }
089
090        reset( objectClassDescription ); // reset and initialize the parser / lexer pair
091
092        try
093        {
094            ObjectClass objectClass = parser.objectClassDescription();
095
096            // Update the schemaName
097            updateSchemaName( objectClass );
098
099            return objectClass;
100        }
101        catch ( RecognitionException re )
102        {
103            String msg = I18n.err( I18n.ERR_04255, objectClassDescription, re.getMessage(), re.getColumn() );
104            LOG.error( msg );
105            throw new ParseException( msg, re.getColumn() );
106        }
107        catch ( TokenStreamException tse )
108        {
109            String msg = I18n.err( I18n.ERR_04256, objectClassDescription, tse.getMessage() );
110            LOG.error( msg );
111            throw new ParseException( msg, 0 );
112        }
113
114    }
115
116
117    /**
118     * Parses a ObjectClass description.
119     * 
120     * @param schemaDescription The ObjectClass description to parse
121     * @return An instance of ObjectClass
122     * @throws ParseException {@inheritDoc}
123     */
124    public ObjectClass parse( String schemaDescription ) throws ParseException
125    {
126        return parseObjectClassDescription( schemaDescription );
127    }
128
129}