1 18 package org.apache.tools.ant.taskdefs.optional.image; 19 20 import com.sun.media.jai.codec.FileSeekableStream; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.DirectoryScanner; 23 import org.apache.tools.ant.taskdefs.MatchingTask; 24 import org.apache.tools.ant.types.FileSet; 25 import org.apache.tools.ant.types.optional.image.Draw; 26 import org.apache.tools.ant.types.optional.image.ImageOperation; 27 import org.apache.tools.ant.types.optional.image.Rotate; 28 import org.apache.tools.ant.types.optional.image.Scale; 29 import org.apache.tools.ant.types.optional.image.TransformOperation; 30 31 import javax.media.jai.JAI; 32 import javax.media.jai.PlanarImage; 33 import java.io.File ; 34 import java.io.FileOutputStream ; 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.Vector ; 39 40 53 public class Image extends MatchingTask { 54 protected Vector instructions = new Vector (); 56 protected boolean overwrite = false; 57 protected Vector filesets = new Vector (); 58 protected File srcDir = null; 59 protected File destDir = null; 60 61 63 protected String str_encoding = "JPEG"; 65 protected boolean garbage_collect = false; 66 67 private boolean failonerror = true; 68 69 71 73 77 public void addFileset(FileSet set) { 78 filesets.addElement(set); 79 } 80 81 86 public void setFailOnError(boolean failonerror) { 87 this.failonerror = failonerror; 88 } 89 90 94 public void setSrcdir(File srcDir) { 95 this.srcDir = srcDir; 96 } 97 98 104 public void setEncoding(String encoding) { 105 str_encoding = encoding; 106 } 107 108 112 public void setOverwrite(boolean overwrite) { 113 this.overwrite = overwrite; 114 } 115 116 121 public void setGc(boolean gc) { 122 garbage_collect = gc; 123 } 124 125 129 public void setDestDir(File destDir) { 130 this.destDir = destDir; 131 } 132 133 137 public void addImageOperation(ImageOperation instr) { 138 instructions.add(instr); 139 } 140 141 146 public void addRotate(Rotate instr) { 147 instructions.add(instr); 148 } 149 150 155 public void addScale(Scale instr) { 156 instructions.add(instr); 157 } 158 159 166 public void addDraw(Draw instr) { 167 instructions.add(instr); 168 } 169 170 175 public void add(ImageOperation instr) { 176 addImageOperation(instr); 177 } 178 179 184 public void processFile(File file) { 185 try { 186 log("Processing File: " + file.getAbsolutePath()); 187 FileSeekableStream input = new FileSeekableStream(file); 188 PlanarImage image = JAI.create("stream", input); 189 for (int i = 0; i < instructions.size(); i++) { 190 Object instr = instructions.elementAt(i); 191 if (instr instanceof TransformOperation) { 192 image = ((TransformOperation) instr) 193 .executeTransformOperation(image); 194 } else { 195 log("Not a TransformOperation: " + instr); 196 } 197 } 198 input.close(); 199 200 if (str_encoding.toLowerCase().equals("jpg")) { 201 str_encoding = "JPEG"; 202 } else if (str_encoding.toLowerCase().equals("tif")) { 203 str_encoding = "TIFF"; 204 } 205 if (destDir == null) { 206 destDir = srcDir; 207 } 208 File newFile = new File (destDir, file.getName()); 209 210 if ((overwrite && newFile.exists()) && (!newFile.equals(file))) { 211 newFile.delete(); 212 } 213 FileOutputStream stream = new FileOutputStream (newFile); 214 215 JAI.create("encode", image, stream, str_encoding.toUpperCase(), 216 null); 217 stream.flush(); 218 stream.close(); 219 } catch (IOException err) { 220 if (!failonerror) { 221 log("Error processing file: " + err); 222 } else { 223 throw new BuildException(err); 224 } 225 } catch (java.lang.RuntimeException rerr) { 226 if (!failonerror) { 227 log("Error processing file: " + rerr); 228 } else { 229 throw new BuildException(rerr); 230 } 231 } 232 } 233 234 238 public void execute() throws BuildException { 239 240 validateAttributes(); 241 242 try { 243 DirectoryScanner ds = null; 244 String [] files = null; 245 ArrayList filesList = new ArrayList (); 246 247 if (srcDir != null) { 249 ds = super.getDirectoryScanner(srcDir); 250 251 files = ds.getIncludedFiles(); 252 for (int i = 0; i < files.length; i++) { 253 filesList.add(new File (srcDir, files[i])); 254 } 255 } 256 for (int i = 0; i < filesets.size(); i++) { 258 FileSet fs = (FileSet) filesets.elementAt(i); 259 ds = fs.getDirectoryScanner(getProject()); 260 files = ds.getIncludedFiles(); 261 File fromDir = fs.getDir(getProject()); 262 for (int j = 0; j < files.length; j++) { 263 filesList.add(new File (fromDir, files[j])); 264 } 265 } 266 if (!overwrite) { 267 ArrayList filesToRemove = new ArrayList (); 269 for (Iterator i = filesList.iterator(); i.hasNext();) { 270 File f = (File ) i.next(); 271 File newFile = new File (destDir, f.getName()); 272 if (newFile.exists()) { 273 filesToRemove.add(f); 274 } 275 } 276 filesList.removeAll(filesToRemove); 277 } 278 for (Iterator i = filesList.iterator(); i.hasNext();) { 280 File file = (File ) i.next(); 281 282 processFile(file); 283 if (garbage_collect) { 284 System.gc(); 285 } 286 } 287 } catch (Exception err) { 288 err.printStackTrace(); 289 throw new BuildException(err.getMessage()); 290 } 291 } 292 293 299 protected void validateAttributes() throws BuildException { 300 if (srcDir == null && filesets.size() == 0) { 301 throw new BuildException("Specify at least one source" 302 + "--a srcDir or a fileset."); 303 } 304 if (srcDir == null && destDir == null) { 305 throw new BuildException("Specify the destDir, or the srcDir."); 306 } 307 } 308 } 309 310 | Popular Tags |