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.trigger;
022    
023    
024    import java.io.StringReader;
025    import java.text.ParseException;
026    
027    import org.apache.directory.shared.ldap.schema.NormalizerMappingResolver;
028    
029    import antlr.RecognitionException;
030    import antlr.TokenStreamException;
031    
032    
033    /**
034     * A reusable wrapper around the ANTLR generated parser for a
035     * TriggerSpecification. This class enables the reuse of the antlr parser/lexer
036     * pair without having to recreate them every time.
037     * 
038     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039     * @version $Rev:$, $Date:$
040     */
041    public class TriggerSpecificationParser
042    {
043        /** the antlr generated parser being wrapped */
044        private ReusableAntlrTriggerSpecificationParser parser;
045    
046        /** the antlr generated lexer being wrapped */
047        private ReusableAntlrTriggerSpecificationLexer lexer;
048    
049        private final boolean isNormalizing;
050    
051    
052        /**
053         * Creates a TriggerSpecification parser.
054         */
055        public TriggerSpecificationParser()
056        {
057            this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) );
058            this.parser = new ReusableAntlrTriggerSpecificationParser( lexer );
059    
060            this.parser.init(); // this method MUST be called while we cannot do
061            // constructor overloading for antlr generated parser
062            this.isNormalizing = false;
063        }
064    
065    
066        /**
067         * Creates a normalizing TriggerSpecification parser.
068         */
069        public TriggerSpecificationParser( NormalizerMappingResolver resolver )
070        {
071            this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) );
072            this.parser = new ReusableAntlrTriggerSpecificationParser( lexer );
073    
074            this.parser.setNormalizerMappingResolver( resolver );
075            this.parser.init(); // this method MUST be called while we cannot do
076            // constructor overloading for ANTLR generated parser
077            this.isNormalizing = true;
078        }
079    
080    
081        /**
082         * Initializes the plumbing by creating a pipe and coupling the parser/lexer
083         * pair with it.
084         * 
085         * @param
086         *          spec the specification to be parsed
087         */
088        private synchronized void reset( String spec )
089        {
090            StringReader in = new StringReader( spec );
091            this.lexer.prepareNextInput( in );
092            this.parser.resetState();
093        }
094    
095    
096        /**
097         * Parses an TriggerSpecification without exhausting the parser.
098         * 
099         * @param spec
100         *          the specification to be parsed
101         * @return the specification bean
102         * @throws ParseException
103         *          if there are any recognition errors (bad syntax)
104         */
105        public synchronized TriggerSpecification parse( String spec ) throws ParseException
106        {
107            TriggerSpecification triggerSpecification = null;
108    
109            if ( spec == null || spec.trim().equals( "" ) )
110            {
111                return null;
112            }
113    
114            reset( spec ); // reset and initialize the parser / lexer pair
115    
116            try
117            {
118                triggerSpecification = this.parser.wrapperEntryPoint();
119            }
120            catch ( TokenStreamException e )
121            {
122                String msg = "Parser failure on Trigger Specification:\n\t" + spec;
123                msg += "\nAntlr exception trace:\n" + e.getMessage();
124                throw new ParseException( msg, 0 );
125            }
126            catch ( RecognitionException e )
127            {
128                String msg = "Parser failure on Trigger Specification:\n\t" + spec;
129                msg += "\nAntlr exception trace:\n" + e.getMessage();
130                throw new ParseException( msg, e.getColumn() );
131            }
132            
133            return triggerSpecification;
134    
135        }
136    
137    
138        /**
139         * Tests to see if this parser is normalizing.
140         * 
141         * @return true if it normalizes false otherwise
142         */
143        public boolean isNormizing()
144        {
145            return this.isNormalizing;
146        }
147    }