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