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