directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r406311 - /directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
Date Sun, 14 May 2006 10:52:58 GMT
Author: elecharny
Date: Sun May 14 03:52:55 2006
New Revision: 406311

URL: http://svn.apache.org/viewcvs?rev=406311&view=rev
Log:
- Added a constror which takes a filename
- Added an init method for common actions in constructors
- Forbid changes/entries mixed in a ldif file
- Added a object to store exceptions, because next() can't throw an exception
- Added two methods (hasError and getError) to get info about parsing errors

Modified:
    directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java

Modified: directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
URL: http://svn.apache.org/viewcvs/directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java?rev=406311&r1=406310&r2=406311&view=diff
==============================================================================
--- directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
(original)
+++ directory/branches/elecharny/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
Sun May 14 03:52:55 2006
@@ -178,6 +178,16 @@
 	/** The ldif Reader */
 	private Reader in;
 	
+	/** A flag set if the ldif contains entries */ 
+	private boolean containsEntries;
+	
+	/** A flag set if the ldif contains changes */ 
+	private boolean containsChanges;
+	
+	/** An Exception to handle error message, has Iterator.next() can't 
+	 * throw exceptions */
+	private Exception error;
+	
 	/**
 	 * Constructors
 	 */
@@ -187,16 +197,15 @@
 		position = new Position();
 		version = DEFAULT_VERSION;
 	}
-
-	/**
-	 * Constructors
-	 */
-	public LdifReader( BufferedReader in )  throws NamingException
+	
+	private void init ( BufferedReader in )  throws NamingException
 	{
 		this.in = in;
 		lines = new ArrayList();
 		position = new Position();
 		version = DEFAULT_VERSION;
+		containsChanges = false;
+		containsEntries = false;
 		
 		// First get the version - if any -
 		version = parseVersion();
@@ -204,19 +213,100 @@
 	}
 
 	/**
-	 * Constructors
+	 * A constructor which takes a file name
+	 * @param in A file name containing ldif formated input 
+	 * @throws NamingException If the file cannot be processed or
+	 * if the format is incorrect
+	 */
+	public LdifReader( String ldifFileName )  throws NamingException
+	{
+		File in = new File( ldifFileName );
+		
+		if ( in.exists() == false )
+		{
+			log.error( "File {} cannot be found", in.getAbsoluteFile() );
+			throw new NamingException( "Cannot find file " + in.getAbsoluteFile() );
+		}
+		
+		if (in.canRead() == false )
+		{
+			log.error( "File {} cannot be read", in.getName() );
+			throw new NamingException( "Cannot read file " + in.getName() );
+		}
+		
+		try
+		{
+			init( new BufferedReader( new FileReader( in ) ) );
+		}
+		catch ( FileNotFoundException fnfe )
+		{
+			log.error( "File {} cannot be found", in.getAbsoluteFile() );
+			throw new NamingException( "Cannot find file " + in.getAbsoluteFile() );
+		}
+	}
+
+	/**
+	 * A constructor which takes a BufferedReader
+	 * @param in A BufferedReader containing ldif formated input 
+	 * @throws NamingException If the file cannot be processed or
+	 * if the format is incorrect
+	 */
+	public LdifReader( BufferedReader in )  throws NamingException
+	{
+		init( in );
+	}
+
+	/**
+	 * A constructor which takes a Reader
+	 * @param in A Reader containing ldif formated input 
+	 * @throws NamingException If the file cannot be processed or
+	 * if the format is incorrect
 	 */
 	public LdifReader( Reader in )  throws NamingException
 	{
-		this( new BufferedReader( in ) );
+		init( new BufferedReader( in ) );
 	}
 
 	/**
-	 * Constructors
+	 * A constructor which takes an InputStream
+	 * @param in An InputStream containing ldif formated input 
+	 * @throws NamingException If the file cannot be processed or
+	 * if the format is incorrect
 	 */
 	public LdifReader( InputStream in )  throws NamingException
 	{
-		this( new BufferedReader( new InputStreamReader( in ) ) );
+		init( new BufferedReader( new InputStreamReader( in ) ) );
+	}
+	
+	/**
+	 * A constructor which takes a File
+	 * @param in A File containing ldif formated input 
+	 * @throws NamingException If the file cannot be processed or
+	 * if the format is incorrect
+	 */
+	public LdifReader( File in )  throws NamingException
+	{
+		if ( in.exists() == false )
+		{
+			log.error( "File {} cannot be found", in.getAbsoluteFile() );
+			throw new NamingException( "Cannot find file " + in.getAbsoluteFile() );
+		}
+		
+		if (in.canRead() == false )
+		{
+			log.error( "File {} cannot be read", in.getName() );
+			throw new NamingException( "Cannot read file " + in.getName() );
+		}
+		
+		try
+		{
+			init( new BufferedReader( new FileReader( in ) ) );
+		}
+		catch ( FileNotFoundException fnfe )
+		{
+			log.error( "File {} cannot be found", in.getAbsoluteFile() );
+			throw new NamingException( "Cannot find file " + in.getAbsoluteFile() );
+		}
 	}
 
 	/**
@@ -1002,6 +1092,14 @@
 			// 3) The first line after the DN is anything else
 			if ( lowerLine.startsWith( "control:" ) )
 			{
+				if ( containsEntries )
+				{
+					log.error( "We cannot have changes when reading a file which already contains entries"
);
+					throw new NamingException( "No changes withing entries" );
+				}
+				
+				containsChanges = true;
+
 				if ( controlSeen )
 				{
 					log.error( "We already have had a control" );
@@ -1011,9 +1109,18 @@
 				// Parse the control
 				control = parseControl( line.substring( "control:".length() ) );
 				entry.setControl( control );
+				
 			}
 			else if ( lowerLine.startsWith( "changetype:" ) )
 			{
+				if ( containsEntries )
+				{
+					log.error( "We cannot have changes when reading a file which already contains entries"
);
+					throw new NamingException( "No changes withing entries" );
+				}
+				
+				containsChanges = true;
+
 				if ( changeTypeSeen )
 				{
 					log.error( "We already have had a changeType" );
@@ -1032,6 +1139,14 @@
 			}
 			else if ( line.indexOf( ':' ) > 0 )
 			{
+				if ( containsChanges )
+				{
+					log.error( "We cannot have entries when reading a file which already contains changes"
);
+					throw new NamingException( "No entries within changes" );
+				}
+				
+				containsEntries = true;
+
 				if ( controlSeen || changeTypeSeen )
 				{
 					log.error( "We can't have a Attribute/Value pair after a control or a changeType" );
@@ -1335,15 +1450,24 @@
             
             Entry entry = prefetched;
             readLines();
-            prefetched = parseEntry();
+            
+            try
+            {
+            	prefetched = parseEntry();
+            }
+            catch ( NamingException ne )
+            {
+            	error = ne;
+            }
 
             log.debug( "next(): -- returning ldif {}\n", entry );
 
             return entry;
         }
-        catch ( NamingException e )
+        catch ( NamingException ne )
         {
             log.error( "Premature termination of LDIF iterator" );
+            error = ne;
             return null;
         }
 	}
@@ -1378,6 +1502,21 @@
     	return this;
     }
     
+    /**
+     * @return True if an error occured during parsing
+     */
+    public boolean hasError()
+    {
+    	return error != null;
+    }
+    
+    /**
+     * @return The exception that occurs during an entry parsing
+     */
+    public Exception getError()
+    {
+    	return error;
+    }
     
 	/**
 	 * The main entry point of the LdifParser. It reads a buffer and returns
@@ -1401,7 +1540,16 @@
 		// When done, get the entries one by one.
 		while ( hasNext() )
 		{
-			entries.add( next() );
+			Entry entry = (Entry)next();
+			
+			if ( error != null )
+			{
+				throw new NamingException( "Error while parsing ldif : " + error.getMessage() );
+			}
+			else if ( entry != null ) 
+			{
+				entries.add( entry );
+			}
 		}
 
 		return entries;



Mime
View raw message