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.LdapSyntax;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import antlr.RecognitionException;
031import antlr.TokenStreamException;
032
033
034/**
035 * A parser for RFC 4512 LDAP syntx descriptions.
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class LdapSyntaxDescriptionSchemaParser extends AbstractSchemaParser
040{
041    /** The LoggerFactory used by this class */
042    protected static final Logger LOG = LoggerFactory.getLogger( LdapSyntaxDescriptionSchemaParser.class );
043
044
045    /**
046     * Creates a schema parser instance.
047     */
048    public LdapSyntaxDescriptionSchemaParser()
049    {
050    }
051
052
053    /**
054     * Parses a LDAP syntax description according to RFC 4512:
055     * 
056     * <pre>
057     * SyntaxDescription = LPAREN WSP
058     *    numericoid                 ; object identifier
059     *    [ SP "DESC" SP qdstring ]  ; description
060     *    extensions WSP RPAREN      ; extensions
061     * </pre>
062     * 
063     * @param ldapSyntaxDescription the LDAP syntax description to be parsed
064     * @return the parsed LdapSyntax bean
065     * @throws ParseException if there are any recognition errors (bad syntax)
066     */
067    public synchronized LdapSyntax parseLdapSyntaxDescription( String ldapSyntaxDescription )
068        throws ParseException
069    {
070        LOG.debug( "Parsing a LdapSyntax : {}", ldapSyntaxDescription );
071
072        if ( ldapSyntaxDescription == null )
073        {
074            LOG.error( I18n.err( I18n.ERR_04239 ) );
075            throw new ParseException( "Null", 0 );
076        }
077
078        reset( ldapSyntaxDescription ); // reset and initialize the parser / lexer pair
079
080        try
081        {
082            LdapSyntax ldapSyntax = parser.ldapSyntaxDescription();
083            ldapSyntax.setSpecification( ldapSyntaxDescription );
084            
085            // Update the schemaName
086            updateSchemaName( ldapSyntax );
087
088            return ldapSyntax;
089        }
090        catch ( RecognitionException re )
091        {
092            String msg = I18n.err( I18n.ERR_04240, ldapSyntaxDescription, re.getMessage(), re.getColumn() );
093            LOG.error( msg );
094            throw new ParseException( msg, re.getColumn() );
095        }
096        catch ( TokenStreamException tse )
097        {
098            String msg = I18n.err( I18n.ERR_04241, ldapSyntaxDescription, tse.getMessage() );
099            LOG.error( msg );
100            throw new ParseException( msg, 0 );
101        }
102    }
103
104
105    /**
106     * Parses a LdapSyntax description.
107     * 
108     * @param schemaDescription The LdapSyntax description to parse
109     * @return An instance of LdapSyntax
110     * @throws ParseException {@inheritDoc}
111     */
112    public LdapSyntax parse( String schemaDescription ) throws ParseException
113    {
114        return parseLdapSyntaxDescription( schemaDescription );
115    }
116}