You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2005/08/29 03:16:10 UTC
svn commit: r263982 - in
/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/name:
DnParser.java ReusableAntlrNameParser.java ReusableAntlrTypeLexer.java
ReusableAntlrValueLexer.java
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 <a href="http://www.faqs.org/rfcs/rfc3672.html">
+ * RFC 3672</a>. This class enables the reuse of the antlr parser without having to
+ * recreate the it every time as stated in
+ * <a href="http://www.antlr.org:8080/pipermail/antlr-interest/2003-April/003631.html">
+ * a Antlr Interest Group mail</a> .
+ *
+ * @see <a href="http://www.faqs.org/rfcs/rfc3672.html">RFC 3672</a>
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @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 <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @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 <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @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;
+ }
+}