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.subtree;
022
023
024 import java.io.StringReader;
025 import java.text.ParseException;
026 import java.util.Map;
027
028 import antlr.RecognitionException;
029 import antlr.TokenStreamException;
030
031
032 /**
033 * A reusable wrapper around the antlr generated parser for an LDAP subtree
034 * specification as defined by <a href="http://www.faqs.org/rfcs/rfc3672.html">
035 * RFC 3672</a>. This class enables the reuse of the antlr parser/lexer pair
036 * without having to recreate the pair every time.
037 *
038 * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 * @version $Rev: 437007 $
041 */
042 public class SubtreeSpecificationChecker
043 {
044 /** the antlr generated parser being wrapped */
045 private ReusableAntlrSubtreeSpecificationChecker parser;
046
047 /** the antlr generated lexer being wrapped */
048 private ReusableAntlrSubtreeSpecificationCheckerLexer lexer;
049
050
051 /**
052 * Creates a subtree specification parser.
053 */
054 public SubtreeSpecificationChecker( Map oidsMap )
055 {
056 StringReader in = new StringReader( "" ); // place holder for the
057 // first input
058 this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in );
059 this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer );
060 this.parser.init(); // this method MUST be called while we cannot do
061 // constructor overloading for antlr generated parser
062 }
063
064
065 /**
066 * Creates a normalizing subtree specification parser.
067 */
068 public SubtreeSpecificationChecker()
069 {
070 StringReader in = new StringReader( "" ); // place holder for the
071 // first input
072 this.lexer = new ReusableAntlrSubtreeSpecificationCheckerLexer( in );
073 this.parser = new ReusableAntlrSubtreeSpecificationChecker( lexer );
074 this.parser.init(); // this method MUST be called while we cannot do
075 // constructor overloading for antlr generated parser
076 }
077
078
079 /**
080 * Initializes the plumbing by creating a pipe and coupling the parser/lexer
081 * pair with it. param spec the specification to be parsed
082 */
083 private synchronized void reset( String spec )
084 {
085 StringReader in = new StringReader( spec + "end" ); // append end of
086 // input token
087 this.lexer.prepareNextInput( in );
088 this.parser.resetState();
089 }
090
091
092 /**
093 * Parses a subtree specification without exhausting the parser.
094 *
095 * @param spec
096 * the specification to be parsed
097 * @throws ParseException
098 * if there are any recognition errors (bad syntax)
099 */
100 public synchronized void parse( String spec ) throws ParseException
101 {
102 if ( spec == null || spec.trim().equals( "" ) )
103 {
104 return;
105 }
106
107 reset( spec ); // reset and initialize the parser / lexer pair
108
109 try
110 {
111 this.parser.wrapperEntryPoint();
112 }
113 catch ( TokenStreamException e )
114 {
115 String msg = "Parser failure on subtree specification:\n\t" + spec;
116 msg += "\nAntlr exception trace:\n" + e.getMessage();
117 throw new ParseException( msg, 0 );
118 }
119 catch ( RecognitionException e )
120 {
121 String msg = "Parser failure on subtree specification:\n\t" + spec;
122 msg += "\nAntlr exception trace:\n" + e.getMessage();
123 throw new ParseException( msg, e.getColumn() );
124 }
125 catch ( Exception e )
126 {
127 String msg = "Parser failure on subtree specification:\n\t" + spec;
128 msg += "\nAntlr exception trace:\n" + e.getMessage();
129 throw new ParseException( msg, 0 );
130 }
131 }
132
133 }