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.parser.init(); // this method MUST be called while we cannot do
62          // constructor overloading for antlr generated parser
63          this.isNormalizing = false;
64      }
65  
66  
67      /**
68       * Creates a normalizing TriggerSpecification parser.
69       *
70       * @param resolver the resolver
71       */
72      public TriggerSpecificationParser( NormalizerMappingResolver<Normalizer> resolver )
73      {
74          this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) );
75          this.parser = new ReusableAntlrTriggerSpecificationParser( lexer );
76  
77          this.parser.setNormalizerMappingResolver( resolver );
78          this.parser.init(); // this method MUST be called while we cannot do
79          // constructor overloading for ANTLR generated parser
80          this.isNormalizing = true;
81      }
82  
83  
84      /**
85       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
86       * pair with it.
87       * 
88       * @param
89       *          spec the specification to be parsed
90       */
91      private synchronized void reset( String spec )
92      {
93          StringReader in = new StringReader( spec );
94          this.lexer.prepareNextInput( in );
95          this.parser.resetState();
96      }
97  
98  
99      /**
100      * Parses an TriggerSpecification without exhausting the parser.
101      * 
102      * @param spec
103      *          the specification to be parsed
104      * @return the specification bean
105      * @throws ParseException
106      *          if there are any recognition errors (bad syntax)
107      */
108     public synchronized TriggerSpecification parse( String spec ) throws ParseException
109     {
110         TriggerSpecification triggerSpecification = null;
111 
112         if ( spec == null || spec.trim().equals( "" ) )
113         {
114             return null;
115         }
116 
117         reset( spec ); // reset and initialize the parser / lexer pair
118 
119         try
120         {
121             triggerSpecification = this.parser.wrapperEntryPoint();
122         }
123         catch ( TokenStreamException e )
124         {
125             String msg = I18n.err( I18n.ERR_04333, spec, e.getLocalizedMessage() );
126             throw new ParseException( msg, 0 );
127         }
128         catch ( RecognitionException e )
129         {
130             String msg = I18n.err( I18n.ERR_04333, spec, e.getLocalizedMessage() );
131             throw new ParseException( msg, e.getColumn() );
132         }
133 
134         return triggerSpecification;
135 
136     }
137 
138 
139     /**
140      * Tests to see if this parser is normalizing.
141      * 
142      * @return true if it normalizes false otherwise
143      */
144     public boolean isNormizing()
145     {
146         return this.isNormalizing;
147     }
148 }