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    
026    import antlr.RecognitionException;
027    import antlr.TokenStreamException;
028    
029    
030    /**
031     * A parser for ApacheDS normalizer descriptions.
032     * 
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev$, $Date$
035     */
036    public class NormalizerDescriptionSchemaParser extends AbstractSchemaParser
037    {
038    
039        /**
040         * Creates a schema parser instance.
041         */
042        public NormalizerDescriptionSchemaParser()
043        {
044        }
045    
046    
047        /**
048         * Parses a normalizer description:
049         * 
050         * <pre>
051         * NormalizerDescription = LPAREN WSP
052         *     numericoid                           ; object identifier
053         *     [ SP "DESC" SP qdstring ]            ; description
054         *     SP "FQCN" SP fqcn                    ; fully qualified class name
055         *     [ SP "BYTECODE" SP base64 ]          ; optional base64 encoded bytecode
056         *     extensions WSP RPAREN                ; extensions
057         * 
058         * base64          = *(4base64-char)
059         * base64-char     = ALPHA / DIGIT / "+" / "/"
060         * fqcn = fqcnComponent 1*( DOT fqcnComponent )
061         * fqcnComponent = ???
062         * 
063         * extensions = *( SP xstring SP qdstrings )
064         * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
065         * </pre>
066         * 
067         * @param normalizerDescription the normalizer description to be parsed
068         * @return the parsed NormalizerDescription bean
069         * @throws ParseException if there are any recognition errors (bad syntax)
070         */
071        public synchronized NormalizerDescription parseNormalizerDescription( String normalizerDescription )
072            throws ParseException
073        {
074    
075            if ( normalizerDescription == null )
076            {
077                throw new ParseException( "Null", 0 );
078            }
079    
080            reset( normalizerDescription ); // reset and initialize the parser / lexer pair
081    
082            try
083            {
084                NormalizerDescription nd = parser.normalizerDescription();
085                return nd;
086            }
087            catch ( RecognitionException re )
088            {
089                String msg = "Parser failure on normalizer description:\n\t" + normalizerDescription;
090                msg += "\nAntlr message: " + re.getMessage();
091                msg += "\nAntlr column: " + re.getColumn();
092                throw new ParseException( msg, re.getColumn() );
093            }
094            catch ( TokenStreamException tse )
095            {
096                String msg = "Parser failure on normalizer description:\n\t" + normalizerDescription;
097                msg += "\nAntlr message: " + tse.getMessage();
098                throw new ParseException( msg, 0 );
099            }
100    
101        }
102    
103    
104        public AbstractSchemaDescription parse( String schemaDescription ) throws ParseException
105        {
106            return parseNormalizerDescription( schemaDescription );
107        }
108    
109    }