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.MatchingRuleUse;
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 matching rule use descriptions.
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$, $Date$
039     */
040    public class MatchingRuleUseDescriptionSchemaParser extends AbstractSchemaParser
041    {
042        /** The LoggerFactory used by this class */
043        protected static final Logger LOG = LoggerFactory.getLogger( MatchingRuleUseDescriptionSchemaParser.class );
044    
045    
046        /**
047         * Creates a schema parser instance.
048         */
049        public MatchingRuleUseDescriptionSchemaParser()
050        {
051        }
052    
053    
054        /**
055         * Parses a matching rule use description according to RFC 4512:
056         * 
057         * <pre>
058         * MatchingRuleUseDescription = 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 "APPLIES" SP oids       ; attribute types
064         *    extensions WSP RPAREN      ; extensions
065         * 
066         * extensions = *( SP xstring SP qdstrings )
067         * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
068         * </pre>
069         * 
070         * @param matchingRuleUseDescription the matching rule use description to be parsed
071         * @return the parsed MatchingRuleUseDescription bean
072         * @throws ParseException if there are any recognition errors (bad syntax)
073         */
074        public synchronized MatchingRuleUse parseMatchingRuleUseDescription( String matchingRuleUseDescription )
075            throws ParseException
076        {
077            LOG.debug( "Parsing a MatchingRuleUse : {}", matchingRuleUseDescription );
078    
079            if ( matchingRuleUseDescription == null )
080            {
081                LOG.error( I18n.err( I18n.ERR_04245 ) );
082                throw new ParseException( "Null", 0 );
083            }
084    
085            reset( matchingRuleUseDescription ); // reset and initialize the parser / lexer pair
086    
087            try
088            {
089                MatchingRuleUse matchingRuleUse = parser.matchingRuleUseDescription();
090                
091                // Update the schemaName
092                setSchemaName( matchingRuleUse );
093    
094                return matchingRuleUse;
095            }
096            catch ( RecognitionException re )
097            {
098                String msg = I18n.err( I18n.ERR_04246, matchingRuleUseDescription, re.getMessage(), re.getColumn() );
099                LOG.error( msg );
100                throw new ParseException( msg, re.getColumn() );
101            }
102            catch ( TokenStreamException tse )
103            {
104                String msg = I18n.err( I18n.ERR_04247, matchingRuleUseDescription, tse.getMessage() );
105                LOG.error( msg );
106                throw new ParseException( msg, 0 );
107            }
108    
109        }
110    
111    
112        /**
113         * Parses a MatchingRuleUse description
114         * 
115         * @param The MatchingRuleUse description to parse
116         * @return An instance of MatchingRuleUse
117         */
118        public MatchingRuleUse parse( String schemaDescription ) throws ParseException
119        {
120            return parseMatchingRuleUseDescription( schemaDescription );
121        }
122    }