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.DitStructureRule;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import antlr.RecognitionException;
031import antlr.TokenStreamException;
032
033
034/**
035 * A parser for RFC 4512 DIT structure rule descriptons
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class DitStructureRuleDescriptionSchemaParser extends AbstractSchemaParser
040{
041    /** The LoggerFactory used by this class */
042    protected static final Logger LOG = LoggerFactory.getLogger( DitStructureRuleDescriptionSchemaParser.class );
043
044
045    /**
046     * Creates a schema parser instance.
047     */
048    public DitStructureRuleDescriptionSchemaParser()
049    {
050        super();
051    }
052
053
054    /**
055     * Parses a DIT structure rule description according to RFC 4512:
056     * 
057     * <pre>
058     * DITStructureRuleDescription = LPAREN WSP
059     *   ruleid                     ; rule identifier
060     *   [ SP "NAME" SP qdescrs ]   ; short names (descriptors)
061     *   [ SP "DESC" SP qdstring ]  ; description
062     *   [ SP "OBSOLETE" ]          ; not active
063     *   SP "FORM" SP oid           ; NameForm
064     *   [ SP "SUP" ruleids ]       ; superior rules
065     *   extensions WSP RPAREN      ; extensions
066     *
067     * ruleids = ruleid / ( LPAREN WSP ruleidlist WSP RPAREN )
068     * ruleidlist = ruleid *( SP ruleid )
069     * ruleid = numbers
070     * </pre>
071     * 
072     * @param ditStructureRuleDescription the DIT structure rule description to be parsed
073     * @return the parsed DITStructureRuleDescription bean
074     * @throws ParseException if there are any recognition errors (bad syntax)
075     */
076    public synchronized DitStructureRule parseDITStructureRuleDescription( String ditStructureRuleDescription )
077        throws ParseException
078    {
079        LOG.debug( "Parsing a DitStructureRule : {}", ditStructureRuleDescription );
080
081        if ( ditStructureRuleDescription == null )
082        {
083            LOG.error( I18n.err( I18n.ERR_04233 ) );
084            throw new ParseException( "Null", 0 );
085        }
086
087        reset( ditStructureRuleDescription ); // reset and initialize the parser / lexer pair
088
089        try
090        {
091            DitStructureRule ditStructureRule = parser.ditStructureRuleDescription();
092
093            // Update the schemaName
094            updateSchemaName( ditStructureRule );
095
096            return ditStructureRule;
097        }
098        catch ( RecognitionException re )
099        {
100            String msg = I18n.err( I18n.ERR_04234, ditStructureRuleDescription, re.getMessage(), re.getColumn() );
101            LOG.error( msg );
102            throw new ParseException( msg, re.getColumn() );
103        }
104        catch ( TokenStreamException tse )
105        {
106            String msg = I18n.err( I18n.ERR_04235, ditStructureRuleDescription, tse.getMessage() );
107            LOG.error( msg );
108            throw new ParseException( msg, 0 );
109        }
110
111    }
112
113
114    /**
115     * Parses a DitStructureRule description.
116     * 
117     * @param schemaDescription The DitStructureRule description to parse
118     * @return An instance of DitStructureRule
119     * @throws ParseException {@inheritDoc}
120     */
121    public DitStructureRule parse( String schemaDescription ) throws ParseException
122    {
123        return parseDITStructureRuleDescription( schemaDescription );
124    }
125}