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  
30  import antlr.RecognitionException;
31  import antlr.TokenStreamException;
32  
33  
34  /**
35   * A reusable wrapper around the antlr generated parser for an ACIItem as
36   * defined by X.501. This class enables the reuse of the antlr parser/lexer pair
37   * without having to recreate them every time.
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class ACIItemChecker
42  {
43      /** the antlr generated parser being wrapped */
44      private ReusableAntlrACIItemParser checker;
45  
46      /** the antlr generated lexer being wrapped */
47      private ReusableAntlrACIItemLexer lexer;
48  
49  
50      /**
51       * Creates a ACIItem parser.
52       *
53       * @param schemaManager the schema manager
54       */
55      public ACIItemChecker( SchemaManager schemaManager )
56      {
57          this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) );
58          this.checker = new ReusableAntlrACIItemParser( lexer );
59          this.checker.init( schemaManager );
60      }
61  
62  
63      /**
64       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
65       * pair with it. param spec the specification to be parsed
66       */
67      private synchronized void reset( String spec )
68      {
69          StringReader in = new StringReader( spec );
70          this.lexer.prepareNextInput( in );
71          this.checker.resetState();
72      }
73  
74  
75      /**
76       * Parses an ACIItem without exhausting the parser.
77       * 
78       * @param spec
79       *            the specification to be parsed
80       * @throws ParseException
81       *             if there are any recognition errors (bad syntax)
82       */
83      public synchronized void parse( String spec ) throws ParseException
84      {
85          if ( spec == null || spec.trim().equals( "" ) )
86          {
87              return;
88          }
89  
90          reset( spec ); // reset and initialize the parser / lexer pair
91  
92          try
93          {
94              this.checker.wrapperEntryPoint();
95          }
96          catch ( TokenStreamException e )
97          {
98              throw new ParseException( I18n
99                  .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), 0 );
100         }
101         catch ( RecognitionException e )
102         {
103             throw new ParseException( I18n
104                 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), e.getColumn() );
105         }
106     }
107 
108 }