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    
021    package org.apache.directory.shared.ldap.aci;
022    
023    
024    import java.io.StringReader;
025    import java.text.ParseException;
026    
027    import org.apache.directory.shared.i18n.I18n;
028    
029    import antlr.RecognitionException;
030    import antlr.TokenStreamException;
031    
032    
033    /**
034     * A reusable wrapper around the antlr generated parser for an ACIItem as
035     * defined by X.501. This class enables the reuse of the antlr parser/lexer pair
036     * without having to recreate them every time.
037     * 
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev: 437007 $
040     */
041    public class ACIItemChecker
042    {
043        /** the antlr generated parser being wrapped */
044        private ReusableAntlrACIItemParser checker;
045    
046        /** the antlr generated lexer being wrapped */
047        private ReusableAntlrACIItemLexer lexer;
048    
049        private final boolean isNormalizing;
050    
051    
052        /**
053         * Creates a ACIItem parser.
054         */
055        public ACIItemChecker()
056        {
057            this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) );
058            this.checker = new ReusableAntlrACIItemParser( lexer );
059            this.isNormalizing = false;
060        }
061    
062    
063        /**
064         * Initializes the plumbing by creating a pipe and coupling the parser/lexer
065         * pair with it. param spec the specification to be parsed
066         */
067        private synchronized void reset( String spec )
068        {
069            StringReader in = new StringReader( spec );
070            this.lexer.prepareNextInput( in );
071            this.checker.resetState();
072        }
073    
074    
075        /**
076         * Parses an ACIItem without exhausting the parser.
077         * 
078         * @param spec
079         *            the specification to be parsed
080         * @return the specification bean
081         * @throws ParseException
082         *             if there are any recognition errors (bad syntax)
083         */
084        public synchronized void parse( String spec ) throws ParseException
085        {
086            if ( spec == null || spec.trim().equals( "" ) )
087            {
088                return;
089            }
090    
091            reset( spec ); // reset and initialize the parser / lexer pair
092    
093            try
094            {
095                this.checker.wrapperEntryPoint();
096            }
097            catch ( TokenStreamException e )
098            {
099                throw new ParseException( I18n.err( I18n.ERR_00004, spec, e.getLocalizedMessage() ), 0 );
100            }
101            catch ( RecognitionException e )
102            {
103                throw new ParseException( I18n.err( I18n.ERR_00004, spec, e.getLocalizedMessage() ), e.getColumn() );
104            }
105    
106            return;
107        }
108    
109    
110        /**
111         * Tests to see if this parser is normalizing.
112         * 
113         * @return true if it normalizes false otherwise
114         */
115        public boolean isNormizing()
116        {
117            return this.isNormalizing;
118        }
119    }