Author: akarasulu
Date: Sun Oct 31 01:41:06 2004
New Revision: 56133
Modified:
incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/ldif/LdifIterator.java
Log:
cleanedup, found and fixed some bugs
Modified:
incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/ldif/LdifIterator.java
==============================================================================
---
incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/ldif/LdifIterator.java
(original)
+++
incubator/directory/ldap/trunk/common/src/java/org/apache/ldap/common/ldif/LdifIterator.java
Sun Oct 31 01:41:06 2004
@@ -1,25 +1,25 @@
-/*
- * 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.
- *
- */
+/*
+ * 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.ldif ;
-
+
import java.io.Reader ;
import java.io.IOException ;
-import java.io.InputStream ;
+import java.io.InputStream ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
@@ -29,26 +29,31 @@
/**
* Iterates through a set of LDIF's on a input channel.
*
- * @author <a href="mailto:[EMAIL PROTECTED]">
- * Apache Directory Project</a>
- * @version $Rev$
- */
+ * @author <a href="mailto:[EMAIL PROTECTED]">
+ * Apache Directory Project</a>
+ * @version $Rev$
+ */
public class LdifIterator implements Iterator
{
/** whether or not debugging is enabled */
private static final boolean DEBUG = false ;
/** the prefetched LDIF record off of the stream */
- private String m_prefetched = null ;
+ private String prefetched = null ;
/** the monitor used */
private LdifIteratorMonitor monitor = new LdifIteratorMonitorAdapter() ;
/** input reader to read from */
- private BufferedReader m_in = null ;
+ private BufferedReader in = null ;
/** a temporary buffer */
- private StringBuffer m_buf = new StringBuffer() ;
+ private StringBuffer buf = new StringBuffer() ;
+
+
+ // ------------------------------------------------------------------------
+ // C O N S T R U C T O R S
+ // ------------------------------------------------------------------------
/**
@@ -58,10 +63,14 @@
*
* @throws IOException if we cannot wrap the stream with a reader
*/
- public LdifIterator( InputStream in )
- throws IOException
+ public LdifIterator( InputStream in ) throws IOException
{
this( new InputStreamReader( in ) ) ;
+
+ if ( in == null )
+ {
+ throw new NullPointerException( "InputStream cannot be null!" );
+ }
}
@@ -72,10 +81,14 @@
*
* @throws IOException if we cannot wrap the reader with a bufferd reader
*/
- public LdifIterator( Reader in )
- throws IOException
+ public LdifIterator( Reader in ) throws IOException
{
- m_in = new BufferedReader( in ) ;
+ if ( in == null )
+ {
+ throw new NullPointerException( "Reader cannot be null!" );
+ }
+
+ this.in = new BufferedReader( in ) ;
debug( "<init>: -- opended file" ) ;
prefetch() ;
debug( "<init>: -- prefetch complete" ) ;
@@ -90,10 +103,14 @@
*
* @throws IOException if we cannot wrap the stream with a reader
*/
- public LdifIterator( InputStream in, LdifIteratorMonitor monitor )
- throws IOException
+ public LdifIterator( InputStream in, LdifIteratorMonitor monitor ) throws
IOException
{
this( new InputStreamReader( in ), monitor ) ;
+
+ if ( in == null )
+ {
+ throw new NullPointerException( "InputStream cannot be null!" );
+ }
}
@@ -105,14 +122,27 @@
*
* @throws IOException if we cannot wrap the reader with a bufferd reader
*/
- public LdifIterator( Reader in, LdifIteratorMonitor monitor )
- throws IOException
+ public LdifIterator( Reader in, LdifIteratorMonitor monitor ) throws
IOException
{
this( in ) ;
- this.monitor = monitor ;
+
+ if ( monitor != null )
+ {
+ this.monitor = monitor ;
+ }
+
+ if ( in == null )
+ {
+ throw new NullPointerException( "Reader cannot be null!" );
+ }
}
+ // ------------------------------------------------------------------------
+ // Iterator Methods
+ // ------------------------------------------------------------------------
+
+
/**
* Tests to see if another LDIF is on the input channel.
*
@@ -122,11 +152,11 @@
{
if ( DEBUG )
{
- debug( "hasNext(): -- returning " + ( m_prefetched != null ) ) ;
+ debug( "hasNext(): -- returning " + ( prefetched != null ) ) ;
}
- return null != m_prefetched ;
+ return null != prefetched ;
}
@@ -137,7 +167,7 @@
*/
public Object next()
{
- String l_retVal = m_prefetched ;
+ String retVal = prefetched ;
try
{
@@ -146,7 +176,7 @@
if ( DEBUG )
{ // Don't pay string + price if debug is off
- debug( "next(): -- returning ldif\n" + l_retVal ) ;
+ debug( "next(): -- returning ldif\n" + retVal ) ;
}
}
catch ( IOException e )
@@ -156,7 +186,7 @@
}
- return l_retVal ;
+ return retVal ;
}
@@ -217,93 +247,65 @@
/**
- * If debugging is enabled these log messages are sent to either the
- * console or to the monitor if one is available.
- *
- * @param msg the debug message to log
- * @param throwable the throwable to log
- */
- private void debug( String msg, Throwable throwable )
- {
- if ( !DEBUG )
- {
- return ;
- }
-
-
- if ( null == monitor )
- {
- System.out.println( msg ) ;
- throwable.printStackTrace( System.out ) ;
- }
- else
- {
- monitor.failure( msg, throwable ) ;
- }
- }
-
-
- /**
* Prefetches a multi-line LDIF fron the input channel.
*
* @throws IOException if it cannot read from the input channel
*/
- private void prefetch()
- throws IOException
+ private void prefetch() throws IOException
{
- boolean l_insideLdif = false ;
- String l_line = null ;
+ boolean insideLdif = false ;
+ String line = null ;
- while ( null != ( l_line = m_in.readLine() ) )
+ while ( null != ( line = in.readLine() ) )
{
- debug( "readLine(): " + l_line ) ;
- l_line = filterComment( l_line ).trim() ;
+ debug( "readLine(): " + line ) ;
+ line = filterComment( line ).trim() ;
- if ( l_insideLdif )
+ if ( insideLdif )
{
- if ( l_line.equals( "" ) )
+ if ( line.equals( "" ) )
{
break ;
}
debug( "prefetch(): -- appending last line to buffer" ) ;
- m_buf.append( l_line ).append( '\n' ) ;
+ buf.append( line ).append( '\n' ) ;
}
else
{
- if ( l_line.equals( "" ) )
+ if ( line.equals( "" ) )
{
continue ;
}
- l_insideLdif = true ;
+ insideLdif = true ;
debug( "prefetch(): -- appending last line to buffer" ) ;
- m_buf.append( l_line ).append( '\n' ) ;
+ buf.append( line ).append( '\n' ) ;
}
}
- if ( ( null == l_line ) && ( 0 == m_buf.length() ) )
+ if ( ( null == line ) && ( 0 == buf.length() ) )
{
debug( "prefetch(): -- line was null and buffer was empty" ) ;
debug( "prefetch(): -- iterator has been consumed" ) ;
- m_prefetched = null ;
+ prefetched = null ;
}
else
{
debug( "prefetch(): -- LDIF prefetched and set as next" ) ;
- m_prefetched = m_buf.toString() ;
+ prefetched = buf.toString() ;
if ( DEBUG )
{
- debug( "prefetch(): -- \n" + m_prefetched ) ;
+ debug( "prefetch(): -- \n" + prefetched ) ;
}
}
- m_buf.setLength( 0 ) ;
+ buf.setLength( 0 ) ;
debug( "prefetch(): -- LDIF buffer cleared" ) ;
}
@@ -317,27 +319,26 @@
*/
public static String filterComment( String line )
{
- int l_index = line.indexOf( '#' ) ;
+ int index = line.indexOf( '#' ) ;
- if ( -1 == l_index )
+ if ( -1 == index )
{
return line ;
}
- else if ( l_index == 0 )
+ else if ( index == 0 )
{
return "" ;
}
- while ( -1 != l_index )
+ while ( -1 != index )
{
// If this is an escaped '#' then take new index from current
// index + 1 and continue from start of loop.
- if ( ( ( l_index - 1 ) > 0 )
- && ( '\\' == line.charAt( l_index - 1 ) ) )
+ if ( ( ( index - 1 ) > 0 ) && ( '\\' == line.charAt( index - 1 ) )
)
{
- if ( ( l_index + 1 ) < line.length() )
+ if ( ( index + 1 ) < line.length() )
{
- l_index = line.indexOf( '#', l_index + 1 ) ;
+ index = line.indexOf( '#', index + 1 ) ;
continue ;
}
@@ -348,10 +349,8 @@
}
}
-
- return line.substring( 0, l_index ) ;
+ return line.substring( 0, index ) ;
}
-
return line ;
}