summaryrefslogtreecommitdiffstats
path: root/src/java/net/sf/antcontrib/logic/OutOfDate.java
blob: 014a15e43fe9cf8001f422e0b34c515f5f92604a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
 * Copyright (c) 2001-2004 Ant-Contrib project.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.antcontrib.logic;

import java.io.File;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Parallel;
import org.apache.tools.ant.taskdefs.Sequential;
import org.apache.tools.ant.taskdefs.condition.Condition;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;

import org.apache.tools.ant.types.EnumeratedAttribute;

/**
* Task to help in calling tasks if generated files are older
* than source files.
* Sets a given property or runs an internal task.
*
* Based on
*  org.apache.org.apache.tools.ant.taskdefs.UpToDate
*
* @author peter reilly
*/

public class OutOfDate extends Task implements Condition {

    /**
     * Enumerated type for collection attribute
     *
     * @see EnumeratedAttribute
     */
    public static class CollectionEnum extends EnumeratedAttribute {
        /** Constants for the enumerations */
        public static final int
            SOURCES = 0, TARGETS = 1, ALLSOURCES = 2, ALLTARGETS = 3;

        /**
         * get the values
         * @return an array of the allowed values for this attribute.
         */
        public String[] getValues() {
            return new String[] {"sources", "targets", "allsources", "alltargets"};
        }
    }

    // attributes and nested elements
    private Task doTask = null;
    private String property;
    private String value                = "true";
    private boolean force               = false;
    private int    verbosity            = Project.MSG_VERBOSE;
    private Vector mappers              = new Vector();
    private Path targetpaths            = null;
    private Path sourcepaths            = null;
    private String outputSources        = null;
    private String outputSourcesPath    = null;
    private String outputTargets        = null;
    private String outputTargetsPath    = null;
    private String allTargets           = null;
    private String allTargetsPath       = null;
    private String separator            = " ";
    private DeleteTargets deleteTargets = null;
    private int    collection     = CollectionEnum.SOURCES;

    // variables
    private Hashtable targetSet = new Hashtable();
    private Hashtable sourceSet = new Hashtable();
    private Hashtable allTargetSet = new Hashtable();
    private Hashtable allSourceSet = new Hashtable();

    /**
     * Set the collection attribute, controls what is
     * returned by the iterator method.
     * <dl>
     * <li>"sources" the sources that are newer than the corresponding targets.</li>
     * <li>"targets" the targets that are older or not present than the corresponding
     *               sources.</li>
     * <li>"allsources" all the sources</li>
     * <li>"alltargets" all the targets</li>
     * </dl>
     * @param collection "sources" the changes
     */
    public void setCollection(CollectionEnum collection) {
        this.collection = collection.getIndex();
    }

    /**
     * Defines the FileNameMapper to use (nested mapper element).
     * @return Mappper to be configured
     */
    public Mapper createMapper() {
        MyMapper mapper = new MyMapper(getProject());
        mappers.addElement(mapper);
        return mapper;
    }

    /**
     * The property to set if any of the target files are outofdate with
     * regard to any of the source files.
     *
     * @param property the name of the property to set if Target is outofdate.
     */
    public void setProperty(String property) {
        this.property = property;
    }

    /**
     * The separator to use to separate the files
     * @param separator separator used in outout properties
     */

    public void setSeparator(String separator) {
        this.separator = separator;
    }

    /**
     * The value to set the named property to the target files
     * are outofdate
     *
     * @param value the value to set the property
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * whether to allways be outofdate
     * @param force true means that outofdate is always set, default
     *              false
     */
    public void setForce(boolean force) {
        this.force = force;
    }

    /**
     * whether to have verbose output
     * @param verbose true means that outofdate outputs debug info
     */
    public void setVerbose(boolean verbose) {
        if (verbose) {
            this.verbosity = Project.MSG_INFO;
        } else {
            this.verbosity = Project.MSG_VERBOSE;
        }
    }

    /**
     * Add to the target files
     *
     * @return a path to be configured
     */
    public Path createTargetfiles() {
        if (targetpaths == null) {
            targetpaths = new Path(getProject());
        }
        return targetpaths;
    }

    /**
     * Add to the source files
     *
     * @return a path to be configured
     */
    public Path createSourcefiles() {
        if (sourcepaths == null) {
            sourcepaths = new Path(getProject());
        }
        return sourcepaths;
    }

    /**
     * A property to contain the output source files
     *
     * @param outputSources the name of the property
     */
    public void setOutputSources(String outputSources) {
        this.outputSources = outputSources;
    }

    /**
     * A property to contain the output target files
     *
     * @param outputTargets the name of the property
     */
    public void setOutputTargets(String outputTargets) {
        this.outputTargets = outputTargets;
    }

    /**
     * A reference to contain the path of target files that
     * are outofdate
     *
     * @param outputTargetsPath the name of the reference
     */
    public void setOutputTargetsPath(String outputTargetsPath) {
        this.outputTargetsPath = outputTargetsPath;
    }

    /**
     * A refernce to contain the path of all the targets
     *
     * @param allTargetsPath the name of the reference
     */
    public void setAllTargetsPath(String allTargetsPath) {
        this.allTargetsPath = allTargetsPath;
    }

    /**
     * A property to contain all the target filenames
     *
     * @param allTargets the name of the property
     */
    public void setAllTargets(String allTargets) {
        this.allTargets = allTargets;
    }

    /**
     * A reference to the path containing all the sources files.
     *
     * @param outputSourcesPath the name of the reference
     */
    public void setOutputSourcesPath(String outputSourcesPath) {
        this.outputSourcesPath = outputSourcesPath;
    }

    /**
     * optional nested delete element
     * @return an element to be configured
     */
    public DeleteTargets createDeleteTargets() {
        deleteTargets = new DeleteTargets();
        return deleteTargets;
    }

    /**
     * Embedded do parallel
     * @param doTask the parallel to embed
     */
    public void addParallel(Parallel doTask) {
        if (this.doTask != null) {
            throw new BuildException(
                "You must not nest more that one <parallel> or <sequential>"
                + " into <outofdate>");
        }
        this.doTask = doTask;
    }

    /**
     * Embedded do sequential.
     * @param doTask the sequential to embed
     */
    public void addSequential(Sequential doTask) {
        if (this.doTask != null) {
            throw new BuildException(
                "You must not nest more that one <parallel> or <sequential>"
                + " into <outofdate>");
        }
        this.doTask = doTask;
    }

    /**
     * Evaluate (all) target and source file(s) to
     * see if the target(s) is/are outoutdate.
     * @return true if any of the targets are outofdate
     */
    public boolean eval() {
        boolean ret = false;
        FileUtils fileUtils = FileUtils.newFileUtils();
        if (sourcepaths == null) {
            throw new BuildException(
                "You must specify a <sourcefiles> element.");
        }

        if (targetpaths == null && mappers.size() == 0) {
            throw new BuildException(
                "You must specify a <targetfiles> or <mapper> element.");
        }

        // Source Paths
        String[] spaths = sourcepaths.list();

        for (int i = 0; i < spaths.length; i++) {
            File sourceFile = new File(spaths[i]);
            if (!sourceFile.exists()) {
                throw new BuildException(sourceFile.getAbsolutePath()
                                         + " not found.");
            }
        }

        // Target Paths

        if (targetpaths != null) {
            String[] paths = targetpaths.list();
            for (int i = 0; i < paths.length; ++i) {
                if (targetNeedsGen(paths[i], spaths)) {
                    ret = true;
                }
            }
        }

        // Mapper Paths
        for (Enumeration e = mappers.elements(); e.hasMoreElements();) {
            MyMapper mapper = (MyMapper) e.nextElement();

            File   relativeDir = mapper.getDir();
            File   baseDir = new File(getProject().getProperty("basedir"));
            if (relativeDir == null) {
                relativeDir = baseDir;
            }
            String[] rpaths = new String[spaths.length];
            for (int i = 0; i < spaths.length; ++i) {
                rpaths[i] = fileUtils.removeLeadingPath(relativeDir, new File(spaths[i]));
            }

            FileNameMapper fileNameMapper = mapper.getImplementation();
            for (int i = 0; i < spaths.length; ++i) {
                String[] mapped = fileNameMapper.mapFileName(rpaths[i]);
                if (mapped != null) {
                    for (int j = 0; j < mapped.length; ++j) {
                        if (outOfDate(new File(spaths[i]),
                                      fileUtils.resolveFile(
                                          baseDir, mapped[j]))) {
                            ret = true;
                        }
                    }
                }
            }
        }

        if (allTargets != null) {
            this.getProject().setNewProperty(
                allTargets, setToString(allTargetSet));
        }

        if (allTargetsPath != null) {
            this.getProject().addReference(
                allTargetsPath, setToPath(allTargetSet));
        }

        if (outputSources != null) {
            this.getProject().setNewProperty(
                outputSources, setToString(sourceSet));
        }

        if (outputTargets != null) {
            this.getProject().setNewProperty(
                outputTargets, setToString(targetSet));
        }

        if (outputSourcesPath != null) {
            this.getProject().addReference(
                outputSourcesPath, setToPath(sourceSet));
        }

        if (outputTargetsPath != null) {
            this.getProject().addReference(
                outputTargetsPath, setToPath(targetSet));
        }

        if (force) {
            ret = true;
        }

        if (ret && deleteTargets != null) {
            deleteTargets.execute();
        }

        if (ret) {
            if (property != null) {
                this.getProject().setNewProperty(property, value);
            }
        }

        return ret;
    }

    private boolean targetNeedsGen(String target, String[] spaths) {
        boolean ret = false;
        File targetFile = new File(target);
        for (int i = 0; i < spaths.length; i++) {
            if (outOfDate(new File(spaths[i]), targetFile)) {
                ret = true;
            }
        }
        // Special case : there are no source files, make sure the
        //                targets exist
        if (spaths.length == 0) {
            if (outOfDate(null, targetFile)) {
                ret = true;
            }
        }
        return ret;
    }

    /**
     * Call evalute and return an iterator over the result
     * @return an iterator over the result
     */
    public Iterator iterator() {
        // Perhaps should check the result and return
        // an empty set if it returns false
        eval();

        switch (collection) {
            case CollectionEnum.SOURCES:
                return sourceSet.values().iterator();
            case CollectionEnum.TARGETS:
                return targetSet.values().iterator();
            case CollectionEnum.ALLSOURCES:
                return allSourceSet.values().iterator();
            case CollectionEnum.ALLTARGETS:
                return allTargetSet.values().iterator();
            default:
                return sourceSet.values().iterator();
        }
    }

    /**
     * Sets property to true and/or executes embedded do
     * if any of the target file(s) do not have a more recent timestamp
     * than (each of) the source file(s).
     */
    public void execute() {
        if (!eval()) {
            return;
        }

        if (doTask != null) {
            doTask.perform();
        }

    }


    private boolean outOfDate(File sourceFile, File targetFile) {
        boolean ret  = false;
        if (sourceFile != null) {
            allSourceSet.put(sourceFile, sourceFile);
        }
        allTargetSet.put(targetFile, targetFile);
        if (!targetFile.exists()) {
            ret = true;
        }
        if ((!ret)  && (sourceFile != null)) {
            ret = sourceFile.lastModified() > targetFile.lastModified();
        }
        if (ret) {
            if ((sourceFile != null && sourceSet.get(sourceFile) == null)
                || targetSet.get(targetFile) == null) {
                log("SourceFile " + sourceFile + " outofdate "
                    + "with regard to " + targetFile, verbosity);
            }
            if (sourceFile != null) {
                sourceSet.put(sourceFile, sourceFile);
            }
            targetSet.put(targetFile, targetFile);
        }
        return ret;
    }

    private String setToString(Hashtable set) {
        StringBuffer b = new StringBuffer();
        for (Enumeration e = set.keys(); e.hasMoreElements();) {
            File v = (File) e.nextElement();
            if (b.length() != 0) {
                b.append(separator);
            }
            String s = v.getAbsolutePath();
            // DOTO: The following needs more work!
            // Handle paths contains sep
            if (s.indexOf(separator) != -1) {
                if (s.indexOf("\"") != -1) {
                    s = "'" + s + "'";
                } else {
                    s = "\"" + s + "\"";
                }
            }
            b.append(s);
        }
        return b.toString();
    }

    private Path setToPath(Hashtable set) {
        Path ret = new Path(getProject());
        for (Enumeration e = set.keys(); e.hasMoreElements();) {
            File v = (File) e.nextElement();
            Path.PathElement el = ret.createPathElement();
            el.setLocation(v);
        }
        return ret;
    }

    /**
     * nested delete targets
     */
    public class DeleteTargets {
        private boolean all         = false;
        private boolean quiet       = false;
        private boolean failOnError = false;

        private int     myLogging   = Project.MSG_INFO;

        /**
         * whether to delete all the targets
         * or just those that are newer than the
         * corresponding sources.
         * @param all true to delete all, default false
         */
        public void setAll(boolean all) {
            this.all = all;
        }

        /**
         * @param quiet if true suppress messages on deleting files
         */
        public void setQuiet(boolean quiet) {
            this.quiet = quiet;
            myLogging = quiet ? Project.MSG_VERBOSE : Project.MSG_INFO;
        }

        /**
         * @param failOnError if true halt if there is a failure to delete
         */
        public void setFailOnError(boolean failOnError) {
            this.failOnError = failOnError;
        }

        private void execute() {
            if (myLogging != Project.MSG_INFO) {
                myLogging = verbosity;
            }

            // Quiet overrides failOnError
            if (quiet) {
                failOnError = false;
            }

            Path toBeDeleted = null;
            if (all) {
                toBeDeleted = setToPath(allTargetSet);
            } else {
                toBeDeleted = setToPath(targetSet);
            }

            String[] names = toBeDeleted.list();
            for (int i = 0; i < names.length; ++i) {
                File file = new File(names[i]);
                if (!file.exists()) {
                    continue;
                }
                if (file.isDirectory()) {
                    removeDir(file);
                    continue;
                }
                log("Deleting " + file.getAbsolutePath(), myLogging);
                if (!file.delete()) {
                    String message =
                        "Unable to delete file " + file.getAbsolutePath();
                    if (failOnError) {
                        throw new BuildException(message);
                    } else {
                        log(message,  myLogging);
                    }
                }
            }
        }

        private static final int DELETE_RETRY_SLEEP_MILLIS = 10;
        /**
         * Attempt to fix possible race condition when deleting
         * files on WinXP. If the delete does not work,
         * wait a little and try again.
         */
        private boolean delete(File f) {
            if (!f.delete()) {
                try {
                    Thread.sleep(DELETE_RETRY_SLEEP_MILLIS);
                    return f.delete();
                } catch (InterruptedException ex) {
                    return f.delete();
                }
            }
            return true;
        }

        private void removeDir(File d) {
            String[] list = d.list();
            if (list == null) {
                list = new String[0];
            }
            for (int i = 0; i < list.length; i++) {
                String s = list[i];
                File f = new File(d, s);
                if (f.isDirectory()) {
                    removeDir(f);
                } else {
                    log("Deleting " + f.getAbsolutePath(), myLogging);
                    if (!f.delete()) {
                        String message = "Unable to delete file "
                            + f.getAbsolutePath();
                        if (failOnError) {
                            throw new BuildException(message);
                        } else {
                            log(message, myLogging);
                        }
                    }
                }
            }
            log("Deleting directory " + d.getAbsolutePath(), myLogging);
            if (!delete(d)) {
                String message = "Unable to delete directory "
                    + d.getAbsolutePath();
                if (failOnError) {
                    throw new BuildException(message);
                } else {
                    log(message, myLogging);
                }
            }
        }
    }

    /**
     *  Wrapper for mapper - includes dir
     */
    public static class MyMapper extends Mapper {
        private File dir = null;
        /**
         * Creates a new <code>MyMapper</code> instance.
         *
         * @param project the current project
         */
        public MyMapper(Project project) {
            super(project);
        }

        /**
         * @param dir the directory that the from files are relative to
         */
        public void setDir(File dir) {
            this.dir = dir;
        }

        /**
         * @return the directory that the from files are relative to
         */
        public File getDir() {
            return dir;
        }
    }
}