ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs FixCRLF.java
Date Sun, 05 Aug 2001 22:17:26 GMT
conor       01/08/05 15:17:26

  Modified:    src/main/org/apache/tools/ant DefaultLogger.java
               src/main/org/apache/tools/ant/taskdefs FixCRLF.java
  Log:
  Only print the final stack trace if in verbose or debug mode, otherwise
  just print exception message
  
  Revision  Changes    Path
  1.15      +6 -1      jakarta-ant/src/main/org/apache/tools/ant/DefaultLogger.java
  
  Index: DefaultLogger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/DefaultLogger.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- DefaultLogger.java	2001/02/13 12:31:58	1.14
  +++ DefaultLogger.java	2001/08/05 22:17:26	1.15
  @@ -138,7 +138,12 @@
           else {
               err.println(lSep + "BUILD FAILED" + lSep);
   
  -            error.printStackTrace(err);
  +            if (Project.MSG_VERBOSE <= msgOutputLevel) {
  +                error.printStackTrace(err);
  +            }
  +            else {
  +                err.println(error.getMessage());
  +            }
           }
   
           out.println(lSep + "Total time: " + formatTime(System.currentTimeMillis() - startTime));
  
  
  
  1.17      +141 -16   jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  
  Index: FixCRLF.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- FixCRLF.java	2001/08/03 16:15:16	1.16
  +++ FixCRLF.java	2001/08/05 22:17:26	1.17
  @@ -53,16 +53,20 @@
    */
   
   package org.apache.tools.ant.taskdefs;
  -
  +import org.apache.tools.ant.Project;
   import org.apache.tools.ant.BuildException;
   import org.apache.tools.ant.DirectoryScanner;
  -import org.apache.tools.ant.Project;
   import org.apache.tools.ant.types.EnumeratedAttribute;
   
   import java.io.*;
   import java.util.*;
   
   /**
  + * FixCRLF.java
  + *
  + * Based on FixCR.java
  + * by Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>.
  + *
    * Task to convert text source files to local OS formatting conventions, as
    * well as repair text files damaged by misconfigured or misguided editors or
    * file transfer programs.
  @@ -101,7 +105,7 @@
    *
    * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
    * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  - * @version $Revision: 1.16 $ $Name:  $
  + * @version $Revision: 1.17 $ $Name:  $
    */
   
   public class FixCRLF extends MatchingTask {
  @@ -133,6 +137,7 @@
       private StringBuffer linebuf = new StringBuffer(1024);
       private StringBuffer linebuf2 = new StringBuffer(1024);
       private int eol;
  +    private int addcr = UNDEF;
       private String eolstr;
       private int ctrlz;
       private int tabs;
  @@ -307,13 +312,12 @@
               ctrlz = ADD;
           }
       }
  -
       /**
        * Executes the task.
        */
       public void execute() throws BuildException {
           // first off, make sure that we've got a srcdir and destdir
  -
  +	
           if (srcDir == null) {
               throw new BuildException("srcdir attribute must be set!");
           }
  @@ -332,6 +336,57 @@
               }
           }
   
  +	// Set up the correct EOL values
  +	if (eol == UNDEF) {
  +	    if (addcr == UNDEF) {
  +		// Neither eol not addcr has been defined
  +		// go for the system defaults
  +		if (System.getProperty("line.separator").equals("\r")) {
  +		    eol = CR;
  +		} else if (System.getProperty("line.separator").equals("\n")) {
  +		    eol = LF;
  +		} else {
  +		    eol = CRLF;
  +		}
  +		
  +	    } // end of if (addcr == UNDEF)
  +	    else {
  +		// addcr has been defined - translate to eol values
  +		switch (addcr) {
  +		case ADD:
  +		    eol = CRLF;
  +		    break;
  +		    
  +		case REMOVE:
  +		    eol = LF;
  +		    break;
  +		    
  +		case ASIS:
  +		    eol = ASIS;
  +		    break;
  +
  +		} // end of switch (addcr)
  +		
  +	    } // end of if (addcr == UNDEF)else
  +	    
  +	} // end of if (eol == UNDEF)
  +
  +	switch (eol) {
  +	    // set eolstr value unless ASIS
  +	case CR:
  +	    eolstr = new String("\r");
  +	    break;
  +	    
  +	case LF:
  +	    eolstr = new String("\n");
  +	    break;
  +	    
  +	case CRLF:
  +	    eolstr = new String("\r\n");
  +	    break;
  +	    
  +	} // end of switch (eol)
  +	
           // log options used
           log("options:" +
               " eol=" +
  @@ -362,6 +417,49 @@
           }
       }
   
  +    /**
  +     * Checks for the inequality of two files
  +     */
  +    private boolean filesEqual(File file1, File file2) {
  +        BufferedReader reader1;
  +        BufferedReader reader2;
  +        char buf1[] = new char[INBUFLEN];
  +        char buf2[] = new char[INBUFLEN];
  +        int buflen;
  +
  +        if (file1.length() != file2.length()) {
  +            return false;
  +        }
  +        
  +        try {
  +             reader1 = new BufferedReader
  +                     (new FileReader(file1), INBUFLEN);
  +             reader2 = new BufferedReader
  +                     (new FileReader(file2), INBUFLEN);
  +             while ((buflen = reader1.read(buf1, 0, INBUFLEN)) != -1 ) {
  +                 reader2.read(buf2, 0, INBUFLEN);
  +                 // Compare the contents of the buffers
  +                 // There must be an easier way to do this, but I don''t
  +                 // know what it is
  +                 for (int i = 0; i < buflen; i++) {
  +                     if (buf1[i] != buf2[i]) {
  +                         reader1.close();
  +                         reader2.close();
  +                         return false;
  +                     } // end of if (buf1[i] != buf2[i])
  +                 }
  +             }
  +             reader1.close();
  +             reader2.close();
  +             return true;   // equal
  +        } catch (IOException e) {
  +            throw new BuildException("IOException in filesEqual: " +
  +                                      file1 + " : " + file2);
  +        } // end of try-catch
  +                 
  +    }
  +
  +
       private void processFile(String file) throws BuildException {
           File srcFile = new File(srcDir, file);
           File tmpFile = null;
  @@ -515,20 +613,47 @@
               catch (IOException e) {
                   throw new BuildException("Unable to close source file " + srcFile);
               }
  -                                     
  +
               if (destFile.exists()) {
  -                if (!destFile.delete()) {
  -                    throw new BuildException("Unable to delete " + destFile);
  +                // Compare the destination with the temp file
  +                System.out.println("destFile exists");
  +                if ( ! filesEqual(destFile, tmpFile)) {
  +                    System.out.println("destFile exists: files not equal");
  +                    log(destFile + " is being written", Project.MSG_VERBOSE);
  +                    if (!destFile.delete()) {
  +                        throw new BuildException("Unable to delete "
  +                                                 + destFile);
  +                    }
  +                    if (!tmpFile.renameTo(destFile)) {
  +                        throw new BuildException(
  +                                "Failed to transform " + srcFile
  +                                + " to " + destFile
  +                                + ". Couldn't rename temporary file: "
  +                                + tmpFile);
  +                    }
  +
  +                } else { // destination is equal to temp file
  +                    System.out.println("destFile exists: files equal");
  +                    log(destFile +
  +                        " is not written, as the contents are identical",
  +                        Project.MSG_VERBOSE);
  +                    if (!tmpFile.delete()) {
  +                        throw new BuildException("Unable to delete "
  +                                                 + destFile);
  +                    }
                   }
  -            }
  -                                     
  -            if (!tmpFile.renameTo(destFile)) {
  -                throw new BuildException("Failed to transform " + srcFile
  -                                         + " to " + destFile
  -                                         + ". Couldn't rename temporary file: " + tmpFile);
  -            } else {
  -                tmpFile = null;
  +            } else { // destFile does not exist - write the temp file
  +                System.out.println("destFile does not exist");
  +                if (!tmpFile.renameTo(destFile)) {
  +                    throw new BuildException(
  +                            "Failed to transform " + srcFile
  +                            + " to " + destFile
  +                            + ". Couldn't rename temporary file: "
  +                            + tmpFile);
  +                }
               }
  +
  +            tmpFile = null;
   
           } finally {
               try {
  
  
  

Mime
View raw message