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
021 package org.apache.directory.shared.ldap.aci;
022
023
024 import java.io.StringReader;
025 import java.text.ParseException;
026
027 import antlr.RecognitionException;
028 import antlr.TokenStreamException;
029
030
031 /**
032 * A reusable wrapper around the antlr generated parser for an ACIItem as
033 * defined by X.501. This class enables the reuse of the antlr parser/lexer pair
034 * without having to recreate them every time.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev: 437007 $
038 */
039 public class ACIItemChecker
040 {
041 /** the antlr generated parser being wrapped */
042 private ReusableAntlrACIItemParser checker;
043
044 /** the antlr generated lexer being wrapped */
045 private ReusableAntlrACIItemLexer lexer;
046
047 private final boolean isNormalizing;
048
049
050 /**
051 * Creates a ACIItem parser.
052 */
053 public ACIItemChecker()
054 {
055 this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) );
056 this.checker = new ReusableAntlrACIItemParser( lexer );
057 this.isNormalizing = false;
058 }
059
060
061 /**
062 * Initializes the plumbing by creating a pipe and coupling the parser/lexer
063 * pair with it. param spec the specification to be parsed
064 */
065 private synchronized void reset( String spec )
066 {
067 StringReader in = new StringReader( spec );
068 this.lexer.prepareNextInput( in );
069 this.checker.resetState();
070 }
071
072
073 /**
074 * Parses an ACIItem without exhausting the parser.
075 *
076 * @param spec
077 * the specification to be parsed
078 * @return the specification bean
079 * @throws ParseException
080 * if there are any recognition errors (bad syntax)
081 */
082 public synchronized void parse( String spec ) throws ParseException
083 {
084 if ( spec == null || spec.trim().equals( "" ) )
085 {
086 return;
087 }
088
089 reset( spec ); // reset and initialize the parser / lexer pair
090
091 try
092 {
093 this.checker.wrapperEntryPoint();
094 }
095 catch ( TokenStreamException e )
096 {
097 String msg = "Parser failure on ACIItem:\n\t" + spec;
098 msg += "\nAntlr exception trace:\n" + e.getMessage();
099 throw new ParseException( msg, 0 );
100 }
101 catch ( RecognitionException e )
102 {
103 String msg = "Parser failure on ACIItem:\n\t" + spec;
104 msg += "\nAntlr exception trace:\n" + e.getMessage();
105 throw new ParseException( msg, e.getColumn() );
106 }
107
108 return;
109 }
110
111
112 /**
113 * Tests to see if this parser is normalizing.
114 *
115 * @return true if it normalizes false otherwise
116 */
117 public boolean isNormizing()
118 {
119 return this.isNormalizing;
120 }
121 }