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