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