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 * @param schemaManager The SchemaManager 56 */ 57 public SubtreeSpecificationChecker( SchemaManager schemaManager ) 58 { 59 // place holder for the first input 60 StringReader in = new StringReader( "" ); 61 this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in ); 62 this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer ); 63 64 // this method MUST be called while we cannot do 65 // constructor overloading for antlr generated parser 66 this.parser.init( schemaManager ); 67 } 68 69 70 /** 71 * Initializes the plumbing by creating a pipe and coupling the parser/lexer 72 * pair with it. param spec the specification to be parsed 73 * 74 * @param spec The specification to parse 75 */ 76 private synchronized void reset( String spec ) 77 { 78 // append end of input token 79 StringReader in = new StringReader( spec + "end" ); 80 this.lexer.prepareNextInput( in ); 81 this.parser.resetState(); 82 } 83 84 85 /** 86 * Parses a subtree specification without exhausting the parser. 87 * 88 * @param spec the specification to be parsed 89 * @throws ParseException if there are any recognition errors (bad syntax) 90 */ 91 public synchronized void parse( String spec ) throws ParseException 92 { 93 if ( ( spec == null ) || ( spec.trim().length() == 0 ) ) 94 { 95 return; 96 } 97 98 // reset and initialize the parser / lexer pair 99 reset( spec ); 100 101 try 102 { 103 this.parser.wrapperEntryPoint(); 104 } 105 catch ( TokenStreamException | RecognitionException e ) 106 { 107 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 108 throw new ParseException( msg, 0 ); 109 } 110 } 111 }