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 021 package org.apache.directory.shared.ldap.subtree; 022 023 024 import java.io.StringReader; 025 import java.text.ParseException; 026 import java.util.Map; 027 028 import org.apache.directory.shared.i18n.I18n; 029 030 import antlr.RecognitionException; 031 import 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 * @version $Rev: 437007 $ 043 */ 044 public class SubtreeSpecificationChecker 045 { 046 /** the antlr generated parser being wrapped */ 047 private ReusableAntlrSubtreeSpecificationChecker parser; 048 049 /** the antlr generated lexer being wrapped */ 050 private ReusableAntlrSubtreeSpecificationCheckerLexer lexer; 051 052 053 /** 054 * Creates a subtree specification parser. 055 */ 056 public SubtreeSpecificationChecker( Map oidsMap ) 057 { 058 StringReader in = new StringReader( "" ); // place holder for the 059 // first input 060 this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in ); 061 this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer ); 062 this.parser.init(); // this method MUST be called while we cannot do 063 // constructor overloading for antlr generated parser 064 } 065 066 067 /** 068 * Creates a normalizing subtree specification parser. 069 */ 070 public SubtreeSpecificationChecker() 071 { 072 StringReader in = new StringReader( "" ); // place holder for the 073 // first input 074 this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in ); 075 this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer ); 076 this.parser.init(); // this method MUST be called while we cannot do 077 // constructor overloading for antlr generated parser 078 } 079 080 081 /** 082 * Initializes the plumbing by creating a pipe and coupling the parser/lexer 083 * pair with it. param spec the specification to be parsed 084 */ 085 private synchronized void reset( String spec ) 086 { 087 StringReader in = new StringReader( spec + "end" ); // append end of 088 // input token 089 this.lexer.prepareNextInput( in ); 090 this.parser.resetState(); 091 } 092 093 094 /** 095 * Parses a subtree specification without exhausting the parser. 096 * 097 * @param spec 098 * the specification to be parsed 099 * @throws ParseException 100 * if there are any recognition errors (bad syntax) 101 */ 102 public synchronized void parse( String spec ) throws ParseException 103 { 104 if ( spec == null || spec.trim().equals( "" ) ) 105 { 106 return; 107 } 108 109 reset( spec ); // reset and initialize the parser / lexer pair 110 111 try 112 { 113 this.parser.wrapperEntryPoint(); 114 } 115 catch ( TokenStreamException e ) 116 { 117 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 118 throw new ParseException( msg, 0 ); 119 } 120 catch ( RecognitionException e ) 121 { 122 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 123 throw new ParseException( msg, e.getColumn() ); 124 } 125 catch ( Exception e ) 126 { 127 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() ); 128 throw new ParseException( msg, 0 ); 129 } 130 } 131 132 }