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.model.subtree;
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 LDAP subtree
36   * specification as defined by <a href="http://www.faqs.org/rfcs/rfc3672.html">
37   * RFC 3672</a>. This class enables the reuse of the antlr parser/lexer pair
38   * without having to recreate the pair every time.
39   * 
40   * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class SubtreeSpecificationChecker
44  {
45      /** the antlr generated parser being wrapped */
46      private ReusableAntlrSubtreeSpecificationChecker parser;
47  
48      /** the antlr generated lexer being wrapped */
49      private ReusableAntlrSubtreeSpecificationCheckerLexer lexer;
50  
51  
52      /**
53       * Creates a normalizing subtree specification parser.
54       */
55      public SubtreeSpecificationChecker( SchemaManager schemaManager )
56      {
57          StringReader in = new StringReader( "" ); // place holder for the
58                                                    // first input
59          this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in );
60          this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer );
61          this.parser.init( schemaManager ); // this method MUST be called while we cannot do
62          // constructor overloading for antlr generated parser
63      }
64  
65  
66      /**
67       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
68       * pair with it. param spec the specification to be parsed
69       */
70      private synchronized void reset( String spec )
71      {
72          StringReader in = new StringReader( spec + "end" ); // append end of
73                                                              // input token
74          this.lexer.prepareNextInput( in );
75          this.parser.resetState();
76      }
77  
78  
79      /**
80       * Parses a subtree specification without exhausting the parser.
81       * 
82       * @param spec
83       *            the specification to be parsed
84       * @throws ParseException
85       *             if there are any recognition errors (bad syntax)
86       */
87      public synchronized void parse( String spec ) throws ParseException
88      {
89          if ( spec == null || spec.trim().equals( "" ) )
90          {
91              return;
92          }
93  
94          reset( spec ); // reset and initialize the parser / lexer pair
95  
96          try
97          {
98              this.parser.wrapperEntryPoint();
99          }
100         catch ( TokenStreamException e )
101         {
102             String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
103             throw new ParseException( msg, 0 );
104         }
105         catch ( RecognitionException e )
106         {
107             String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
108             throw new ParseException( msg, e.getColumn() );
109         }
110         catch ( Exception e )
111         {
112             String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
113             throw new ParseException( msg, 0 );
114         }
115     }
116 
117 }