Browse Source

Javadoc & changed File constructors from

new File(f1.getAbsolutePath() + File.separator + f2.getName()) to
new File(f1, f2.getName())


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277587 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 20 years ago
parent
commit
9116e41153
1 changed files with 50 additions and 63 deletions
  1. +50
    -63
      src/main/org/apache/tools/ant/taskdefs/optional/image/Image.java

+ 50
- 63
src/main/org/apache/tools/ant/taskdefs/optional/image/Image.java View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002,2004-2005 The Apache Software Foundation
* Copyright 2002, 2004-2005 The Apache Software Foundation
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -37,40 +37,43 @@ import java.util.Iterator;
import java.util.Vector; import java.util.Vector;


/** /**
* A MatchingTask which relies on <A
* HREF="http://java.sun.com/products/java-media/jai">JAI (Java
* Advanced Imaging)</A> to perform image manipulation operations on
* A MatchingTask which relies on <a
* href="http://java.sun.com/products/java-media/jai">JAI (Java
* Advanced Imaging)</a> to perform image manipulation operations on
* existing images. The operations are represented as ImageOperation * existing images. The operations are represented as ImageOperation
* DataType objects. The operations are arranged to conform to the * DataType objects. The operations are arranged to conform to the
* Chaining Model of JAI. Check out the <A
* HREF="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/">
* JAI Programming Guide</A>
* Chaining Model of JAI. Check out the <a
* href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/">
* JAI Programming Guide</a>.
* *
* @see org.apache.tools.ant.types.optional.image.ImageOperation * @see org.apache.tools.ant.types.optional.image.ImageOperation
* @see org.apache.tools.ant.types.DataType * @see org.apache.tools.ant.types.DataType
*/ */
public class Image extends MatchingTask { public class Image extends MatchingTask {
protected Vector instructions = new Vector(); protected Vector instructions = new Vector();
protected String str_encoding = "JPEG";
protected boolean overwrite = false; protected boolean overwrite = false;
protected boolean garbage_collect = false;
private boolean failonerror = true;
protected Vector filesets = new Vector(); protected Vector filesets = new Vector();


protected File srcDir = null; protected File srcDir = null;
protected File destDir = null; protected File destDir = null;


//cannot remove underscores due to protected visibility >:(
protected String str_encoding = "JPEG";
protected boolean garbage_collect = false;

private boolean failonerror = true;

/** /**
* Adds a set of files to be deleted.
* Add a set of files to be deleted.
* @param set the FileSet to add.
*/ */
public void addFileset(FileSet set) { public void addFileset(FileSet set) {
filesets.addElement(set); filesets.addElement(set);
} }


/** /**
* Set whether to fail on error.
* If false, note errors to the output but keep going. * If false, note errors to the output but keep going.
* @param failonerror true or false
* @param failonerror true or false.
*/ */
public void setFailOnError(boolean failonerror) { public void setFailOnError(boolean failonerror) {
this.failonerror = failonerror; this.failonerror = failonerror;
@@ -78,55 +81,58 @@ public class Image extends MatchingTask {


/** /**
* Set the source dir to find the image files. * Set the source dir to find the image files.
* @param srcDir the directory in which the image files reside.
*/ */
public void setSrcdir(File srcDir) { public void setSrcdir(File srcDir) {
this.srcDir = srcDir; this.srcDir = srcDir;
} }


/** /**
* Set the image encoding type. <A
* HREF="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56610">
* See this table in the JAI Programming Guide</A>.
* Set the image encoding type. <a
* href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56610">
* See this table in the JAI Programming Guide</a>.
* @param encoding the String image encoding.
*/ */
public void setEncoding(String encoding) { public void setEncoding(String encoding) {
str_encoding = encoding; str_encoding = encoding;
} }


/** /**
* Sets whether or not to overwrite a file if there is a naming conflict.
* Set whether to overwrite a file if there is a naming conflict.
* @param overwrite whether to overwrite.
*/ */
public void setOverwrite(boolean overwrite) { public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite; this.overwrite = overwrite;
} }


/** /**
* Enables Garbage Collection after each image processed.
* Defaults to false.
* Set whether to invoke Garbage Collection after each image processed.
* Defaults to false.
* @param gc whether to invoke the garbage collector.
*/ */
public void setGc(boolean gc) { public void setGc(boolean gc) {
garbage_collect = gc; garbage_collect = gc;
} }



/** /**
* Sets the destination directory for manipulated images.
* @param destDir The destination directory
* Set the destination directory for manipulated images.
* @param destDir The destination directory.
*/ */
public void setDestDir(File destDir) { public void setDestDir(File destDir) {
this.destDir = destDir; this.destDir = destDir;
} }


/** /**
* Adds an ImageOperation to chain.
* @param instr The ImageOperation to append to the chain
* Add an ImageOperation to chain.
* @param instr The ImageOperation to append to the chain.
*/ */
public void addImageOperation(ImageOperation instr) { public void addImageOperation(ImageOperation instr) {
instructions.add(instr); instructions.add(instr);
} }


/** /**
* Adds a Rotate ImageOperation to the chain
* @param instr The Rotate operation to add to the chain
* Add a Rotate ImageOperation to the chain.
* @param instr The Rotate operation to add to the chain.
* @see org.apache.tools.ant.types.optional.image.Rotate * @see org.apache.tools.ant.types.optional.image.Rotate
*/ */
public void addRotate(Rotate instr) { public void addRotate(Rotate instr) {
@@ -134,8 +140,8 @@ public class Image extends MatchingTask {
} }


/** /**
* Adds a Scale ImageOperation to the chain
* @param instr The Scale operation to add to the chain
* Add a Scale ImageOperation to the chain.
* @param instr The Scale operation to add to the chain.
* @see org.apache.tools.ant.types.optional.image.Scale * @see org.apache.tools.ant.types.optional.image.Scale
*/ */
public void addScale(Scale instr) { public void addScale(Scale instr) {
@@ -143,9 +149,9 @@ public class Image extends MatchingTask {
} }


/** /**
* Adds a Draw ImageOperation to the chain. DrawOperation
* DataType objects can be nested inside the Draw object
* @param instr The Draw operation to add to the chain
* Add a Draw ImageOperation to the chain. DrawOperation
* DataType objects can be nested inside the Draw object.
* @param instr The Draw operation to add to the chain.
* @see org.apache.tools.ant.types.optional.image.Draw * @see org.apache.tools.ant.types.optional.image.Draw
* @see org.apache.tools.ant.types.optional.image.DrawOperation * @see org.apache.tools.ant.types.optional.image.DrawOperation
*/ */
@@ -153,11 +159,9 @@ public class Image extends MatchingTask {
instructions.add(instr); instructions.add(instr);
} }



/** /**
* Adds an ImageOperation to chain.
* Adds an ImageOperation to the chain.
* @param instr The ImageOperation to append to the chain
* Add an ImageOperation to chain.
* @param instr The ImageOperation to append to the chain.
* @since Ant 1.7 * @since Ant 1.7
*/ */
public void add(ImageOperation instr) { public void add(ImageOperation instr) {
@@ -167,7 +171,7 @@ public class Image extends MatchingTask {
/** /**
* Executes all the chained ImageOperations on the file * Executes all the chained ImageOperations on the file
* specified. * specified.
* @param file The file to be processed
* @param file The file to be processed.
*/ */
public void processFile(File file) { public void processFile(File file) {
try { try {
@@ -190,26 +194,20 @@ public class Image extends MatchingTask {
} else if (str_encoding.toLowerCase().equals("tif")) { } else if (str_encoding.toLowerCase().equals("tif")) {
str_encoding = "TIFF"; str_encoding = "TIFF";
} }

if (destDir == null) { if (destDir == null) {
destDir = srcDir; destDir = srcDir;
} }

File new_file = new File(destDir.getAbsolutePath()
+ File.separator + file.getName());
File new_file = new File(destDir, file.getName());


if ((overwrite && new_file.exists()) && (!new_file.equals(file))) { if ((overwrite && new_file.exists()) && (!new_file.equals(file))) {
new_file.delete(); new_file.delete();
} }

FileOutputStream stream = new FileOutputStream(new_file); FileOutputStream stream = new FileOutputStream(new_file);


JAI.create("encode", image, stream, str_encoding.toUpperCase(), JAI.create("encode", image, stream, str_encoding.toUpperCase(),
null); null);
stream.flush(); stream.flush();
stream.close(); stream.close();


} catch (IOException err) { } catch (IOException err) {
if (!failonerror) { if (!failonerror) {
log("Error processing file: " + err); log("Error processing file: " + err);
@@ -223,11 +221,11 @@ public class Image extends MatchingTask {
throw new BuildException(rerr); throw new BuildException(rerr);
} }
} }

} }


/** /**
* Executes the Task
* Executes the Task.
* @throws BuildException on error.
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {


@@ -238,15 +236,13 @@ public class Image extends MatchingTask {
String[] files = null; String[] files = null;
ArrayList filesList = new ArrayList(); ArrayList filesList = new ArrayList();



// deal with specified srcDir // deal with specified srcDir
if (srcDir != null) { if (srcDir != null) {
ds = super.getDirectoryScanner(srcDir); ds = super.getDirectoryScanner(srcDir);


files = ds.getIncludedFiles(); files = ds.getIncludedFiles();
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
filesList.add(new File(srcDir.getAbsolutePath()
+ File.separator + files[i]));
filesList.add(new File(srcDir, files[i]));
} }
} }
// deal with the filesets // deal with the filesets
@@ -256,26 +252,21 @@ public class Image extends MatchingTask {
files = ds.getIncludedFiles(); files = ds.getIncludedFiles();
File fromDir = fs.getDir(getProject()); File fromDir = fs.getDir(getProject());
for (int j = 0; j < files.length; j++) { for (int j = 0; j < files.length; j++) {
filesList.add(new File(fromDir.getAbsolutePath()
+ File.separator + files[j]));
filesList.add(new File(fromDir, files[j]));
} }
} }

if (!overwrite) { if (!overwrite) {
// remove any files that shouldn't be overwritten. // remove any files that shouldn't be overwritten.
ArrayList filesToRemove = new ArrayList(); ArrayList filesToRemove = new ArrayList();
for (Iterator i = filesList.iterator(); i.hasNext();) { for (Iterator i = filesList.iterator(); i.hasNext();) {
File f = (File) i.next(); File f = (File) i.next();
File new_file = new File(destDir.getAbsolutePath()
+ File.separator + f.getName());
File new_file = new File(destDir, f.getName());
if (new_file.exists()) { if (new_file.exists()) {
filesToRemove.add(f); filesToRemove.add(f);
} }
} }
filesList.removeAll(filesToRemove); filesList.removeAll(filesToRemove);
} }


// iterator through all the files and process them. // iterator through all the files and process them.
for (Iterator i = filesList.iterator(); i.hasNext();) { for (Iterator i = filesList.iterator(); i.hasNext();) {
File file = (File) i.next(); File file = (File) i.next();
@@ -285,30 +276,26 @@ public class Image extends MatchingTask {
System.gc(); System.gc();
} }
} }


} catch (Exception err) { } catch (Exception err) {
err.printStackTrace(); err.printStackTrace();
throw new BuildException(err.getMessage()); throw new BuildException(err.getMessage());
} }
} }



/** /**
* Ensure we have a consistent and legal set of attributes, and set * Ensure we have a consistent and legal set of attributes, and set
* any internal flags necessary based on different combinations * any internal flags necessary based on different combinations
* of attributes. * of attributes.
* @throws BuildException on error.
*/ */
protected void validateAttributes() throws BuildException { protected void validateAttributes() throws BuildException {
if (srcDir == null && filesets.size() == 0) { if (srcDir == null && filesets.size() == 0) {
throw new BuildException("Specify at least one source "
+ "- a srcDir or a fileset.");
throw new BuildException("Specify at least one source"
+ "--a srcDir or a fileset.");
} }

if (srcDir == null && destDir == null) { if (srcDir == null && destDir == null) {
throw new BuildException("Specify the destDir, or the srcDir."); throw new BuildException("Specify the destDir, or the srcDir.");
} }
} }

} }



Loading…
Cancel
Save