Author: elecharny
Date: Mon Dec 14 12:16:11 2009
New Revision: 890287
URL: http://svn.apache.org/viewvc?rev=890287&view=rev
Log:
Fixed a potential NPE
Modified:
directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java?rev=890287&r1=890286&r2=890287&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
(original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
Mon Dec 14 12:16:11 2009
@@ -19,6 +19,7 @@
*/
package org.apache.directory.shared.ldap.ldif;
+
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.DataInputStream;
@@ -59,6 +60,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
/**
* <pre>
* <ldif-file> ::= "version:" <fill> <number>
<seps> <dn-spec> <sep>
@@ -181,6 +183,7 @@
/** The current position */
private int pos;
+
/**
* Creates a new instance of Position.
*/
@@ -189,6 +192,7 @@
pos = 0;
}
+
/**
* Increment the current position by one
*
@@ -198,6 +202,7 @@
pos++;
}
+
/**
* Increment the current position by the given value
*
@@ -259,6 +264,7 @@
*/
protected Exception error;
+
/**
* Constructors
*/
@@ -268,7 +274,7 @@
position = new Position();
version = DEFAULT_VERSION;
}
-
+
private void init( BufferedReader reader ) throws NamingException
{
@@ -284,6 +290,7 @@
prefetched = parseEntry();
}
+
/**
* A constructor which takes a file name
*
@@ -311,14 +318,14 @@
{
init( new BufferedReader( new FileReader( file ) ) );
}
- catch (FileNotFoundException fnfe)
+ catch ( FileNotFoundException fnfe )
{
LOG.error( "File {} cannot be found", file.getAbsoluteFile() );
throw new NamingException( "Cannot find file " + file.getAbsoluteFile() );
}
}
-
+
/**
* A constructor which takes a Reader
*
@@ -332,6 +339,7 @@
init( new BufferedReader( in ) );
}
+
/**
* A constructor which takes an InputStream
*
@@ -345,6 +353,7 @@
init( new BufferedReader( new InputStreamReader( in ) ) );
}
+
/**
* A constructor which takes a File
*
@@ -371,13 +380,14 @@
{
init( new BufferedReader( new FileReader( file ) ) );
}
- catch (FileNotFoundException fnfe)
+ catch ( FileNotFoundException fnfe )
{
LOG.error( "File {} cannot be found", file.getAbsoluteFile() );
throw new NamingException( "Cannot find file " + file.getAbsoluteFile() );
}
}
+
/**
* @return The ldif file version
*/
@@ -386,6 +396,7 @@
return version;
}
+
/**
* @return The maximum size of a file which is used into an attribute value.
*/
@@ -394,6 +405,7 @@
return sizeLimit;
}
+
/**
* Set the maximum file size that can be accepted for an attribute value
*
@@ -405,6 +417,7 @@
this.sizeLimit = sizeLimit;
}
+
// <fill> ::= ' ' <fill> | �
private static void parseFill( char[] document, Position position )
{
@@ -415,6 +428,7 @@
}
}
+
/**
* Parse a number following the rules :
*
@@ -453,6 +467,7 @@
}
}
+
/**
* Parse the changeType
*
@@ -490,6 +505,7 @@
return operation;
}
+
/**
* Parse the DN of an entry
*
@@ -527,7 +543,7 @@
{
dn = new String( Base64.decode( trimmedLine.toCharArray() ), "UTF-8"
);
}
- catch (UnsupportedEncodingException uee)
+ catch ( UnsupportedEncodingException uee )
{
// The DN is not base 64 encoded
LOG.error( "The ldif entry is supposed to have a base 64 encoded
DN" );
@@ -557,7 +573,7 @@
{
LdapDnParser.parseInternal( dn, new ArrayList<Rdn>() );
}
- catch (InvalidNameException ine)
+ catch ( InvalidNameException ine )
{
LOG.error( "The DN {} is not valid" );
throw ine;
@@ -566,6 +582,7 @@
return dn;
}
+
/**
* Parse the value part.
*
@@ -598,6 +615,7 @@
}
}
+
/**
* Parse the value part.
*
@@ -651,7 +669,7 @@
}
else
{
- byte[] data = new byte[(int) length];
+ byte[] data = new byte[( int ) length];
DataInputStream inf = null;
try
@@ -661,7 +679,7 @@
return data;
}
- catch (FileNotFoundException fnfe)
+ catch ( FileNotFoundException fnfe )
{
// We can't reach this point, the file
// existence has already been
@@ -669,7 +687,7 @@
LOG.error( "File {} not found", fileName );
throw new NamingException( "Bad URL, file not found"
);
}
- catch (IOException ioe)
+ catch ( IOException ioe )
{
LOG.error( "File {} error reading", fileName );
throw new NamingException( "Bad URL, file can't be read"
);
@@ -695,7 +713,7 @@
throw new NamingException( "Unsupported URL protocol" );
}
}
- catch (MalformedURLException mue)
+ catch ( MalformedURLException mue )
{
LOG.error( "Bad URL {}", urlName );
throw new NamingException( "Bad URL" );
@@ -712,6 +730,7 @@
}
}
+
/**
* Parse a control. The grammar is : <control> ::= "control:" <fill>
* <ldap-oid> <critical-e> <value-spec-e> <sep>
<critical-e> ::= <spaces>
@@ -766,7 +785,7 @@
{
oid = new OID( oidString );
}
- catch (DecoderException de)
+ catch ( DecoderException de )
{
LOG.error( "The OID {} is not valid", oidString );
throw new NamingException( "Bad control oid" );
@@ -832,7 +851,7 @@
for ( int i = 0; i < length - criticalPos - 1; i++ )
{
- value[i] = (byte) controlValue[i + criticalPos + 1];
+ value[i] = ( byte ) controlValue[i + criticalPos + 1];
}
control.setValue( value );
@@ -842,6 +861,7 @@
return control;
}
+
/**
* Parse an AttributeType/AttributeValue
*
@@ -866,6 +886,7 @@
}
}
+
/**
* Parse an AttributeType/AttributeValue
*
@@ -893,6 +914,7 @@
entry.addAttribute( attributeType, attributeValue );
}
+
/**
* Parse a ModRDN operation
*
@@ -916,8 +938,8 @@
{
int colonIndex = line.indexOf( ':' );
Object attributeValue = parseValue( line, colonIndex );
- entry.setNewRdn( attributeValue instanceof String ? (String) attributeValue
: StringTools
- .utf8ToString( (byte[]) attributeValue ) );
+ entry.setNewRdn( attributeValue instanceof String ? ( String ) attributeValue
: StringTools
+ .utf8ToString( ( byte[] ) attributeValue ) );
}
else
{
@@ -958,6 +980,7 @@
return;
}
+
/**
* Parse a modify change type.
*
@@ -1085,13 +1108,13 @@
if ( attributeValue instanceof String )
{
- attribute.add( (String)attributeValue );
+ attribute.add( ( String ) attributeValue );
}
else
{
- attribute.add( (byte[])attributeValue );
+ attribute.add( ( byte[] ) attributeValue );
}
-
+
isEmptyValue = false;
state = ATTRVAL_SPEC_OR_SEP;
@@ -1099,6 +1122,7 @@
}
}
+
/**
* Parse a change operation. We have to handle different cases depending on
* the operation. 1) Delete : there should *not* be any line after the
@@ -1167,8 +1191,8 @@
{
int colonIndex = line.indexOf( ':' );
Object attributeValue = parseValue( line, colonIndex );
- entry.setNewSuperior( attributeValue instanceof String ? (String)
attributeValue : StringTools
- .utf8ToString( (byte[]) attributeValue ) );
+ entry.setNewSuperior( attributeValue instanceof String ? ( String
) attributeValue
+ : StringTools.utf8ToString( ( byte[] ) attributeValue ) );
}
else
{
@@ -1197,6 +1221,7 @@
}
}
+
/**
* Parse a ldif file. The following rules are processed :
*
@@ -1222,7 +1247,7 @@
String line = lines.get( 0 );
String name = parseDn( line );
-
+
LdapDN dn = new LdapDN( name );
// Ok, we have found a DN
@@ -1352,6 +1377,7 @@
return entry;
}
+
/**
* Parse the version from the ldif input.
*
@@ -1400,7 +1426,7 @@
{
ver = Integer.parseInt( versionNumber );
}
- catch (NumberFormatException nfe)
+ catch ( NumberFormatException nfe )
{
LOG.error( "The version is not a number" );
throw new NamingException( "Ldif parsing error" );
@@ -1410,7 +1436,7 @@
// We have found the version, just discard the line from the list
lines.remove( 0 );
-
+
// and read the next lines if the current buffer is empty
if ( lines.size() == 0 )
{
@@ -1425,6 +1451,7 @@
return ver;
}
+
/**
* Reads an entry in a ldif buffer, and returns the resulting lines, without
* comments, and unfolded.
@@ -1444,7 +1471,7 @@
try
{
- while ( ( line = ( (BufferedReader) reader ).readLine() ) != null )
+ while ( ( line = ( ( BufferedReader ) reader ).readLine() ) != null )
{
if ( line.length() == 0 )
{
@@ -1503,7 +1530,7 @@
}
}
}
- catch (IOException ioe)
+ catch ( IOException ioe )
{
throw new NamingException( "Error while reading ldif lines" );
}
@@ -1517,6 +1544,7 @@
return;
}
+
/**
* Parse a ldif file (using the default encoding).
*
@@ -1531,6 +1559,7 @@
return parseLdifFile( fileName, Charset.forName( StringTools.getDefaultCharsetName()
).toString() );
}
+
/**
* Parse a ldif file, decoding it using the given charset encoding
*
@@ -1557,18 +1586,18 @@
LOG.error( "Cannot parse the file {}, it does not exist", fileName );
throw new NamingException( "Filename " + fileName + " not found." );
}
-
+
BufferedReader reader = null;
// Open the file and then get a channel from the stream
try
{
- reader = new BufferedReader( new InputStreamReader( new FileInputStream( file
),
- Charset.forName( encoding ) ) );
+ reader = new BufferedReader(
+ new InputStreamReader( new FileInputStream( file ), Charset.forName( encoding
) ) );
return parseLdif( reader );
}
- catch (FileNotFoundException fnfe)
+ catch ( FileNotFoundException fnfe )
{
LOG.error( "Cannot find file {}", fileName );
throw new NamingException( "Filename " + fileName + " not found." );
@@ -1578,7 +1607,7 @@
// close the reader
try
{
- reader.close();
+ close();
}
catch ( IOException ioe )
{
@@ -1587,6 +1616,7 @@
}
}
+
/**
* A method which parses a ldif string and returns a list of entries.
*
@@ -1614,12 +1644,13 @@
if ( LOG.isDebugEnabled() )
{
- LOG.debug( "Parsed {} entries.", ( entries == null ? Integer.valueOf( 0 )
: Integer.valueOf( entries.size() ) ) );
+ LOG.debug( "Parsed {} entries.", ( entries == null ? Integer.valueOf( 0 )
: Integer.valueOf( entries
+ .size() ) ) );
}
return entries;
}
- catch (NamingException ne)
+ catch ( NamingException ne )
{
LOG.error( "Cannot parse the ldif buffer : {}", ne.getMessage() );
throw new NamingException( "Error while parsing the ldif buffer" );
@@ -1629,16 +1660,17 @@
// Close the reader
try
{
- reader.close();
+ close();
}
catch ( IOException ioe )
{
// Nothing to do
}
-
+
}
}
+
// ------------------------------------------------------------------------
// Iterator Methods
// ------------------------------------------------------------------------
@@ -1662,7 +1694,7 @@
{
prefetched = parseEntry();
}
- catch (NamingException ne)
+ catch ( NamingException ne )
{
error = ne;
throw new NoSuchElementException( ne.getMessage() );
@@ -1672,7 +1704,7 @@
return entry;
}
- catch (NamingException ne)
+ catch ( NamingException ne )
{
LOG.error( "Premature termination of LDIF iterator" );
error = ne;
@@ -1680,7 +1712,7 @@
}
}
-
+
/**
* Gets the next LDIF on the channel.
*
@@ -1703,7 +1735,7 @@
return null != prefetched;
}
-
+
/**
* Tests to see if another LDIF is on the input channel.
*
@@ -1727,7 +1759,7 @@
throw new UnsupportedOperationException();
}
-
+
/**
* Always throws UnsupportedOperationException!
*
@@ -1738,30 +1770,34 @@
removeInternal();
}
+
/**
* @return An iterator on the file
*/
public Iterator<LdifEntry> iterator()
{
- return new Iterator<LdifEntry>()
+ return new Iterator<LdifEntry>()
{
- public boolean hasNext()
+ public boolean hasNext()
{
return hasNextInternal();
}
-
- public LdifEntry next()
+
+
+ public LdifEntry next()
{
return nextInternal();
}
-
- public void remove()
+
+
+ public void remove()
{
throw new UnsupportedOperationException();
}
};
}
+
/**
* @return True if an error occured during parsing
*/
@@ -1770,6 +1806,7 @@
return error != null;
}
+
/**
* @return The exception that occurs during an entry parsing
*/
@@ -1778,6 +1815,7 @@
return error;
}
+
/**
* The main entry point of the LdifParser. It reads a buffer and returns a
* List of entries.
@@ -1802,7 +1840,7 @@
// When done, get the entries one by one.
try
{
- for ( LdifEntry entry:this )
+ for ( LdifEntry entry : this )
{
if ( entry != null )
{
@@ -1818,6 +1856,7 @@
return entries;
}
+
/**
* @return True if the ldif file contains entries, fals if it contains
* changes
|