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.aci; 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; 029import org.apache.directory.api.util.StringConstants; 030 031import antlr.RecognitionException; 032import antlr.TokenStreamException; 033 034 035/** 036 * A reusable wrapper around the antlr generated parser for an ACIItem as 037 * defined by X.501. This class enables the reuse of the antlr parser/lexer pair 038 * without having to recreate them every time. 039 * 040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 041 */ 042public class ACIItemChecker 043{ 044 /** the antlr generated parser being wrapped */ 045 private ReusableAntlrACIItemParser checker; 046 047 /** the antlr generated lexer being wrapped */ 048 private ReusableAntlrACIItemLexer lexer; 049 050 051 /** 052 * Creates a ACIItem parser. 053 * 054 * @param schemaManager the schema manager 055 */ 056 public ACIItemChecker( SchemaManager schemaManager ) 057 { 058 this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) ); 059 this.checker = new ReusableAntlrACIItemParser( lexer ); 060 this.checker.init( schemaManager ); 061 } 062 063 064 /** 065 * Initializes the plumbing by creating a pipe and coupling the parser/lexer 066 * pair with it. param spec the specification to be parsed 067 */ 068 private synchronized void reset( String spec ) 069 { 070 StringReader in = new StringReader( spec ); 071 this.lexer.prepareNextInput( in ); 072 this.checker.resetState(); 073 } 074 075 076 /** 077 * Parses an ACIItem without exhausting the parser. 078 * 079 * @param spec 080 * the specification to be parsed 081 * @throws ParseException 082 * if there are any recognition errors (bad syntax) 083 */ 084 public synchronized void parse( String spec ) throws ParseException 085 { 086 if ( spec == null || StringConstants.EMPTY .equals( spec.trim() ) ) 087 { 088 return; 089 } 090 091 // reset and initialize the parser / lexer pair 092 reset( spec ); 093 094 try 095 { 096 this.checker.wrapperEntryPoint(); 097 } 098 catch ( TokenStreamException e ) 099 { 100 throw new ParseException( I18n 101 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), 0 ); 102 } 103 catch ( RecognitionException e ) 104 { 105 throw new ParseException( I18n 106 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), e.getColumn() ); 107 } 108 } 109 110}