1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.util;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.util.Vector;
|
---|
22 | import org.apache.tools.ant.Task;
|
---|
23 | import org.apache.tools.ant.types.ResourceFactory;
|
---|
24 | import org.apache.tools.ant.types.Resource;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Utility class that collects the functionality of the various
|
---|
28 | * scanDir methods that have been scattered in several tasks before.
|
---|
29 | *
|
---|
30 | * <p>The only method returns an array of source files. The array is a
|
---|
31 | * subset of the files given as a parameter and holds only those that
|
---|
32 | * are newer than their corresponding target files.</p>
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class SourceFileScanner implements ResourceFactory {
|
---|
36 |
|
---|
37 | protected Task task;
|
---|
38 |
|
---|
39 | private FileUtils fileUtils;
|
---|
40 | private File destDir; // base directory of the fileset
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @param task The task we should log messages through
|
---|
44 | */
|
---|
45 | public SourceFileScanner(Task task) {
|
---|
46 | this.task = task;
|
---|
47 | fileUtils = FileUtils.newFileUtils();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Restrict the given set of files to those that are newer than
|
---|
52 | * their corresponding target files.
|
---|
53 | *
|
---|
54 | * @param files the original set of files
|
---|
55 | * @param srcDir all files are relative to this directory
|
---|
56 | * @param destDir target files live here. if null file names
|
---|
57 | * returned by the mapper are assumed to be absolute.
|
---|
58 | * @param mapper knows how to construct a target file names from
|
---|
59 | * source file names.
|
---|
60 | */
|
---|
61 | public String[] restrict(String[] files, File srcDir, File destDir,
|
---|
62 | FileNameMapper mapper) {
|
---|
63 | return restrict(files, srcDir, destDir, mapper,
|
---|
64 | fileUtils.getFileTimestampGranularity());
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Restrict the given set of files to those that are newer than
|
---|
69 | * their corresponding target files.
|
---|
70 | *
|
---|
71 | * @param files the original set of files
|
---|
72 | * @param srcDir all files are relative to this directory
|
---|
73 | * @param destDir target files live here. if null file names
|
---|
74 | * returned by the mapper are assumed to be absolute.
|
---|
75 | * @param mapper knows how to construct a target file names from
|
---|
76 | * source file names.
|
---|
77 | * @param granularity The number of milliseconds leeway to give
|
---|
78 | * before deciding a target is out of date.
|
---|
79 | *
|
---|
80 | * @since Ant 1.6.2
|
---|
81 | */
|
---|
82 | public String[] restrict(String[] files, File srcDir, File destDir,
|
---|
83 | FileNameMapper mapper, long granularity) {
|
---|
84 | // record destdir for later use in getResource
|
---|
85 | this.destDir = destDir;
|
---|
86 | Vector v = new Vector();
|
---|
87 | for (int i = 0; i < files.length; i++) {
|
---|
88 | File src = fileUtils.resolveFile(srcDir, files[i]);
|
---|
89 | v.addElement(new Resource(files[i], src.exists(),
|
---|
90 | src.lastModified(), src.isDirectory()));
|
---|
91 | }
|
---|
92 | Resource[] sourceresources = new Resource[v.size()];
|
---|
93 | v.copyInto(sourceresources);
|
---|
94 |
|
---|
95 | // build the list of sources which are out of date with
|
---|
96 | // respect to the target
|
---|
97 | Resource[] outofdate =
|
---|
98 | ResourceUtils.selectOutOfDateSources(task, sourceresources,
|
---|
99 | mapper, this, granularity);
|
---|
100 | String[] result = new String[outofdate.length];
|
---|
101 | for (int counter = 0; counter < outofdate.length; counter++) {
|
---|
102 | result[counter] = outofdate[counter].getName();
|
---|
103 | }
|
---|
104 | return result;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Convinience layer on top of restrict that returns the source
|
---|
109 | * files as File objects (containing absolute paths if srcDir is
|
---|
110 | * absolute).
|
---|
111 | */
|
---|
112 | public File[] restrictAsFiles(String[] files, File srcDir, File destDir,
|
---|
113 | FileNameMapper mapper) {
|
---|
114 | return restrictAsFiles(files, srcDir, destDir, mapper,
|
---|
115 | fileUtils.getFileTimestampGranularity());
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Convinience layer on top of restrict that returns the source
|
---|
120 | * files as File objects (containing absolute paths if srcDir is
|
---|
121 | * absolute).
|
---|
122 | *
|
---|
123 | * @since Ant 1.6.2
|
---|
124 | */
|
---|
125 | public File[] restrictAsFiles(String[] files, File srcDir, File destDir,
|
---|
126 | FileNameMapper mapper, long granularity) {
|
---|
127 | String[] res = restrict(files, srcDir, destDir, mapper, granularity);
|
---|
128 | File[] result = new File[res.length];
|
---|
129 | for (int i = 0; i < res.length; i++) {
|
---|
130 | result[i] = new File(srcDir, res[i]);
|
---|
131 | }
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * returns resource information for a file at destination
|
---|
137 | * @param name relative path of file at destination
|
---|
138 | * @return data concerning a file whose relative path to destDir is name
|
---|
139 | *
|
---|
140 | * @since Ant 1.5.2
|
---|
141 | */
|
---|
142 | public Resource getResource(String name) {
|
---|
143 | File src = fileUtils.resolveFile(destDir, name);
|
---|
144 | return new Resource(name, src.exists(), src.lastModified(),
|
---|
145 | src.isDirectory());
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|