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    import java.io.Reader;
023    
024    import org.apache.directory.shared.ldap.schema.syntax.AntlrSchemaLexer;
025    
026    import antlr.CharBuffer;
027    import antlr.LexerSharedInputState;
028    
029    /**
030     * A reusable lexer class extended from antlr generated lexer for an LDAP
031     * schema as defined in RFC 4512. This class
032     * enables the reuse of the antlr lexer without having to recreate the it every
033     * time as stated in <a
034     * href="http://www.antlr.org:8080/pipermail/antlr-interest/2003-April/003631.html">
035     * a Antlr Interest Group mail</a> .
036     * 
037     * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
038     * @version $Rev$, $Date$
039     */
040    public class ReusableAntlrSchemaLexer extends AntlrSchemaLexer
041    {
042        private boolean savedCaseSensitive;
043    
044        private boolean savedCaseSensitiveLiterals;
045    
046    
047        /**
048         * Creates a ReusableAntlrSchemaLexer instance.
049         * 
050         * @param in
051         *            the input to the lexer
052         */
053        public ReusableAntlrSchemaLexer( Reader in )
054        {
055            super( in );
056            savedCaseSensitive = getCaseSensitive();
057            savedCaseSensitiveLiterals = getCaseSensitiveLiterals();
058        }
059    
060    
061        /**
062         * Resets the state of an antlr lexer and initializes it with new input.
063         * 
064         * @param in
065         *            the input to the lexer
066         */
067        public void prepareNextInput( Reader in )
068        {
069            CharBuffer buf = new CharBuffer( in );
070            LexerSharedInputState state = new LexerSharedInputState( buf );
071            this.setInputState( state );
072    
073            this.setCaseSensitive( savedCaseSensitive );
074    
075            // no set method for this protected field.
076            this.caseSensitiveLiterals = savedCaseSensitiveLiterals;
077        }
078    }