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       * @param schemaManager The SchemaManager
59       */
60      public SubtreeSpecificationParser( SchemaManager schemaManager )
61      {
62          // place holder for the first input
63          StringReader in = new StringReader( "" );
64          this.lexer = new ReusableAntlrSubtreeSpecificationLexer( in );
65          this.parser = new ReusableAntlrSubtreeSpecificationParser( lexer );
66          // this method MUST be called while we cannot do
67          // constructor overloading for antlr generated parser
68          this.parser.init( schemaManager );
69          this.isNormalizing = false;
70      }
71  
72  
73      /**
74       * Creates a normalizing subtree specification parser.
75       * 
76       * @param resolver The resolver to use
77       * @param schemaManager The SchemaManager
78       */
79      public SubtreeSpecificationParser( @SuppressWarnings("rawtypes") NormalizerMappingResolver resolver,
80          SchemaManager schemaManager )
81      {
82          // place holder for the first input
83          StringReader in = new StringReader( "" );
84          this.lexer = new ReusableAntlrSubtreeSpecificationLexer( in );
85          this.parser = new ReusableAntlrSubtreeSpecificationParser( lexer );
86          this.parser.setNormalizerMappingResolver( resolver );
87          // this method MUST be called while we cannot do
88          // constructor overloading for antlr generated parser
89          this.parser.init( schemaManager );
90          this.isNormalizing = true;
91      }
92  
93  
94      /**
95       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
96       * pair with it. param spec the specification to be parsed
97       */
98      private synchronized void reset( String spec )
99      {
100         // append end of input token
101         StringReader in = new StringReader( spec + "end" );
102         this.lexer.prepareNextInput( in );
103         this.parser.resetState();
104     }
105 
106 
107     /**
108      * Parses a subtree specification 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 SubtreeSpecification parse( String spec ) throws ParseException
117     {
118         SubtreeSpecification ss;
119 
120         if ( ( spec == null ) || ( spec.trim().length() == 0 ) )
121         {
122             return null;
123         }
124 
125         // reset and initialize the parser / lexer pair
126         reset( spec );
127 
128         try
129         {
130             ss = this.parser.wrapperEntryPoint();
131         }
132         catch ( TokenStreamException | RecognitionException e )
133         {
134             String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
135             throw new ParseException( msg, 0 );
136         }
137 
138         return ss;
139     }
140 
141 
142     /**
143      * Tests to see if this parser is normalizing.
144      * 
145      * @return true if it normalizes false otherwise
146      */
147     public boolean isNormizing()
148     {
149         return this.isNormalizing;
150     }
151 }