You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/09/02 23:42:52 UTC
svn commit: r267327 -
/directory/shared/ldap/branches/new-codec-integration/common/src/java/org/apache/ldap/common/name/DnParser.java
Author: elecharny
Date: Fri Sep 2 14:42:48 2005
New Revision: 267327
URL: http://svn.apache.org/viewcvs?rev=267327&view=rev
Log:
Updated the branch to reflect the modifications in the trunk
Modified:
directory/shared/ldap/branches/new-codec-integration/common/src/java/org/apache/ldap/common/name/DnParser.java
Modified: directory/shared/ldap/branches/new-codec-integration/common/src/java/org/apache/ldap/common/name/DnParser.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/new-codec-integration/common/src/java/org/apache/ldap/common/name/DnParser.java?rev=267327&r1=267326&r2=267327&view=diff
==============================================================================
--- directory/shared/ldap/branches/new-codec-integration/common/src/java/org/apache/ldap/common/name/DnParser.java (original)
+++ directory/shared/ldap/branches/new-codec-integration/common/src/java/org/apache/ldap/common/name/DnParser.java Fri Sep 2 14:42:48 2005
@@ -1,257 +1,221 @@
-/*
- * 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.IOException ;
-import java.io.PipedInputStream ;
-import java.io.PipedOutputStream ;
-
-import javax.naming.Name ;
-import javax.naming.NameParser ;
-import javax.naming.NamingException ;
-
-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;
-
-import antlr.TokenStreamSelector ;
-import antlr.RecognitionException ;
-import antlr.TokenStreamException ;
-
-
-/**
- * A distinguished name parser which generates JNDI Ldap exception on error.
- *
- * @see <a href="http://www.faqs.org/rfcs/rfc2253.html">RFC 2253</a>
- * @see <a href="http://www.faqs.org/rfcs/rfc1779.html">RFC 1779</a>
- */
-public class DnParser implements NameParser
-{
- /** */
- private PipedOutputStream m_parserIn ;
- /** */
- private TokenStreamSelector m_selector ;
- /** */
- private final boolean m_isNormalizing ;
- /** */
- private antlrNameParser m_parser ;
-
-
- /**
- * Creates a regular non normalizing name parser.
- *
- * @throws LdapNamingException if there is a problem creating the pipe
- */
- public DnParser() throws NamingException
- {
- this.m_isNormalizing = false ;
- try
- {
- init() ;
- }
- catch ( IOException e )
- {
- String msg = "failed while initializing a name parser:\n";
- msg += ExceptionUtils.getStackTrace( e );
- LdapNamingException ne = new LdapNamingException( msg, ResultCodeEnum.OTHER );
- ne.setRootCause( e );
- throw ne;
- }
- }
-
-
- /**
- * Creates a normalizing name parser.
- *
- * @param a_normalizer the name component value normaliser used
- * @throws LdapNamingException if there is a problem creating the pipe
- */
- public DnParser( NameComponentNormalizer a_normalizer ) throws NamingException
- {
- try
- {
- init() ;
- }
- catch ( IOException e )
- {
- String msg = "failed while initializing a name parser:\n";
- msg += ExceptionUtils.getStackTrace( e );
- LdapNamingException ne = new LdapNamingException( msg, ResultCodeEnum.OTHER );
- ne.setRootCause( e );
- throw ne;
- }
- this.m_isNormalizing = true ;
- this.m_parser.setNormalizer( a_normalizer ) ;
- }
-
-
- /**
- * Tests to see if this parser is normalizing.
- *
- * @return true if it normalizes false otherwise
- */
- public boolean isNormizing()
- {
- return this.m_isNormalizing ;
- }
-
-
- /**
- * Initializes the parser machinery and the pluming.
- *
- * @throws IOException if there is a problem creating the parser's pipe
- */
- 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 ) ;
-
- // 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 ) ;
- }
-
-
- /**
- * Parses a name as a String into an existing Name object.
- *
- * @param name the distinguished name as a string.
- * @param emptyName the empty LdapName to be populated or null.
- * @return the populated LdapName
- * @throws NamingException if a_name is invalid or the parsers plumbing
- * breaks
- */
- public Name parse( String name, LdapName emptyName ) throws NamingException
- {
- // Handle the empty name basis case.
- if ( name == null || name.trim().equals( "" ) )
- {
- return null == emptyName ? new LdapName() : emptyName ;
- }
-
- try
- {
- synchronized ( m_parserIn )
- {
- 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() ) ;
- }
- }
- }
- 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 )
- {
- throw new LdapNamingException( "Failed to reinitialize dn parser", ResultCodeEnum.OTHER );
- }
-
- 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 )
- {
- Throwable throwable = e.getCause() ;
- if ( throwable instanceof NamingException )
- {
- NamingException ne = ( NamingException ) throwable ;
- throw ne ;
- }
- else
- {
- throw e ;
- }
- }
-
- return emptyName ;
- }
-
-
- /**
- * Parses a name as a String into a Name object.
- *
- * @see javax.naming.NameParser#parse(java.lang.String)
- */
- public Name parse( String name ) throws NamingException
- {
- return parse( name, new LdapName() ) ;
- }
-}
+/*
+ * 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.IOException;
+import java.io.StringReader;
+
+import javax.naming.Name ;
+import javax.naming.NameParser ;
+import javax.naming.NamingException ;
+
+import org.apache.ldap.common.util.ExceptionUtils ;
+import org.apache.ldap.common.util.NestableRuntimeException ;
+import org.apache.ldap.common.exception.LdapNamingException;
+import org.apache.ldap.common.exception.LdapInvalidNameException;
+import org.apache.ldap.common.message.ResultCodeEnum;
+
+import antlr.TokenStreamSelector ;
+import antlr.RecognitionException ;
+import antlr.TokenStreamException ;
+
+
+/**
+ * A distinguished name parser which generates JNDI Ldap exception on error.
+ *
+ * @see <a href="http://www.faqs.org/rfcs/rfc2253.html">RFC 2253</a>
+ * @see <a href="http://www.faqs.org/rfcs/rfc1779.html">RFC 1779</a>
+ */
+public class DnParser implements NameParser
+{
+ private TokenStreamSelector m_selector ;
+
+ private final boolean m_isNormalizing ;
+
+ private ReusableAntlrNameParser m_parser;
+
+ private ReusableAntlrTypeLexer typeLexer;
+
+ private ReusableAntlrValueLexer valueLexer;
+
+
+ /**
+ * Creates a regular non normalizing name parser.
+ *
+ * @throws LdapNamingException if there is a problem creating the pipe
+ */
+ public DnParser() throws NamingException
+ {
+ this.m_isNormalizing = false ;
+ try
+ {
+ init() ;
+ }
+ catch ( IOException e )
+ {
+ String msg = "failed while initializing a name parser:\n";
+ msg += ExceptionUtils.getStackTrace( e );
+ LdapNamingException ne = new LdapNamingException( msg, ResultCodeEnum.OTHER );
+ ne.setRootCause( e );
+ throw ne;
+ }
+ }
+
+
+ /**
+ * Creates a normalizing name parser.
+ *
+ * @param a_normalizer the name component value normaliser used
+ * @throws LdapNamingException if there is a problem creating the pipe
+ */
+ public DnParser( NameComponentNormalizer a_normalizer ) throws NamingException
+ {
+ try
+ {
+ init() ;
+ }
+ catch ( IOException e )
+ {
+ String msg = "failed while initializing a name parser:\n";
+ msg += ExceptionUtils.getStackTrace( e );
+ LdapNamingException ne = new LdapNamingException( msg, ResultCodeEnum.OTHER );
+ ne.setRootCause( e );
+ throw ne;
+ }
+ this.m_isNormalizing = true ;
+ this.m_parser.setNormalizer( a_normalizer ) ;
+ }
+
+
+ /**
+ * Tests to see if this parser is normalizing.
+ *
+ * @return true if it normalizes false otherwise
+ */
+ public boolean isNormizing()
+ {
+ return this.m_isNormalizing ;
+ }
+
+
+ /**
+ * Initializes the parser machinery and the pluming.
+ *
+ * @throws IOException if there is a problem creating the parser's pipe
+ */
+ private void init() throws IOException
+ {
+ this.m_selector = new TokenStreamSelector() ;
+
+ // Create lexers and add them to the selector.
+ 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( 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();
+ }
+
+
+ /**
+ * Parses a name as a String into an existing Name object.
+ *
+ * @param name the distinguished name as a string.
+ * @param emptyName the empty LdapName to be populated or null.
+ * @return the populated LdapName
+ * @throws NamingException if a_name is invalid or the parsers plumbing
+ * breaks
+ */
+ public synchronized Name parse( String name, LdapName emptyName ) throws NamingException
+ {
+ // Handle the empty name basis case.
+ if ( name == null || name.trim().equals( "" ) )
+ {
+ return null == emptyName ? new LdapName() : emptyName ;
+ }
+
+ try
+ {
+ if ( null == emptyName )
+ {
+ reset( name );
+ emptyName = new LdapName( m_parser.name() ) ;
+ }
+ else
+ {
+ reset( name );
+ emptyName.setList( m_parser.name() ) ;
+ }
+ }
+ catch ( RecognitionException e )
+ {
+ String msg = "Parser failure on name:\n\t" + name ;
+ msg += "\nAntlr exception trace:\n" + ExceptionUtils.getFullStackTrace( e ) ;
+
+ 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 ) ;
+ throw new LdapInvalidNameException( msg, ResultCodeEnum.INVALIDDNSYNTAX ) ;
+ }
+ catch ( NestableRuntimeException e )
+ {
+ Throwable throwable = e.getCause() ;
+ if ( throwable instanceof NamingException )
+ {
+ NamingException ne = ( NamingException ) throwable ;
+ throw ne ;
+ }
+ else
+ {
+ throw e ;
+ }
+ }
+
+ return emptyName ;
+ }
+
+
+ /**
+ * Parses a name as a String into a Name object.
+ *
+ * @see javax.naming.NameParser#parse(java.lang.String)
+ */
+ public Name parse( String name ) throws NamingException
+ {
+ return parse( name, new LdapName() ) ;
+ }
+}