Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: /antelope/src/ise/antelope/tasks/, /antelope/src/ise/antelope/tasks/typedefs/

antelope
Discussion topic

Back to topic list

CVS update: /antelope/src/ise/antelope/tasks/, /antelope/src/ise/antelope/tasks/typedefs/

Author danson
Full name Dale Anson
Date 2005-10-21 11:59:19 PDT
Message User: danson
Date: 2005/10/21 11:59:19

Added:
   antelope/src/ise/ant​elope/tasks/typedefs​/FileLineCount.java

Modified:
   antelope/src/ise/ant​elope/tasks/FileUtil​Task.java
   antelope/src/ise/ant​elope/tasks/SplitTas​k.java
   antelope/src/ise/ant​elope/tasks/antelope​.taskdefs
   antelope/src/ise/ant​elope/tasks/antlib.x​ml

Log:
 updates for SplitTask and tests.
 

File Changes:

Directory: /antelope/src/ise/an​telope/tasks/
====================​====================​====

File [changed]: FileUtilTask.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/tasks/FileUti​lTask.java?r1=1.1​&r2=1.2
Delta lines: +8 -1
-------------------
--- FileUtilTask.java 21 Oct 2005 03:47:45 -0000 1.1
+++ FileUtilTask.java 21 Oct 2005 18:59:16 -0000 1.2
@@ -13,7 +13,7 @@
  *
  * Copyright 2003
  *
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
  */
 public class FileUtilTask extends Task {
 
@@ -101,6 +101,13 @@
      * What files are in the directory?
      */
     public void addListFiles(FileList op) {
+ ops.add(op);
+ }
+
+ /**
+ * How many lines are in the file?
+ */
+ public void addLineCount(FileLineCount op) {
         ops.add(op);
     }
     

File [changed]: SplitTask.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/tasks/SplitTa​sk.java?r1=1.1&r​2=1.2
Delta lines: +129 -116
-----------------------
--- SplitTask.java 21 Oct 2005 03:47:45 -0000 1.1
+++ SplitTask.java 21 Oct 2005 18:59:16 -0000 1.2
@@ -7,8 +7,11 @@
 import org.apache.tools.ant​.taskdefs.*;
 
 /**
+ * This task is similar to the Unix/Linux split utility, it splits a file into
+ * a number of smaller pieces. It will also split a property value or a string.
+ *
  * @author Dale Anson
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
  * @since Ant 1.6
  */
 public class SplitTask extends Task {
@@ -23,113 +26,134 @@
 
 
     /**
- * Sets the prefix attribute of the SplitTask object
+ * The start of the file names to write. Files are named using this string,
+ * followed by a "." and a number, e.g. x.0, x.1, etc. The Unix/Linux split
+ * uses a letter scheme for the suffix, that is not supported here.
+ *
+ * Should the dot go away? If the user wants a dot, it could be part of this
+ * attribute. Right now, the dot is there, and there is no way for the user
+ * to make it go away.
      *
      * @param x The new prefix value
      */
- public void setPrefix(String x) {
+ public void setPrefix( String x ) {
         prefix = x;
     }
 
     /**
- * Use bytes or lines, not both. In general, use bytes for binary files,
- * lines for text.
+ * Set the number of bytes per part. This is not a required parameter,
+ * the default is to use lines rather than bytes.
+ *
+ * Use bytes or lines, not both. In general, use bytes or size for binary
+ * files, lines for text files.
      *
      * @param b number of bytes per part.
      */
- public void setBytes(int b) {
+ public void setBytes( int b ) {
         bytes = b;
         lines = -1;
     }
 
     /**
      * The linux split command allows modifiers: b for 512, k for 1K, m for 1
- * Meg.
+ * Meg. Use this method for a similar effect. This is not a required
+ * parameter, the default is to use lines rather than size.
+ *
+ * Use bytes or lines, not both. In general, use bytes or size for binary
+ * files, lines for text files.
      *
      * @param b the number of bytes per part, with an optional modifier. If
      * there is no modifier, treat same as setBytes(int). For example,
      * setSize("100k") is the same as setBytes(100 * 1024).
      */
- public void setSize(String b) {
- if (b == null)
- return;
- if (b.length() == 0)
- return;
+ public void setSize( String b ) {
+ if ( b == null || b.length() == 0 )
+ return ;
         b = b.toLowerCase();
- String modifier = b.substring(b.length() - 1);
+ String modifier = b.substring( b.length() - 1 );
         int multiplier = 1;
- b = b.substring(0, b.length() - 1);
- if (modifier.equals("b")) {
+ b = b.substring( 0, b.length() - 1 );
+ if ( modifier.equals( "b" ) ) {
             multiplier = 512;
         }
- else if (modifier.equals("k")) {
+ else if ( modifier.equals( "k" ) ) {
             multiplier = 1024;
         }
- else if (modifier.equals("m")) {
+ else if ( modifier.equals( "m" ) ) {
             multiplier = 1024 * 1024;
         }
         else {
+ // modifier is not recognized, so put it back, maybe it's a number
             b = b + modifier;
         }
         try {
- int size = Integer.parseInt(b) * multiplier;
- setBytes(size);
- return;
+ int size = Integer.parseInt( b ) * multiplier;
+ setBytes( size );
         }
- catch (NumberFormatException e) {
- throw new BuildException("Invalid bytes parameter.");
+ catch ( NumberFormatException e ) {
+ throw new BuildException( "Invalid size parameter." );
         }
     }
 
     /**
- * Use bytes or lines, not both. In general, use bytes for binary files,
- * lines for text.
+ * Set the number of lines per part, default is 1000. This is not a required
+ * parameter, but is the default setting for splitting.
+ *
+ * Use bytes or lines, not both. In general, use bytes or size for binary
+ * files, lines for text files.
      *
- * @param x The new lines value
+ * @param x The number of lines per part.
      */
- public void setLines(int x) {
+ public void setLines( int x ) {
         lines = x;
         bytes = -1;
     }
 
     /**
- * Split the text content of value of the given property.
+ * Split the text value of the given property.
+ *
+ * One of property, value, or file are required.
      *
      * @param p the name of the property whose value will be split.
      */
- public void setProperty(String p) {
- value = getProject().getProperty(p);
- if (value == null || value.equals(""))
- throw new BuildException("Property " + p + " has no value.");
+ public void setProperty( String p ) {
+ String v = getProject().getProperty( p );
+ if ( v == null || v.equals( "" ) )
+ throw new BuildException( "Property " + p + " has no value." );
+ setValue( v );
     }
 
     /**
      * Split the given string.
      *
+ * One of property, value, or file are required.
+ *
      * @param v a string
      */
- public void setValue(String v) {
- if (v == null || v.equals(""))
- throw new BuildException("Value is null or empty.");
+ public void setValue( String v ) {
+ if ( v == null || v.equals( "" ) )
+ throw new BuildException( "Value is null or empty." );
         value = v;
     }
 
     /**
      * Split the contents of the given file.
      *
+ * One of property, value, or file are required.
+ *
      * @param f the name of the file
      */
- public void setFile(File f) {
+ public void setFile( File f ) {
         file = f;
     }
 
     /**
- * Where to put the parts. If file has been set and output dir not set,
- * output to directory containing file.
+ * Where to put the parts. If file has been set and output directory has not
+ * set, output to directory containing file.
      *
      * @param d the output directory
      */
- public void setOutputdir(File d) {
+ public void setOutputdir( File d ) {
         outputDir = d;
     }
 
@@ -139,68 +163,64 @@
      *
      * @param fail true or false
      */
- public void setFailonerror(boolean fail) {
+ public void setFailonerror( boolean fail ) {
         failOnError = fail;
     }
 
 
     /**
- * Split the given property or file into pieces.
+ * Split the given property, value, or file into pieces.
      *
      * @exception BuildException only if failOnError is true
      */
     public void execute() throws BuildException {
         // check params --
         // must have value or file
- if (value == null && file == null)
- throw new BuildException("Must have property, value, or file.");
+ if ( value == null && file == null )
+ throw new BuildException( "Must have property, value, or file." );
         // if no file, must have outputDir
- if (file == null && outputDir == null)
- throw new BuildException("Must have output directory.");
+ if ( file == null && outputDir == null )
+ throw new BuildException( "Must have output directory." );
         // must have only one of value or file
- if (value != null && file != null)
- throw new BuildException("Must not have more than one of property, value, or file.");
+ if ( value != null && file != null )
+ throw new BuildException( "Must not have more than one of property, value, or file." );
 
         try {
- if (value != null) {
+ if ( value != null )
                 splitValue();
- }
- else {
+ else
                 splitFile();
             }
- }
- catch (Exception e) {
- e.printStackTrace();
- if (failOnError) {
- throw new BuildException(e.getMessage());
- }
+ catch ( Exception e ) {
+ if ( failOnError )
+ throw new BuildException( e.getMessage() );
             else
- log(e.getMessage());
+ log( e.getMessage() );
         }
     }
 
     /**
- * Description of the Method
+ * Split a string value into several files.
      *
- * @exception Exception Description of Exception
+ * @exception IOException if there is an i/o problem
      */
     private void splitValue() throws Exception {
- if (!outputDir.exists() && !outputDir.mkdirs()) {
- throw new IOException("Unable to create output directory.");
+ if ( !outputDir.exists() && !outputDir.mkdirs() ) {
+ throw new IOException( "Unable to create output directory." );
         }
 
- StringReader reader = new StringReader(value);
+ StringReader reader = new StringReader( value );
         int bytes_read = 0;
         int suffix = 0;
- if (bytes > 0) {
+ if ( bytes > 0 ) {
             // make files all the same number of bytes
- char[] buffer = new char[bytes];
- while (bytes_read > -1) {
- bytes_read = reader.read(buffer, 0, bytes);
- if (bytes_read == -1)
+ char[] buffer = new char[ bytes ];
+ while ( bytes_read > -1 ) {
+ bytes_read = reader.read( buffer, 0, bytes );
+ if ( bytes_read == -1 )
                     break;
- FileWriter fw = new FileWriter(new File(outputDir, prefix + "." + String.valueOf(suffix)));
- fw.write(buffer, 0, bytes_read);
+ FileWriter fw = new FileWriter( new File( outputDir, prefix + "." + String.valueOf( suffix ) ) );
+ fw.write( buffer, 0, bytes_read );
                 fw.flush();
                 fw.close();
                 ++suffix;
@@ -208,52 +228,38 @@
         }
         else {
             // make files all the same number of lines
- // reusing offset as line count
- LineNumberReader lnr = new LineNumberReader(reader);
- String line = lnr.readLine();
- FileWriter fw = new FileWriter(new File(outputDir, prefix + "." + String.valueOf(suffix)));
- while (line != null) {
- fw.write(line);
- if (lnr.getLineNumber() % lines == 0) {
- fw.flush();
- fw.close();
- ++suffix;
- fw = new FileWriter(new File(outputDir, prefix + "." + String.valueOf(suffix)));
- }
- }
- fw.flush();
- fw.close();
+ splitByLines( reader );
         }
     }
 
     /**
- * Description of the Method
+ * Split a file into several files.
      *
- * @exception Exception Description of Exception
+ * @exception IOException if there is an i/o problem
      */
- private void splitFile() throws Exception {
- if (!file.exists())
- throw new FileNotFoundExceptio​n(file.toString());
- if (file.length() == 0)
- throw new BuildException("Zero length file.");
- if (outputDir == null)
+ private void splitFile() throws IOException {
+ if ( !file.exists() )
+ throw new FileNotFoundException( file.toString() );
+ if ( file.length() == 0 )
+ throw new BuildException( "Zero length file." );
+ if ( outputDir == null )
             outputDir = file.getParentFile();
- if (!outputDir.exists() && !outputDir.mkdirs()) {
- throw new IOException("Unable to create output directory.");
+ if ( !outputDir.exists() && !outputDir.mkdirs() ) {
+ throw new IOException( "Unable to create output directory." );
         }
 
         int suffix = 0;
- if (bytes > 0) {
+ if ( bytes > 0 ) {
             // make files all the same number of bytes
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
+ BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );
             int bytes_read = 0;
- byte[] buffer = new byte[bytes];
- while (bytes_read > -1) {
- bytes_read = bis.read(buffer, 0, bytes);
- if (bytes_read == -1)
+ byte[] buffer = new byte[ bytes ];
+ while ( bytes_read > -1 ) {
+ bytes_read = bis.read( buffer, 0, bytes );
+ if ( bytes_read == -1 )
                     break;
- FileOutputStream fos = new FileOutputStream(new File(outputDir, prefix + "." + String.valueOf(suffix)));
- fos.write(buffer, 0, bytes_read);
+ FileOutputStream fos = new FileOutputStream( new File( outputDir, prefix + "." + String.valueOf( suffix ) ) );
+ fos.write( buffer, 0, bytes_read );
                 fos.flush();
                 fos.close();
                 ++suffix;
@@ -261,23 +267,30 @@
         }
         else {
             // make files all the same number of lines
- // reusing offset as line count
- LineNumberReader lnr = new LineNumberReader(new FileReader(file));
+ splitByLines( new FileReader( file ) );
+ }
+ }
+
+ private void splitByLines( Reader reader ) throws IOException {
+ int suffix = 0;
+ LineNumberReader lnr = new LineNumberReader( reader );
             String line = lnr.readLine();
- FileWriter fw = new FileWriter(new File(outputDir, prefix + "." + String.valueOf(suffix)));
- while (line != null) {
- fw.write(line);
- if (lnr.getLineNumber() % lines == 0) {
- fw.flush();
- fw.close();
+ BufferedWriter writer = new BufferedWriter( new FileWriter( new File( outputDir, prefix + "." + String.valueOf( suffix ) ) ) );
+ while ( line != null ) {
+ writer.write( line );
+ writer.newLine();
+ if ( lnr.getLineNumber() % lines == 0 ) {
+ writer.flush();
+ writer.close();
                     ++suffix;
- fw = new FileWriter(new File(outputDir, prefix + "." + String.valueOf(suffix)));
+ writer = new BufferedWriter( new FileWriter( new File( outputDir, prefix + "." + String.valueOf( suffix ) ) ) );
                 }
+ line = lnr.readLine();
             }
- fw.flush();
- fw.close();
- }
+ writer.flush();
+ writer.close();
     }
+
 }
 
 

File [changed]: antelope.taskdefs
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/tasks/antelop​e.taskdefs?r1=1.4​&r2=1.5
Delta lines: +1 -0
-------------------
--- antelope.taskdefs 21 Oct 2005 03:38:27 -0000 1.4
+++ antelope.taskdefs 21 Oct 2005 18:59:16 -0000 1.5
@@ -60,6 +60,7 @@
 canwrite=ise.antelop​e.tasks.typedefs.Can​Write
 filecount=ise.antelo​pe.tasks.typedefs.Fi​leCount
 filelength=ise.antel​ope.tasks.typedefs.F​ileLength
+linecount=ise.antel​ope.tasks.typedefs.F​ileLineCount
 listfiles=ise.antelo​pe.tasks.typedefs.Fi​leList
 isdirectory=ise.ante​lope.tasks.typedefs.​IsDirectory
 isfile=ise.antelope.​tasks.typedefs.IsFil​e

File [changed]: antlib.xml
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/tasks/antlib.​xml?r1=1.4&r2=1.​5
Delta lines: +1 -0
-------------------
--- antlib.xml 21 Oct 2005 03:38:27 -0000 1.4
+++ antlib.xml 21 Oct 2005 18:59:16 -0000 1.5
@@ -67,6 +67,7 @@
    <typedef name="canwrite" classname="ise.antel​ope.tasks.typedefs.C​anWrite"/>
    <typedef name="filecount" classname="ise.antel​ope.tasks.typedefs.F​ileCount"/>
    <typedef name="filelength" classname="ise.antel​ope.tasks.typedefs.F​ileLength"/>
+ <typedef name="linecount" classname="ise.antel​ope.tasks.typedefs.F​ileLineCount"/>
    <typedef name="listfiles" classname="ise.antel​ope.tasks.typedefs.F​ileList"/>
    <typedef name="isdirectory" classname="ise.antel​ope.tasks.typedefs.I​sDirectory"/>
    <typedef name="isfile" classname="ise.antel​ope.tasks.typedefs.I​sFile"/>

Directory: /antelope/src/ise/an​telope/tasks/typedef​s/
====================​====================​=============

File [added]: FileLineCount.java
Url: http://antelope.tigr​is.org/source/browse​/antelope/src/ise/an​telope/tasks/typedef​s/FileLineCount.java​?rev=1.1&content​-type=text/vnd.viewc​vs-markup
Added lines: 48
---------------
package ise.antelope.tasks.typedefs;

import java.io.*;
import org.apache.tools.ant​.BuildException;

/**
 * Copyright 2003
 *
 * @version $Revision: 1.1 $
 */
public class FileLineCount implements FileOp {
    
    /**
     * Description of the Method
     *
     * @param f a file
     * @return the number of lines in the file
     */
    public String execute(File f) {
        if (f == null)
            throw new IllegalArgumentException("file cannot be null");
        System.out.println(f);
        LineNumberReader lnr = null;
        try {
            lnr = new LineNumberReader(new FileReader(f));
            String line = lnr.readLine();
            while(line != null) {
                line = lnr.readLine();
            }
            return String.valueOf(lnr.g​etLineNumber());
        }
        catch(Exception e) {
            throw new BuildException(e.getMessage());
        }
        finally {
            try {
                if (lnr != null)
                    lnr.close();
            }
            catch(Exception e) {
                // ignored
            }
        }
        
    }
}

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

CVS update: /antelope/src/ise/antelope/tasks/, /antelope/src/ise/antelope/tasks/typedefs/ danson Dale Anson 2005-10-21 11:59:19 PDT
Messages per page: