View Javadoc
1   /*
2   
3   
4    *  Licensed to the Apache Software Foundation (ASF) under one
5    *  or more contributor license agreements.  See the NOTICE file
6    *  distributed with this work for additional information
7    *  regarding copyright ownership.  The ASF licenses this file
8    *  to you under the Apache License, Version 2.0 (the
9    *  "License"); you may not use this file except in compliance
10   *  with the License.  You may obtain a copy of the License at
11   *  
12   *    https://www.apache.org/licenses/LICENSE-2.0
13   *  
14   *  Unless required by applicable law or agreed to in writing,
15   *  software distributed under the License is distributed on an
16   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   *  KIND, either express or implied.  See the License for the
18   *  specific language governing permissions and limitations
19   *  under the License. 
20   *  
21   */
22  
23  package org.apache.directory.api.ldap.model.subtree;
24  
25  
26  import java.io.StringReader;
27  import java.text.ParseException;
28  
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.model.schema.NormalizerMappingResolver;
31  import org.apache.directory.api.ldap.model.schema.SchemaManager;
32  import org.apache.directory.api.util.Strings;
33  
34  import antlr.RecognitionException;
35  import antlr.TokenStreamException;
36  
37  
38  /**
39   * A reusable wrapper around the antlr generated parser for an LDAP subtree
40   * specification as defined by <a href="http://www.faqs.org/rfcs/rfc3672.html">
41   * RFC 3672</a>. This class enables the reuse of the antlr parser/lexer pair
42   * without having to recreate the pair every time.
43   * 
44   * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  public class SubtreeSpecificationParser
48  {
49      /** the antlr generated parser being wrapped */
50      private ReusableAntlrSubtreeSpecificationParser parser;
51  
52      /** the antlr generated lexer being wrapped */
53      private ReusableAntlrSubtreeSpecificationLexer lexer;
54  
55      private final boolean isNormalizing;
56  
57  
58      /**
59       * Creates a subtree specification parser.
60       * 
61       * @param schemaManager The SchemaManager
62       */
63      public SubtreeSpecificationParser( SchemaManager schemaManager )
64      {
65          // place holder for the first input
66          StringReader in = new StringReader( "" );
67          this.lexer = new ReusableAntlrSubtreeSpecificationLexer( in );
68          this.parser = new ReusableAntlrSubtreeSpecificationParser( lexer );
69          // this method MUST be called while we cannot do
70          // constructor overloading for antlr generated parser
71          this.parser.init( schemaManager );
72          this.isNormalizing = false;
73      }
74  
75  
76      /**
77       * Creates a normalizing subtree specification parser.
78       * 
79       * @param resolver The resolver to use
80       * @param schemaManager The SchemaManager
81       */
82      public SubtreeSpecificationParser( @SuppressWarnings("rawtypes") NormalizerMappingResolver resolver,
83          SchemaManager schemaManager )
84      {
85          // place holder for the first input
86          StringReader in = new StringReader( "" );
87          this.lexer = new ReusableAntlrSubtreeSpecificationLexer( in );
88          this.parser = new ReusableAntlrSubtreeSpecificationParser( lexer );
89          this.parser.setNormalizerMappingResolver( resolver );
90          // this method MUST be called while we cannot do
91          // constructor overloading for antlr generated parser
92          this.parser.init( schemaManager );
93          this.isNormalizing = true;
94      }
95  
96  
97      /**
98       * Initializes the plumbing by creating a pipe and coupling the parser/lexer
99       * pair with it.
100      * 
101      * @param spec The specification to parse
102      */
103     private synchronized void reset( String spec )
104     {
105         // append end of input token
106         StringReader in = new StringReader( spec + "end" );
107         this.lexer.prepareNextInput( in );
108         this.parser.resetState();
109     }
110 
111 
112     /**
113      * Parses a subtree specification without exhausting the parser.
114      * 
115      * @param spec
116      *            the specification to be parsed
117      * @return the specification bean
118      * @throws ParseException
119      *             if there are any recognition errors (bad syntax)
120      */
121     public synchronized SubtreeSpecification parse( String spec ) throws ParseException
122     {
123         SubtreeSpecification ss;
124 
125         if ( ( spec == null ) || Strings.isEmpty( spec.trim() ) )
126         {
127             return null;
128         }
129 
130         // reset and initialize the parser / lexer pair
131         reset( spec );
132 
133         try
134         {
135             ss = this.parser.wrapperEntryPoint();
136         }
137         catch ( TokenStreamException | RecognitionException e )
138         {
139             String msg = I18n.err( I18n.ERR_13028_SUBTREE_SPEC_PARSER_FAILURE, spec, e.getLocalizedMessage() );
140             throw new ParseException( msg, 0 );
141         }
142 
143         return ss;
144     }
145 
146 
147     /**
148      * Tests to see if this parser is normalizing.
149      * 
150      * @return true if it normalizes false otherwise
151      */
152     public boolean isNormizing()
153     {
154         return this.isNormalizing;
155     }
156 }