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.shared.ldap.trigger; 022 023 024import java.io.StringReader; 025import java.text.ParseException; 026 027import org.apache.directory.shared.i18n.I18n; 028import org.apache.directory.shared.ldap.model.schema.NormalizerMappingResolver; 029 030import antlr.RecognitionException; 031import antlr.TokenStreamException; 032 033 034/** 035 * A reusable wrapper around the ANTLR generated parser for a 036 * TriggerSpecification. This class enables the reuse of the antlr parser/lexer 037 * pair without having to recreate them every time. 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 */ 041public class TriggerSpecificationParser 042{ 043 /** the antlr generated parser being wrapped */ 044 private ReusableAntlrTriggerSpecificationParser parser; 045 046 /** the antlr generated lexer being wrapped */ 047 private ReusableAntlrTriggerSpecificationLexer lexer; 048 049 private final boolean isNormalizing; 050 051 052 /** 053 * Creates a TriggerSpecification parser. 054 */ 055 public TriggerSpecificationParser() 056 { 057 this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) ); 058 this.parser = new ReusableAntlrTriggerSpecificationParser( lexer ); 059 060 this.parser.init(); // this method MUST be called while we cannot do 061 // constructor overloading for antlr generated parser 062 this.isNormalizing = false; 063 } 064 065 066 /** 067 * Creates a normalizing TriggerSpecification parser. 068 * 069 * @param resolver the resolver 070 */ 071 public TriggerSpecificationParser( NormalizerMappingResolver resolver ) 072 { 073 this.lexer = new ReusableAntlrTriggerSpecificationLexer( new StringReader( "" ) ); 074 this.parser = new ReusableAntlrTriggerSpecificationParser( lexer ); 075 076 this.parser.setNormalizerMappingResolver( resolver ); 077 this.parser.init(); // this method MUST be called while we cannot do 078 // constructor overloading for ANTLR generated parser 079 this.isNormalizing = true; 080 } 081 082 083 /** 084 * Initializes the plumbing by creating a pipe and coupling the parser/lexer 085 * pair with it. 086 * 087 * @param 088 * spec the specification to be parsed 089 */ 090 private synchronized void reset( String spec ) 091 { 092 StringReader in = new StringReader( spec ); 093 this.lexer.prepareNextInput( in ); 094 this.parser.resetState(); 095 } 096 097 098 /** 099 * Parses an TriggerSpecification without exhausting the parser. 100 * 101 * @param spec 102 * the specification to be parsed 103 * @return the specification bean 104 * @throws ParseException 105 * if there are any recognition errors (bad syntax) 106 */ 107 public synchronized TriggerSpecification parse( String spec ) throws ParseException 108 { 109 TriggerSpecification triggerSpecification = null; 110 111 if ( spec == null || spec.trim().equals( "" ) ) 112 { 113 return null; 114 } 115 116 reset( spec ); // reset and initialize the parser / lexer pair 117 118 try 119 { 120 triggerSpecification = this.parser.wrapperEntryPoint(); 121 } 122 catch ( TokenStreamException e ) 123 { 124 String msg = I18n.err( I18n.ERR_04333, spec, e.getLocalizedMessage() ); 125 throw new ParseException( msg, 0 ); 126 } 127 catch ( RecognitionException e ) 128 { 129 String msg = I18n.err( I18n.ERR_04333, spec, e.getLocalizedMessage() ); 130 throw new ParseException( msg, e.getColumn() ); 131 } 132 133 return triggerSpecification; 134 135 } 136 137 138 /** 139 * Tests to see if this parser is normalizing. 140 * 141 * @return true if it normalizes false otherwise 142 */ 143 public boolean isNormizing() 144 { 145 return this.isNormalizing; 146 } 147}