Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 26693 invoked from network); 14 May 2006 11:15:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 14 May 2006 11:15:02 -0000 Received: (qmail 14235 invoked by uid 500); 14 May 2006 11:14:37 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 13453 invoked by uid 500); 14 May 2006 11:14:30 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 12777 invoked by uid 99); 14 May 2006 11:14:20 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 14 May 2006 04:14:19 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 14 May 2006 03:53:19 -0700 Received: (qmail 24040 invoked by uid 65534); 14 May 2006 10:52:59 -0000 Message-ID: <20060514105259.24038.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.8 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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;