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   *    https://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.aci;
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.SchemaManager;
29  import org.apache.directory.api.ldap.model.schema.normalizers.NameComponentNormalizer;
30  import org.apache.directory.api.util.StringConstants;
31  
32  import antlr.RecognitionException;
33  import antlr.TokenStreamException;
34  
35  
36  /**
37   * A reusable wrapper around the antlr generated parser for an ACIItem as
38   * defined by X.501. This class enables the reuse of the antlr parser/lexer pair
39   * without having to recreate them every time.
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class ACIItemParser
44  {
45      /** the antlr generated parser being wrapped */
46      private ReusableAntlrACIItemParser parser;
47  
48      /** the antlr generated lexer being wrapped */
49      private ReusableAntlrACIItemLexer lexer;
50  
51      /** The is normalizing flag. */
52      private final boolean isNormalizing;
53  
54  
55      /**
56       * Creates a ACIItem parser.
57       *
58       * @param schemaManager the schema manager
59       */
60      public ACIItemParser( SchemaManager schemaManager )
61      {
62          this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) );
63          this.parser = new ReusableAntlrACIItemParser( lexer );
64  
65          // this method MUST be called while we cannot do
66          // constructor overloading for antlr generated parser
67          this.parser.init( schemaManager );
68  
69          this.isNormalizing = false;
70      }
71  
72  
73      /**
74       * Creates a normalizing ACIItem parser.
75       *
76       * @param normalizer the normalizer
77       * @param schemaManager the schema manager
78       */
79      public ACIItemParser( NameComponentNormalizer normalizer, SchemaManager schemaManager )
80      {
81          this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) );
82          this.parser = new ReusableAntlrACIItemParser( lexer );
83  
84          this.parser.setNormalizer( normalizer );
85          this.isNormalizing = true;
86  
87          // this method MUST be called while we cannot do
88          // constructor overloading for antlr generated parser
89          this.parser.init( schemaManager );
90      }
91  
92  
93      /**
94       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
95       * pair with it. 
96       * 
97       * @param spec the specification to be parsed
98       */
99      private synchronized void reset( String spec )
100     {
101         StringReader in = new StringReader( spec );
102         this.lexer.prepareNextInput( in );
103         this.parser.resetState();
104     }
105 
106 
107     /**
108      * Parses an ACIItem without exhausting the parser.
109      * 
110      * @param spec
111      *            the specification to be parsed
112      * @return the specification bean
113      * @throws ParseException
114      *             if there are any recognition errors (bad syntax)
115      */
116     public synchronized ACIItem parse( String spec ) throws ParseException
117     {
118         ACIItem aCIItem;
119 
120         if ( ( spec == null ) || StringConstants.EMPTY.equals( spec.trim() ) )
121         {
122             return null;
123         }
124 
125         // reset and initialize the parser / lexer pair
126         reset( spec );
127 
128         try
129         {
130             aCIItem = this.parser.wrapperEntryPoint();
131         }
132         catch ( TokenStreamException e )
133         {
134             throw new ParseException( I18n
135                 .err( I18n.ERR_07004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), 0 );
136         }
137         catch ( RecognitionException e )
138         {
139             throw new ParseException(
140                 I18n
141                     .err( I18n.ERR_07004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage(), e.getLine(),
142                         e.getColumn() ), e.getColumn() );
143         }
144 
145         return aCIItem;
146     }
147 
148 
149     /**
150      * Tests to see if this parser is normalizing.
151      * 
152      * @return true if it normalizes false otherwise
153      */
154     public boolean isNormizing()
155     {
156         return this.isNormalizing;
157     }
158 }