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