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     * @param schemaManager The SchemaManager
056     */
057    public SubtreeSpecificationChecker( SchemaManager schemaManager )
058    {
059        // place holder for the first input
060        StringReader in = new StringReader( "" );
061        this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in );
062        this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer );
063
064        // this method MUST be called while we cannot do
065        // constructor overloading for antlr generated parser
066        this.parser.init( schemaManager );
067    }
068
069
070    /**
071     * Initializes the plumbing by creating a pipe and coupling the parser/lexer
072     * pair with it. param spec the specification to be parsed
073     * 
074     * @param spec The specification to parse
075     */
076    private synchronized void reset( String spec )
077    {
078        // append end of input token
079        StringReader in = new StringReader( spec + "end" );
080        this.lexer.prepareNextInput( in );
081        this.parser.resetState();
082    }
083
084
085    /**
086     * Parses a subtree specification without exhausting the parser.
087     * 
088     * @param spec the specification to be parsed
089     * @throws ParseException if there are any recognition errors (bad syntax)
090     */
091    public synchronized void parse( String spec ) throws ParseException
092    {
093        if ( ( spec == null ) || ( spec.trim().length() == 0 ) )
094        {
095            return;
096        }
097
098        // reset and initialize the parser / lexer pair
099        reset( spec );
100
101        try
102        {
103            this.parser.wrapperEntryPoint();
104        }
105        catch ( TokenStreamException | RecognitionException e )
106        {
107            String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
108            throw new ParseException( msg, 0 );
109        }
110    }
111}