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.ldap.model.schema.normalizers.NameComponentNormalizer; 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 ACIItemParser 043{ 044 /** the antlr generated parser being wrapped */ 045 private ReusableAntlrACIItemParser parser; 046 047 /** the antlr generated lexer being wrapped */ 048 private ReusableAntlrACIItemLexer lexer; 049 050 /** The is normalizing flag. */ 051 private final boolean isNormalizing; 052 053 054 /** 055 * Creates a ACIItem parser. 056 * 057 * @param schemaManager the schema manager 058 */ 059 public ACIItemParser( SchemaManager schemaManager ) 060 { 061 this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) ); 062 this.parser = new ReusableAntlrACIItemParser( lexer ); 063 064 // this method MUST be called while we cannot do 065 // constructor overloading for antlr generated parser 066 this.parser.init( schemaManager ); 067 068 this.isNormalizing = false; 069 } 070 071 072 /** 073 * Creates a normalizing ACIItem parser. 074 * 075 * @param normalizer the normalizer 076 * @param schemaManager the schema manager 077 */ 078 public ACIItemParser( NameComponentNormalizer normalizer, SchemaManager schemaManager ) 079 { 080 this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) ); 081 this.parser = new ReusableAntlrACIItemParser( lexer ); 082 083 this.parser.setNormalizer( normalizer ); 084 this.isNormalizing = true; 085 086 // this method MUST be called while we cannot do 087 // constructor overloading for antlr generated parser 088 this.parser.init( schemaManager ); 089 } 090 091 092 /** 093 * Initializes the plumbing by creating a pipe and coupling the parser/lexer 094 * pair with it. param spec the specification to be parsed 095 */ 096 private synchronized void reset( String spec ) 097 { 098 StringReader in = new StringReader( spec ); 099 this.lexer.prepareNextInput( in ); 100 this.parser.resetState(); 101 } 102 103 104 /** 105 * Parses an ACIItem without exhausting the parser. 106 * 107 * @param spec 108 * the specification to be parsed 109 * @return the specification bean 110 * @throws ParseException 111 * if there are any recognition errors (bad syntax) 112 */ 113 public synchronized ACIItem parse( String spec ) throws ParseException 114 { 115 ACIItem aCIItem = null; 116 117 if ( spec == null || spec.trim().equals( "" ) ) 118 { 119 return null; 120 } 121 122 reset( spec ); // reset and initialize the parser / lexer pair 123 124 try 125 { 126 aCIItem = this.parser.wrapperEntryPoint(); 127 } 128 catch ( TokenStreamException e ) 129 { 130 throw new ParseException( I18n 131 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), 0 ); 132 } 133 catch ( RecognitionException e ) 134 { 135 throw new ParseException( 136 I18n 137 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage(), e.getLine(), 138 e.getColumn() ), e.getColumn() ); 139 } 140 141 return aCIItem; 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}