001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util;
020:
021: import java.io.File;
022: import java.util.Vector;
023: import org.apache.tools.ant.Task;
024: import org.apache.tools.ant.types.Resource;
025: import org.apache.tools.ant.types.ResourceFactory;
026: import org.apache.tools.ant.types.resources.FileResource;
027:
028: /**
029: * Utility class that collects the functionality of the various
030: * scanDir methods that have been scattered in several tasks before.
031: *
032: * <p>The only method returns an array of source files. The array is a
033: * subset of the files given as a parameter and holds only those that
034: * are newer than their corresponding target files.</p>
035: *
036: */
037: public class SourceFileScanner implements ResourceFactory {
038:
039: // CheckStyle:VisibilityModifier OFF - bc
040: protected Task task;
041: // CheckStyle:VisibilityModifier ON
042:
043: private static final FileUtils FILE_UTILS = FileUtils
044: .getFileUtils();
045: private File destDir; // base directory of the fileset
046:
047: /**
048: * Construct a new SourceFileScanner.
049: * @param task The task we should log messages through.
050: */
051: public SourceFileScanner(Task task) {
052: this .task = task;
053: }
054:
055: /**
056: * Restrict the given set of files to those that are newer than
057: * their corresponding target files.
058: *
059: * @param files the original set of files.
060: * @param srcDir all files are relative to this directory.
061: * @param destDir target files live here. if null file names
062: * returned by the mapper are assumed to be absolute.
063: * @param mapper knows how to construct a target file names from
064: * source file names.
065: * @return an array of filenames.
066: */
067: public String[] restrict(String[] files, File srcDir, File destDir,
068: FileNameMapper mapper) {
069: return restrict(files, srcDir, destDir, mapper, FILE_UTILS
070: .getFileTimestampGranularity());
071: }
072:
073: /**
074: * Restrict the given set of files to those that are newer than
075: * their corresponding target files.
076: *
077: * @param files the original set of files.
078: * @param srcDir all files are relative to this directory.
079: * @param destDir target files live here. If null file names
080: * returned by the mapper are assumed to be absolute.
081: * @param mapper knows how to construct a target file names from
082: * source file names.
083: * @param granularity The number of milliseconds leeway to give
084: * before deciding a target is out of date.
085: * @return an array of filenames.
086: *
087: * @since Ant 1.6.2
088: */
089: public String[] restrict(String[] files, File srcDir, File destDir,
090: FileNameMapper mapper, long granularity) {
091: // record destdir for later use in getResource
092: this .destDir = destDir;
093: Vector v = new Vector();
094: for (int i = 0; i < files.length; i++) {
095: File src = FILE_UTILS.resolveFile(srcDir, files[i]);
096: v.addElement(new Resource(files[i], src.exists(), src
097: .lastModified(), src.isDirectory()));
098: }
099: Resource[] sourceresources = new Resource[v.size()];
100: v.copyInto(sourceresources);
101:
102: // build the list of sources which are out of date with
103: // respect to the target
104: Resource[] outofdate = ResourceUtils.selectOutOfDateSources(
105: task, sourceresources, mapper, this , granularity);
106: String[] result = new String[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[] restrictAsFiles(String[] files, File srcDir,
126: File destDir, 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[] restrictAsFiles(String[] files, File srcDir,
148: File destDir, FileNameMapper mapper, long granularity) {
149: String[] res = restrict(files, srcDir, destDir, mapper,
150: granularity);
151: File[] result = new File[res.length];
152: for (int i = 0; i < res.length; i++) {
153: result[i] = new File(srcDir, res[i]);
154: }
155: return result;
156: }
157:
158: /**
159: * Returns resource information for a file at destination.
160: * @param name relative path of file at destination.
161: * @return data concerning a file whose relative path to destDir is name.
162: *
163: * @since Ant 1.5.2
164: */
165: public Resource getResource(String name) {
166: return new FileResource(destDir, name);
167: }
168:
169: }
|