001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 021package org.apache.directory.api.ldap.model.subtree; 022 023 024import java.io.StringReader; 025import java.text.ParseException; 026 027import org.apache.directory.api.i18n.I18n; 028import org.apache.directory.api.ldap.model.schema.SchemaManager; 029 030import antlr.RecognitionException; 031import antlr.TokenStreamException; 032 033 034/** 035 * A reusable wrapper around the antlr generated parser for an LDAP subtree 036 * specification as defined by <a href="http://www.faqs.org/rfcs/rfc3672.html"> 037 * RFC 3672</a>. This class enables the reuse of the antlr parser/lexer pair 038 * without having to recreate the pair every time. 039 * 040 * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a> 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043public class SubtreeSpecificationChecker 044{ 045 /** the antlr generated parser being wrapped */ 046 private ReusableAntlrSubtreeSpecificationChecker parser; 047 048 /** the antlr generated lexer being wrapped */ 049 private ReusableAntlrSubtreeSpecificationCheckerLexer lexer; 050 051 052 /** 053 * Creates a normalizing subtree specification parser. 054 */ 055 public SubtreeSpecificationChecker( SchemaManager schemaManager ) 056 { 057 StringReader in = new StringReader( "" ); // place holder for the 058 // first input 059 this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in ); 060 this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer ); 061 this.parser.init( schemaManager ); // this method MUST be called while we cannot do 062 // constructor overloading for antlr generated parser 063 } 064 065 066 /** 067 * Initializes the plumbing by creating a pipe and coupling the parser/lexer 068 * pair with it. param spec the specification to be parsed 069 */ 070 private synchronized void reset( String spec ) 071 { 072 StringReader in = new StringReader( spec + "end" ); // append end of 073 // input token 074 this.lexer.prepareNextInput( in ); 075 this.parser.resetState(); 076 } 077 078 079 /** 080 * Parses a subtree specification without exhausting the parser. 081 * 082 * @param spec 083 * the specification to be parsed 084 * @throws ParseException 085 * if there are any recognition errors (bad syntax) 086 */ 087 public synchronized void parse( String spec ) throws ParseException 088 { 089 if ( spec == null || spec.trim().equals( "" ) ) 090 { 091 return; 092 } 093 094 reset( spec ); // reset and initialize the parser / lexer pair 095 096 try 097 { 098 this.parser.wrapperEntryPoint(); 099 } 100 catch ( TokenStreamException e ) 101 { 102 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 103 throw new ParseException( msg, 0 ); 104 } 105 catch ( RecognitionException e ) 106 { 107 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 108 throw new ParseException( msg, e.getColumn() ); 109 } 110 catch ( Exception e ) 111 { 112 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 113 throw new ParseException( msg, 0 ); 114 } 115 } 116 117}