View | Details | Raw Unified | Return to bug 23243
Collapse All | Expand All

(-)src/main/org/apache/tools/ant/taskdefs/optional/image/Image.java (-81 / +134 lines)
Lines 56-61 Link Here
56
import com.sun.media.jai.codec.FileSeekableStream;
56
import com.sun.media.jai.codec.FileSeekableStream;
57
import org.apache.tools.ant.BuildException;
57
import org.apache.tools.ant.BuildException;
58
import org.apache.tools.ant.DirectoryScanner;
58
import org.apache.tools.ant.DirectoryScanner;
59
import org.apache.tools.ant.Project;
59
import org.apache.tools.ant.taskdefs.MatchingTask;
60
import org.apache.tools.ant.taskdefs.MatchingTask;
60
import org.apache.tools.ant.types.FileSet;
61
import org.apache.tools.ant.types.FileSet;
61
import org.apache.tools.ant.types.optional.image.Draw;
62
import org.apache.tools.ant.types.optional.image.Draw;
Lines 63-68 Link Here
63
import org.apache.tools.ant.types.optional.image.Rotate;
64
import org.apache.tools.ant.types.optional.image.Rotate;
64
import org.apache.tools.ant.types.optional.image.Scale;
65
import org.apache.tools.ant.types.optional.image.Scale;
65
import org.apache.tools.ant.types.optional.image.TransformOperation;
66
import org.apache.tools.ant.types.optional.image.TransformOperation;
67
import org.apache.tools.ant.types.Mapper;
68
import org.apache.tools.ant.util.FileNameMapper;
69
import org.apache.tools.ant.util.IdentityMapper;
66
70
67
import javax.media.jai.JAI;
71
import javax.media.jai.JAI;
68
import javax.media.jai.PlanarImage;
72
import javax.media.jai.PlanarImage;
Lines 101-106 Link Here
101
    protected File srcDir = null;
105
    protected File srcDir = null;
102
    protected File destDir = null;
106
    protected File destDir = null;
103
107
108
    protected Mapper mapperElement = null;
109
110
    /**
111
     * Defines the mapper to map source to destination files.
112
     * @return a mapper to be configured
113
     * @exception BuildException if more than one mapper is defined
114
     */
115
    public Mapper createMapper() throws BuildException {
116
        if (mapperElement != null) {
117
            throw new BuildException("Cannot define more than one mapper",
118
                                     getLocation());
119
        }
120
        mapperElement = new Mapper(getProject());
121
        return mapperElement;
122
    }
123
124
104
    /**
125
    /**
105
     * Adds a set of files to be deleted.
126
     * Adds a set of files to be deleted.
106
     */
127
     */
Lines 197-254 Link Here
197
     * specified.
218
     * specified.
198
     * @param file The file to be processed
219
     * @param file The file to be processed
199
     */
220
     */
200
    public void processFile(File file) {
221
    public int processDir(final File srcDir, final String srcNames[], final File dstDir, final FileNameMapper mapper){
201
        try {
202
            log("Processing File: " + file.getAbsolutePath());
203
            FileSeekableStream input = new FileSeekableStream(file);
204
            PlanarImage image = JAI.create("stream", input);
205
            for (int i = 0; i < instructions.size(); i++) {
206
                Object instr = instructions.elementAt(i);
207
                if (instr instanceof TransformOperation) {
208
                    image = ((TransformOperation) instr).executeTransformOperation(image);
209
                } else {
210
                    log("Not a TransformOperation: " + instr);
211
                }
212
            }
213
            input.close();
214
222
215
            if (str_encoding.toLowerCase().equals("jpg")) {
223
        int writeCount = 0;
216
                str_encoding = "JPEG";
217
            } else if (str_encoding.toLowerCase().equals("tif")) {
218
                str_encoding = "TIFF";
219
            }
220
224
221
            if (destDir == null) {
225
        for(int i=0; i<srcNames.length; ++i){
222
                destDir = srcDir;
223
            }
224
226
225
            File new_file = new File(destDir.getAbsolutePath() + File.separator + file.getName());
227
            final String srcName = srcNames[i];
228
            final File srcFile = new File(srcDir, srcName).getAbsoluteFile();
226
229
227
            if ((overwrite && new_file.exists()) && (!new_file.equals(file))) {
230
            final String[] dstNames = mapper.mapFileName(srcName);
228
                new_file.delete();
231
            for(int j=0; j<dstNames.length; ++j){
229
            }
230
232
231
            FileOutputStream stream = new FileOutputStream(new_file);
233
                final String dstName = dstNames[j];
234
                final File dstFile = new File(dstDir, dstName).getAbsoluteFile();
232
235
233
            JAI.create("encode", image, stream, str_encoding.toUpperCase(), null);
236
                if(dstFile.exists()){
234
            stream.flush();
235
            stream.close();
236
237
238
                    // avoid overwriting unless necessary
239
                    if(!overwrite && srcFile.lastModified() <= dstFile.lastModified()){
237
240
238
        } catch (IOException err) {
241
                        log(srcFile + " omitted as " + dstFile
239
            if (!failonerror) {
242
                            + " is up to date.", Project.MSG_VERBOSE);
240
                log("Error processing file:  " + err);
243
241
            } else {
244
                        // don't overwrite the file
242
                throw new BuildException(err);
245
                        continue;
243
            }
246
					}
244
        } catch (java.lang.RuntimeException rerr) {
247
245
            if (!failonerror) {
248
                    // avoid extra work while overwriting
246
                log("Error processing file:  " + rerr);
249
                    if(!srcFile.equals(dstFile)){
247
            } else {
250
                        dstFile.delete();
248
                throw new BuildException(rerr);
251
                    }
252
                }
253
254
                // process the file
255
                try {
256
                    log("Writing File: " + dstName, Project.MSG_INFO);
257
                    FileSeekableStream input = new FileSeekableStream(srcFile);
258
                    PlanarImage image = JAI.create("stream", input);
259
                    for (int k = 0; k < instructions.size(); k++) {
260
                        Object instr = instructions.elementAt(k);
261
                        if (instr instanceof TransformOperation) {
262
                            image = ((TransformOperation) instr).executeTransformOperation(image);
263
                        } else {
264
                            log("Not a TransformOperation: " + instr);
265
                        }
266
                    }
267
                    input.close();
268
269
                    File dstParent = dstFile.getParentFile();
270
                    if(!dstParent.exists()){
271
                        dstParent.mkdirs();
272
                    }
273
                    FileOutputStream stream = new FileOutputStream(dstFile);
274
275
                    JAI.create("encode", image, stream, str_encoding.toUpperCase(), null);
276
                    stream.flush();
277
                    stream.close();
278
279
                    ++writeCount;
280
281
                } catch (IOException err) {
282
                    if(!srcFile.equals(dstFile)){
283
                        dstFile.delete();
284
                    }
285
                    if (!failonerror) {
286
                        log("Error processing file:  " + err);
287
                    } else {
288
                        throw new BuildException(err);
289
                    }
290
                } catch (java.lang.RuntimeException rerr) {
291
                    if(!srcFile.equals(dstFile)){
292
                        dstFile.delete();
293
                    }
294
                    if (!failonerror) {
295
                        log("Error processing file:  " + rerr);
296
                    } else {
297
                        throw new BuildException(rerr);
298
                    }
299
                }
249
            }
300
            }
250
        }
301
        }
251
302
303
        // run the garbage collector if wanted
304
        if (garbage_collect) {
305
            System.gc();
306
        }
307
308
        return writeCount;
252
    }
309
    }
253
310
254
    /**
311
    /**
Lines 259-315 Link Here
259
        validateAttributes();
316
        validateAttributes();
260
317
261
        try {
318
        try {
262
            DirectoryScanner ds = null;
263
            String[] files = null;
264
            ArrayList filesList = new ArrayList();
265
319
320
            // ensure we have a destination
321
            if (destDir == null) {
322
                destDir = srcDir;
323
            }
324
325
            //log("Writing images to " + destDir);
326
327
            int writeCount = 0;
328
329
            // build mapper
330
            final FileNameMapper mapper;
331
            if(mapperElement==null){
332
                mapper = new IdentityMapper();
333
            }
334
            else{
335
                mapper = mapperElement.getImplementation();
336
            }
266
337
267
            // deal with specified srcDir
338
            // deal with specified srcDir
268
            if (srcDir != null) {
339
            if (srcDir != null) {
269
                ds = super.getDirectoryScanner(srcDir);
340
                final DirectoryScanner ds = super.getDirectoryScanner(srcDir);
270
341
271
                files = ds.getIncludedFiles();
342
                final String[] included = ds.getIncludedFiles();
272
                for (int i = 0; i < files.length; i++) {
343
                writeCount += processDir(srcDir,included,destDir,mapper);
273
                    filesList.add(new File(srcDir.getAbsolutePath() + File.separator + files[i]));
274
                }
275
            }
344
            }
345
276
            // deal with the filesets
346
            // deal with the filesets
277
            for (int i = 0; i < filesets.size(); i++) {
347
            for (int i = 0; i < filesets.size(); i++) {
278
                FileSet fs = (FileSet) filesets.elementAt(i);
348
                final FileSet fs = (FileSet) filesets.elementAt(i);
279
                ds = fs.getDirectoryScanner(getProject());
349
                final DirectoryScanner ds = fs.getDirectoryScanner(getProject());
280
                files = ds.getIncludedFiles();
350
                final File fsDir = fs.getDir(getProject());
281
                File fromDir = fs.getDir(getProject());
282
                for (int j = 0; j < files.length; j++) {
283
                    filesList.add(new File(fromDir.getAbsolutePath() + File.separator + files[j]));
284
                }
285
            }
286
351
287
            if (!overwrite) {
352
                final String included[] = ds.getIncludedFiles();
288
                // remove any files that shouldn't be overwritten.
353
				writeCount += processDir(fsDir,included,destDir,mapper);
289
                ArrayList filesToRemove = new ArrayList();
290
                for (Iterator i = filesList.iterator(); i.hasNext();) {
291
                    File f = (File) i.next();
292
                    File new_file = new File(destDir.getAbsolutePath()
293
                        + File.separator + f.getName());
294
                    if (new_file.exists()) {
295
                        filesToRemove.add(f);
296
                    }
297
                }
298
                filesList.removeAll(filesToRemove);
299
            }
354
            }
300
355
301
356
            if(writeCount>0){
302
            // iterator through all the files and process them.
357
                log("Processed " + writeCount + (writeCount==1?" image.":" images."));
303
            for (Iterator i = filesList.iterator(); i.hasNext();) {
304
                File file = (File) i.next();
305
306
                processFile(file);
307
                if (garbage_collect) {
308
                    System.gc();
309
                }
310
            }
358
            }
311
359
312
313
        } catch (Exception err) {
360
        } catch (Exception err) {
314
            err.printStackTrace();
361
            err.printStackTrace();
315
            throw new BuildException(err.getMessage());
362
            throw new BuildException(err.getMessage());
Lines 332-339 Link Here
332
            throw new BuildException("Specify the destDir, or the srcDir.");
379
            throw new BuildException("Specify the destDir, or the srcDir.");
333
        }
380
        }
334
381
382
        // ensure encoding is sensible
335
383
384
        str_encoding = str_encoding.toUpperCase();
385
        if (str_encoding.equals("JPG")) {
386
            str_encoding = "JPEG";
387
        } else if (str_encoding.equals("TIF")) {
388
            str_encoding = "TIFF";
389
        }
336
    }
390
    }
337
391
338
}
392
}
339
(-)src/testcases/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java (-7 / +6 lines)
Lines 92-102 Link Here
92
    }
92
    }
93
93
94
    public void testEchoToLog() {
94
    public void testEchoToLog() {
95
        expectLogContaining("testEchoToLog", "Processing File");
95
        expectLogContaining("testEchoToLog", "Writing File:");
96
    }
96
    }
97
97
98
    public void testSimpleScale(){
98
    public void testSimpleScale(){
99
      expectLogContaining("testSimpleScale", "Processing File");
99
      expectLogContaining("testSimpleScale", "Writing File:");
100
      File f = createRelativeFile( "/dest/" + LARGEIMAGE  );
100
      File f = createRelativeFile( "/dest/" + LARGEIMAGE  );
101
          assertTrue(
101
          assertTrue(
102
              "Did not create "+f.getAbsolutePath(),
102
              "Did not create "+f.getAbsolutePath(),
Lines 105-111 Link Here
105
    }
105
    }
106
106
107
    public void testOverwriteTrue() {
107
    public void testOverwriteTrue() {
108
      expectLogContaining("testSimpleScale", "Processing File");
108
      expectLogContaining("testSimpleScale", "Writing File:");
109
      File f = createRelativeFile( "/dest/" + LARGEIMAGE  );
109
      File f = createRelativeFile( "/dest/" + LARGEIMAGE  );
110
      long lastModified = f.lastModified();
110
      long lastModified = f.lastModified();
111
       if (Os.isFamily("dos")) {
111
       if (Os.isFamily("dos")) {
Lines 114-130 Link Here
114
           }
114
           }
115
           catch (InterruptedException e) {}
115
           catch (InterruptedException e) {}
116
       }
116
       }
117
      expectLogContaining("testOverwriteTrue", "Processing File");
117
      expectLogContaining("testOverwriteTrue", "Writing File:");
118
      f = createRelativeFile( "/dest/" + LARGEIMAGE  );
118
      f = createRelativeFile( "/dest/" + LARGEIMAGE  );
119
      long overwrittenLastModified = f.lastModified();
119
      long overwrittenLastModified = f.lastModified();
120
      assertTrue("File was not overwritten.",lastModified < overwrittenLastModified);
120
      assertTrue("File was not overwritten.",lastModified < overwrittenLastModified);
121
    }
121
    }
122
122
123
    public void testOverwriteFalse() {
123
    public void testOverwriteFalse() {
124
      expectLogContaining("testSimpleScale", "Processing File");
124
      expectLogContaining("testSimpleScale", "Writing File:");
125
      File f = createRelativeFile( "/dest/" + LARGEIMAGE  );
125
      File f = createRelativeFile( "/dest/" + LARGEIMAGE  );
126
      long lastModified = f.lastModified();
126
      long lastModified = f.lastModified();
127
      expectLogContaining("testOverwriteFalse", "Processing File");
127
      expectLogContaining("testOverwriteFalse", "Writing File:");
128
      f = createRelativeFile( "/dest/" + LARGEIMAGE  );
128
      f = createRelativeFile( "/dest/" + LARGEIMAGE  );
129
      long overwrittenLastModified = f.lastModified();
129
      long overwrittenLastModified = f.lastModified();
130
      assertTrue("File was overwritten.",lastModified == overwrittenLastModified);
130
      assertTrue("File was overwritten.",lastModified == overwrittenLastModified);
Lines 139-145 Link Here
139
        assertTrue("Run time exception should say 'Unable to process image stream'. :" + re.toString(),re.toString().indexOf("Unable to process image stream") > -1);
139
        assertTrue("Run time exception should say 'Unable to process image stream'. :" + re.toString(),re.toString().indexOf("Unable to process image stream") > -1);
140
      }
140
      }
141
    }
141
    }
142
143
142
144
143
145
  protected File createRelativeFile( String filename ) {
144
  protected File createRelativeFile( String filename ) {

Return to bug 23243