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