KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > SourceFileScanner


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util;
20
21 import java.io.File JavaDoc;
22 import java.util.Vector JavaDoc;
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.types.Resource;
25 import org.apache.tools.ant.types.ResourceFactory;
26 import org.apache.tools.ant.types.resources.FileResource;
27
28 /**
29  * Utility class that collects the functionality of the various
30  * scanDir methods that have been scattered in several tasks before.
31  *
32  * <p>The only method returns an array of source files. The array is a
33  * subset of the files given as a parameter and holds only those that
34  * are newer than their corresponding target files.</p>
35  *
36  */

37 public class SourceFileScanner implements ResourceFactory {
38
39     // CheckStyle:VisibilityModifier OFF - bc
40
protected Task task;
41     // CheckStyle:VisibilityModifier ON
42

43     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
44     private File JavaDoc destDir; // base directory of the fileset
45

46     /**
47      * Construct a new SourceFileScanner.
48      * @param task The task we should log messages through.
49      */

50     public SourceFileScanner(Task task) {
51         this.task = task;
52     }
53
54     /**
55      * Restrict the given set of files to those that are newer than
56      * their corresponding target files.
57      *
58      * @param files the original set of files.
59      * @param srcDir all files are relative to this directory.
60      * @param destDir target files live here. if null file names
61      * returned by the mapper are assumed to be absolute.
62      * @param mapper knows how to construct a target file names from
63      * source file names.
64      * @return an array of filenames.
65      */

66     public String JavaDoc[] restrict(String JavaDoc[] files, File JavaDoc srcDir, File JavaDoc destDir,
67                              FileNameMapper mapper) {
68         return restrict(files, srcDir, destDir, mapper,
69                         FILE_UTILS.getFileTimestampGranularity());
70     }
71
72     /**
73      * Restrict the given set of files to those that are newer than
74      * their corresponding target files.
75      *
76      * @param files the original set of files.
77      * @param srcDir all files are relative to this directory.
78      * @param destDir target files live here. If null file names
79      * returned by the mapper are assumed to be absolute.
80      * @param mapper knows how to construct a target file names from
81      * source file names.
82      * @param granularity The number of milliseconds leeway to give
83      * before deciding a target is out of date.
84      * @return an array of filenames.
85      *
86      * @since Ant 1.6.2
87      */

88     public String JavaDoc[] restrict(String JavaDoc[] files, File JavaDoc srcDir, File JavaDoc destDir,
89                              FileNameMapper mapper, long granularity) {
90         // record destdir for later use in getResource
91
this.destDir = destDir;
92         Vector JavaDoc v = new Vector JavaDoc();
93         for (int i = 0; i < files.length; i++) {
94             File JavaDoc src = FILE_UTILS.resolveFile(srcDir, files[i]);
95             v.addElement(new Resource(files[i], src.exists(),
96                                       src.lastModified(), src.isDirectory()));
97         }
98         Resource[] sourceresources = new Resource[v.size()];
99         v.copyInto(sourceresources);
100
101         // build the list of sources which are out of date with
102
// respect to the target
103
Resource[] outofdate =
104             ResourceUtils.selectOutOfDateSources(task, sourceresources,
105                                                  mapper, this, granularity);
106         String JavaDoc[] result = new String JavaDoc[outofdate.length];
107         for (int counter = 0; counter < outofdate.length; counter++) {
108             result[counter] = outofdate[counter].getName();
109         }
110         return result;
111     }
112
113     /**
114      * Convenience layer on top of restrict that returns the source
115      * files as File objects (containing absolute paths if srcDir is
116      * absolute).
117      * @param files the original set of files.
118      * @param srcDir all files are relative to this directory.
119      * @param destDir target files live here. If null file names
120      * returned by the mapper are assumed to be absolute.
121      * @param mapper knows how to construct a target file names from
122      * source file names.
123      * @return an array of files.
124      */

125     public File JavaDoc[] restrictAsFiles(String JavaDoc[] files, File JavaDoc srcDir, File JavaDoc destDir,
126                                   FileNameMapper mapper) {
127         return restrictAsFiles(files, srcDir, destDir, mapper,
128                                FILE_UTILS.getFileTimestampGranularity());
129     }
130
131     /**
132      * Convenience layer on top of restrict that returns the source
133      * files as File objects (containing absolute paths if srcDir is
134      * absolute).
135      *
136      * @param files the original set of files.
137      * @param srcDir all files are relative to this directory.
138      * @param destDir target files live here. If null file names
139      * returned by the mapper are assumed to be absolute.
140      * @param mapper knows how to construct a target file names from
141      * source file names.
142      * @param granularity The number of milliseconds leeway to give
143      * before deciding a target is out of date.
144      * @return an array of files.
145      * @since Ant 1.6.2
146      */

147     public File JavaDoc[] restrictAsFiles(String JavaDoc[] files, File JavaDoc srcDir, File JavaDoc destDir,
148                                   FileNameMapper mapper, long granularity) {
149         String JavaDoc[] res = restrict(files, srcDir, destDir, mapper, granularity);
150         File JavaDoc[] result = new File JavaDoc[res.length];
151         for (int i = 0; i < res.length; i++) {
152             result[i] = new File JavaDoc(srcDir, res[i]);
153         }
154         return result;
155     }
156
157     /**
158      * Returns resource information for a file at destination.
159      * @param name relative path of file at destination.
160      * @return data concerning a file whose relative path to destDir is name.
161      *
162      * @since Ant 1.5.2
163      */

164     public Resource getResource(String JavaDoc name) {
165         return new FileResource(destDir, name);
166     }
167
168 }
169
170
Popular Tags