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 * https://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 * @param spec The part to parse 069 */ 070 private synchronized void reset( String spec ) 071 { 072 StringReader in = new StringReader( spec ); 073 this.lexer.prepareNextInput( in ); 074 this.checker.resetState(); 075 } 076 077 078 /** 079 * Parses an ACIItem without exhausting the parser. 080 * 081 * @param spec 082 * the specification to be parsed 083 * @throws ParseException 084 * if there are any recognition errors (bad syntax) 085 */ 086 public synchronized void parse( String spec ) throws ParseException 087 { 088 if ( spec == null || StringConstants.EMPTY.equals( spec.trim() ) ) 089 { 090 return; 091 } 092 093 // reset and initialize the parser / lexer pair 094 reset( spec ); 095 096 try 097 { 098 this.checker.wrapperEntryPoint(); 099 } 100 catch ( TokenStreamException e ) 101 { 102 throw new ParseException( I18n 103 .err( I18n.ERR_07004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), 0 ); 104 } 105 catch ( RecognitionException e ) 106 { 107 throw new ParseException( I18n 108 .err( I18n.ERR_07004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), e.getColumn() ); 109 } 110 } 111 112}