Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 37582 invoked from network); 29 Aug 2005 01:16:17 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 29 Aug 2005 01:16:17 -0000 Received: (qmail 20375 invoked by uid 500); 29 Aug 2005 01:16:16 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 20257 invoked by uid 500); 29 Aug 2005 01:16:15 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 20217 invoked by uid 99); 29 Aug 2005 01:16:15 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 28 Aug 2005 18:16:15 -0700 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 28 Aug 2005 18:16:29 -0700 Received: (qmail 37573 invoked by uid 65534); 29 Aug 2005 01:16:11 -0000 Message-ID: <20050829011611.37572.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r263982 - in /directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name: DnParser.java ReusableAntlrNameParser.java ReusableAntlrTypeLexer.java ReusableAntlrValueLexer.java Date: Mon, 29 Aug 2005 01:16:10 -0000 To: commits@directory.apache.org From: akarasulu@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: akarasulu Date: Sun Aug 28 18:16:03 2005 New Revision: 263982 URL: http://svn.apache.org/viewcvs?rev=263982&view=rev Log: Applied tested and deployed Ersin Er's patch for DIRLDAP-23 here: http://issues.apache.org/jira/browse/DIRLDAP-23 Added: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrNameParser.java directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrTypeLexer.java directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrValueLexer.java Modified: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/DnParser.java Modified: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/DnParser.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/DnParser.java?rev=263982&r1=263981&r2=263982&view=diff ============================================================================== --- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/DnParser.java (original) +++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/DnParser.java Sun Aug 28 18:16:03 2005 @@ -18,9 +18,8 @@ package org.apache.ldap.common.name ; -import java.io.IOException ; -import java.io.PipedInputStream ; -import java.io.PipedOutputStream ; +import java.io.IOException; +import java.io.StringReader; import javax.naming.Name ; import javax.naming.NameParser ; @@ -28,7 +27,6 @@ import org.apache.ldap.common.util.ExceptionUtils ; import org.apache.ldap.common.util.NestableRuntimeException ; -import org.apache.ldap.common.util.ParserPipedInputStream; import org.apache.ldap.common.exception.LdapNamingException; import org.apache.ldap.common.exception.LdapInvalidNameException; import org.apache.ldap.common.message.ResultCodeEnum; @@ -46,14 +44,15 @@ */ public class DnParser implements NameParser { - /** */ - private PipedOutputStream m_parserIn ; - /** */ private TokenStreamSelector m_selector ; - /** */ + private final boolean m_isNormalizing ; - /** */ - private antlrNameParser m_parser ; + + private ReusableAntlrNameParser m_parser; + + private ReusableAntlrTypeLexer typeLexer; + + private ReusableAntlrValueLexer valueLexer; /** @@ -122,24 +121,30 @@ */ private void init() throws IOException { - // Create selector and piped plumbing to feed the parser. - this.m_parserIn = new PipedOutputStream() ; this.m_selector = new TokenStreamSelector() ; - PipedInputStream in = new ParserPipedInputStream() ; - this.m_parserIn.connect( in ) ; // Create lexers and add them to the selector. - antlrTypeLexer typeLexer = new antlrTypeLexer( in ) ; - this.m_selector.addInputStream( typeLexer, antlrTypeLexer.LEXER_KEY ) ; - - antlrValueLexer valueLexer = new antlrValueLexer( typeLexer.getInputState() ) ; - this.m_selector.addInputStream( valueLexer, antlrValueLexer.LEXER_KEY ) ; + typeLexer = new ReusableAntlrTypeLexer( new StringReader( "" ) ); + this.m_selector.addInputStream( typeLexer, ReusableAntlrTypeLexer.LEXER_KEY ); + valueLexer = new ReusableAntlrValueLexer( typeLexer.getInputState() ); + this.m_selector.addInputStream( valueLexer, ReusableAntlrValueLexer.LEXER_KEY ); // Set selector on lexers, select initial lexer and initalize parser typeLexer.setSelector( this.m_selector ) ; valueLexer.setSelector( this.m_selector ) ; - this.m_selector.select( antlrTypeLexer.LEXER_KEY ) ; - this.m_parser = new antlrNameParser( m_selector ) ; + this.m_selector.select( ReusableAntlrTypeLexer.LEXER_KEY ); + this.m_parser = new ReusableAntlrNameParser( m_selector ); + } + + + /** + * Resets the parser and lexers to be reused with new input + */ + private synchronized void reset( String name ) + { + this.typeLexer.prepareNextInput( new StringReader( name + "#\n" ) ); + this.valueLexer.prepareNextInput( typeLexer.getInputState() ); + this.m_parser.resetState(); } @@ -152,7 +157,7 @@ * @throws NamingException if a_name is invalid or the parsers plumbing * breaks */ - public Name parse( String name, LdapName emptyName ) throws NamingException + public synchronized Name parse( String name, LdapName emptyName ) throws NamingException { // Handle the empty name basis case. if ( name == null || name.trim().equals( "" ) ) @@ -162,69 +167,28 @@ try { - synchronized ( m_parserIn ) + if ( null == emptyName ) { - m_parserIn.write( name.getBytes() ) ; - m_parserIn.write( '#' ) ; - m_parserIn.write( '\n' ) ; - m_parserIn.flush() ; - - if ( null == emptyName ) - { - emptyName = new LdapName( m_parser.name() ) ; - } - else - { - emptyName.setList( m_parser.name() ) ; - } + reset( name ); + emptyName = new LdapName( m_parser.name() ) ; } - } - catch ( IOException e ) - { - String msg = "Parser failure on name:\n\t" + name ; - msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e ) ; - - try - { - init() ; - } - catch ( IOException initError ) + else { - throw new LdapNamingException( "Failed to reinitialize dn parser", ResultCodeEnum.OTHER ); + reset( name ); + emptyName.setList( m_parser.name() ) ; } - - throw new LdapNamingException( msg, ResultCodeEnum.OTHER ) ; } catch ( RecognitionException e ) { String msg = "Parser failure on name:\n\t" + name ; msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e ) ; - try - { - init() ; - } - catch ( IOException initError ) - { - throw new LdapNamingException( "Failed to reinitialize dn parser", ResultCodeEnum.OTHER ); - } - throw new LdapInvalidNameException( msg, ResultCodeEnum.INVALIDDNSYNTAX ) ; } catch ( TokenStreamException e2 ) { String msg = "Parser failure on name:\n\t" + name ; msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e2 ) ; - - try - { - init() ; - } - catch ( IOException initError ) - { - throw new LdapNamingException( "Failed to reinitialize dn parser", ResultCodeEnum.OTHER ); - } - throw new LdapInvalidNameException( msg, ResultCodeEnum.INVALIDDNSYNTAX ) ; } catch ( NestableRuntimeException e ) Added: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrNameParser.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrNameParser.java?rev=263982&view=auto ============================================================================== --- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrNameParser.java (added) +++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrNameParser.java Sun Aug 28 18:16:03 2005 @@ -0,0 +1,58 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package org.apache.ldap.common.name; + + +import antlr.TokenStreamSelector; + + +/** + * A reusable parser class extended from antlr generated parser for an LDAP + * subtree specification as defined by + * RFC 3672. This class enables the reuse of the antlr parser without having to + * recreate the it every time as stated in + * + * a Antlr Interest Group mail . + * + * @see RFC 3672 + * @author Apache Directory Project + * @version $Rev$ + */ +class ReusableAntlrNameParser extends antlrNameParser +{ + /** + * Creates a ReusableAntlrNameParser instance. + */ + public ReusableAntlrNameParser( TokenStreamSelector selector ) + { + super( selector ); + } + + + /** + * Resets the state of an antlr parser. + */ + public void resetState() + { + // no set method for this protected field. + this.traceDepth = 0; + + this.getInputState().reset(); + } +} Added: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrTypeLexer.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrTypeLexer.java?rev=263982&view=auto ============================================================================== --- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrTypeLexer.java (added) +++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrTypeLexer.java Sun Aug 28 18:16:03 2005 @@ -0,0 +1,68 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package org.apache.ldap.common.name; + + +import java.io.Reader; + +import antlr.CharBuffer; +import antlr.LexerSharedInputState; + + +/** + * A reusable lexer class extended from antlr generated antlrTypelexer + * + * @author Apache Directory Project + * @version $Rev$ + */ +public class ReusableAntlrTypeLexer extends antlrTypeLexer +{ + private boolean savedCaseSensitive; + private boolean savedCaseSensitiveLiterals; + + /** + * Creates a ReusableAntlrValueLexer instance. + * + * @param in the input to the lexer + */ + public ReusableAntlrTypeLexer( Reader in ) + { + super( in ); + savedCaseSensitive = getCaseSensitive(); + savedCaseSensitiveLiterals = getCaseSensitiveLiterals(); + } + + + /** + * Resets the state of an antlr lexer and initializes it with new input. + * + * @param in the input to the lexer + */ + public void prepareNextInput( Reader in ) + { + CharBuffer buf = new CharBuffer( in ); + LexerSharedInputState state = new LexerSharedInputState( buf ); + this.setInputState(state); + + this.setCaseSensitive(savedCaseSensitive); + + // no set method for this protected field. + this.caseSensitiveLiterals = savedCaseSensitiveLiterals; + } +} Added: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrValueLexer.java URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrValueLexer.java?rev=263982&view=auto ============================================================================== --- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrValueLexer.java (added) +++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name/ReusableAntlrValueLexer.java Sun Aug 28 18:16:03 2005 @@ -0,0 +1,63 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +package org.apache.ldap.common.name; + + +import antlr.LexerSharedInputState; + + +/** + * A reusable lexer class extended from antlr generated antlrValueLexer + * + * @author Apache Directory Project + * @version $Rev$ + */ +public class ReusableAntlrValueLexer extends antlrValueLexer +{ + private boolean savedCaseSensitive; + private boolean savedCaseSensitiveLiterals; + + /** + * Creates a ReusableAntlrValueLexer instance. + * + * @param in the input to the lexer + */ + public ReusableAntlrValueLexer( LexerSharedInputState inputState ) + { + super( inputState ); + savedCaseSensitive = getCaseSensitive(); + savedCaseSensitiveLiterals = getCaseSensitiveLiterals(); + } + + + /** + * Resets the state of an antlr lexer and initializes it with new input. + * + * @param in the input to the lexer + */ + public void prepareNextInput( LexerSharedInputState inputState ) + { + this.setInputState( inputState ); + + this.setCaseSensitive(savedCaseSensitive); + + // no set method for this protected field. + this.caseSensitiveLiterals = savedCaseSensitiveLiterals; + } +}