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