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.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    import antlr.RecognitionException;
030    import antlr.TokenStreamException;
031    
032    
033    /**
034     * A parser for ApacheDS comparator descriptions.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev$, $Date$
038     */
039    public class LdapComparatorDescriptionSchemaParser extends AbstractSchemaParser
040    {
041        /** The LoggerFactory used by this class */
042        protected static final Logger LOG = LoggerFactory.getLogger( LdapComparatorDescriptionSchemaParser.class );
043    
044        /**
045         * Creates a schema parser instance.
046         */
047        public LdapComparatorDescriptionSchemaParser()
048        {
049            super();
050        }
051    
052    
053        /**
054         * Parses an comparator description:
055         * 
056         * <pre>
057         * ComparatorDescription = LPAREN WSP
058         *     numericoid                           ; object identifier
059         *     [ SP "DESC" SP qdstring ]            ; description
060         *     SP "FQCN" SP fqcn                    ; fully qualified class name
061         *     [ SP "BYTECODE" SP base64 ]          ; optional base64 encoded bytecode
062         *     extensions WSP RPAREN                ; extensions
063         * 
064         * base64          = *(4base64-char)
065         * base64-char     = ALPHA / DIGIT / "+" / "/"
066         * fqcn = fqcnComponent 1*( DOT fqcnComponent )
067         * fqcnComponent = ???
068         * 
069         * extensions = *( SP xstring SP qdstrings )
070         * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
071         * </pre>
072         * 
073         * @param comparatorDescription the comparator description to be parsed
074         * @return the parsed ComparatorDescription bean
075         * @throws ParseException if there are any recognition errors (bad syntax)
076         */
077        public LdapComparatorDescription parseComparatorDescription( String comparatorDescription )
078            throws ParseException
079        {
080            LOG.debug( "Parsing a Comparator : {}", comparatorDescription );
081            
082            if ( comparatorDescription == null )
083            {
084                LOG.error( I18n.err( I18n.ERR_04236 ) );
085                throw new ParseException( "Null", 0 );
086            }
087    
088            synchronized ( parser )
089            {
090                reset( comparatorDescription ); // reset and initialize the parser / lexer pair
091        
092                try
093                {
094                    LdapComparatorDescription ldapComparatorDescription = parser.ldapComparator();
095                    LOG.debug( "Parsed a LdapComparator : {}", ldapComparatorDescription );
096                    
097    
098                    // Update the schemaName
099                    setSchemaName( ldapComparatorDescription );
100    
101                    return ldapComparatorDescription;
102                }
103                catch ( RecognitionException re )
104                {
105                    String msg = I18n.err( I18n.ERR_04273, comparatorDescription, re.getMessage(), re.getColumn() );
106                    LOG.error( msg );
107                    throw new ParseException( msg, re.getColumn() );
108                }
109                catch ( TokenStreamException tse )
110                {
111                    String msg = I18n.err( I18n.ERR_04238, comparatorDescription, tse.getMessage() );
112                    LOG.error( msg );
113                    throw new ParseException( msg, 0 );
114                }
115            }
116        }
117    
118    
119        /**
120         * Parses a LdapComparator description
121         * 
122         * @param The LdapComparator description to parse
123         * @return An instance of LdapComparatorDescription
124         */
125        public LdapComparatorDescription parse( String schemaDescription ) throws ParseException
126        {
127            return parseComparatorDescription( schemaDescription );
128        }
129    }