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
021package org.apache.directory.api.ldap.model.subtree;
022
023
024import java.io.StringReader;
025import java.text.ParseException;
026
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.model.schema.SchemaManager;
029
030import antlr.RecognitionException;
031import antlr.TokenStreamException;
032
033
034/**
035 * A reusable wrapper around the antlr generated parser for an LDAP subtree
036 * specification as defined by <a href="http://www.faqs.org/rfcs/rfc3672.html">
037 * RFC 3672</a>. This class enables the reuse of the antlr parser/lexer pair
038 * without having to recreate the pair every time.
039 * 
040 * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class SubtreeSpecificationChecker
044{
045    /** the antlr generated parser being wrapped */
046    private ReusableAntlrSubtreeSpecificationChecker parser;
047
048    /** the antlr generated lexer being wrapped */
049    private ReusableAntlrSubtreeSpecificationCheckerLexer lexer;
050
051
052    /**
053     * Creates a normalizing subtree specification parser.
054     */
055    public SubtreeSpecificationChecker( SchemaManager schemaManager )
056    {
057        // place holder for the first input
058        StringReader in = new StringReader( "" );
059        this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in );
060        this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer );
061
062        // this method MUST be called while we cannot do
063        // constructor overloading for antlr generated parser
064        this.parser.init( schemaManager );
065    }
066
067
068    /**
069     * Initializes the plumbing by creating a pipe and coupling the parser/lexer
070     * pair with it. param spec the specification to be parsed
071     */
072    private synchronized void reset( String spec )
073    {
074        // append end of input token
075        StringReader in = new StringReader( spec + "end" );
076        this.lexer.prepareNextInput( in );
077        this.parser.resetState();
078    }
079
080
081    /**
082     * Parses a subtree specification without exhausting the parser.
083     * 
084     * @param spec
085     *            the specification to be parsed
086     * @throws ParseException
087     *             if there are any recognition errors (bad syntax)
088     */
089    public synchronized void parse( String spec ) throws ParseException
090    {
091        if ( spec == null || spec.trim().equals( "" ) )
092        {
093            return;
094        }
095
096        // reset and initialize the parser / lexer pair
097        reset( spec );
098
099        try
100        {
101            this.parser.wrapperEntryPoint();
102        }
103        catch ( TokenStreamException e )
104        {
105            String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
106            throw new ParseException( msg, 0 );
107        }
108        catch ( RecognitionException e )
109        {
110            String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
111            throw new ParseException( msg, e.getColumn() );
112        }
113        catch ( Exception e )
114        {
115            String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
116            throw new ParseException( msg, 0 );
117        }
118    }
119}