View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.api.ldap.trigger;
22  
23  
24  import java.io.StringReader;
25  import java.text.ParseException;
26  
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.model.schema.Normalizer;
29  import org.apache.directory.api.ldap.model.schema.NormalizerMappingResolver;
30  
31  import antlr.RecognitionException;
32  import antlr.TokenStreamException;
33  
34  
35  /**
36   * A reusable wrapper around the ANTLR generated parser for a
37   * TriggerSpecification. This class enables the reuse of the antlr parser/lexer
38   * pair without having to recreate them every time.
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class TriggerSpecificationParser
43  {
44      /** the antlr generated parser being wrapped */
45      private ReusableAntlrTriggerSpecificationParser parser;
46  
47      /** the antlr generated lexer being wrapped */
48      private ReusableAntlrTriggerSpecificationLexer lexer;
49  
50      private final boolean isNormalizing;
51  
52  
53      /**
54       * Creates a TriggerSpecification parser.
55       */
56      public TriggerSpecificationParser()
57      {
58          this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) );
59          this.parser = new ReusableAntlrTriggerSpecificationParser( lexer );
60  
61          // this method MUST be called while we cannot do
62          // constructor overloading for antlr generated parser
63          this.parser.init();
64          this.isNormalizing = false;
65      }
66  
67  
68      /**
69       * Creates a normalizing TriggerSpecification parser.
70       *
71       * @param resolver the resolver
72       */
73      public TriggerSpecificationParser( NormalizerMappingResolver<Normalizer> resolver )
74      {
75          this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) );
76          this.parser = new ReusableAntlrTriggerSpecificationParser( lexer );
77  
78          this.parser.setNormalizerMappingResolver( resolver );
79          // this method MUST be called while we cannot do
80          // constructor overloading for ANTLR generated parser
81          this.parser.init();
82          this.isNormalizing = true;
83      }
84  
85  
86      /**
87       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
88       * pair with it.
89       * 
90       * @param
91       *          spec the specification to be parsed
92       */
93      private synchronized void reset( String spec )
94      {
95          StringReader in = new StringReader( spec );
96          this.lexer.prepareNextInput( in );
97          this.parser.resetState();
98      }
99  
100 
101     /**
102      * Parses an TriggerSpecification without exhausting the parser.
103      * 
104      * @param spec
105      *          the specification to be parsed
106      * @return the specification bean
107      * @throws ParseException
108      *          if there are any recognition errors (bad syntax)
109      */
110     public synchronized TriggerSpecification parse( String spec ) throws ParseException
111     {
112         TriggerSpecification triggerSpecification = null;
113 
114         if ( spec == null || spec.trim().equals( "" ) )
115         {
116             return null;
117         }
118 
119         // reset and initialize the parser / lexer pair
120         reset( spec );
121 
122         try
123         {
124             triggerSpecification = this.parser.wrapperEntryPoint();
125         }
126         catch ( TokenStreamException e )
127         {
128             String msg = I18n.err( I18n.ERR_04333, spec, e.getLocalizedMessage() );
129             throw new ParseException( msg, 0 );
130         }
131         catch ( RecognitionException e )
132         {
133             String msg = I18n.err( I18n.ERR_04333, spec, e.getLocalizedMessage() );
134             throw new ParseException( msg, e.getColumn() );
135         }
136 
137         return triggerSpecification;
138 
139     }
140 
141 
142     /**
143      * Tests to see if this parser is normalizing.
144      * 
145      * @return true if it normalizes false otherwise
146      */
147     public boolean isNormizing()
148     {
149         return this.isNormalizing;
150     }
151 }