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