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