1 | /*
|
---|
2 | * Copyright 2003-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 | package org.apache.tools.ant.util;
|
---|
18 |
|
---|
19 | import org.apache.tools.ant.Project;
|
---|
20 | import org.apache.tools.ant.ProjectComponent;
|
---|
21 | import org.apache.tools.ant.types.Resource;
|
---|
22 | import org.apache.tools.ant.types.ResourceFactory;
|
---|
23 | import org.apache.tools.ant.types.selectors.SelectorUtils;
|
---|
24 |
|
---|
25 | import java.io.File;
|
---|
26 | import java.util.Vector;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * this class provides utility methods to process resources
|
---|
30 | *
|
---|
31 | * @since Ant 1.5.2
|
---|
32 | */
|
---|
33 | public class ResourceUtils {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * tells which source files should be reprocessed based on the
|
---|
37 | * last modification date of target files
|
---|
38 | * @param logTo where to send (more or less) interesting output
|
---|
39 | * @param source array of resources bearing relative path and last
|
---|
40 | * modification date
|
---|
41 | * @param mapper filename mapper indicating how to find the target
|
---|
42 | * files
|
---|
43 | * @param targets object able to map as a resource a relative path
|
---|
44 | * at <b>destination</b>
|
---|
45 | * @return array containing the source files which need to be
|
---|
46 | * copied or processed, because the targets are out of date or do
|
---|
47 | * not exist
|
---|
48 | */
|
---|
49 | public static Resource[] selectOutOfDateSources(ProjectComponent logTo,
|
---|
50 | Resource[] source,
|
---|
51 | FileNameMapper mapper,
|
---|
52 | ResourceFactory targets) {
|
---|
53 | return selectOutOfDateSources(logTo, source, mapper, targets,
|
---|
54 | FileUtils.newFileUtils()
|
---|
55 | .getFileTimestampGranularity());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * tells which source files should be reprocessed based on the
|
---|
60 | * last modification date of target files
|
---|
61 | * @param logTo where to send (more or less) interesting output
|
---|
62 | * @param source array of resources bearing relative path and last
|
---|
63 | * modification date
|
---|
64 | * @param mapper filename mapper indicating how to find the target
|
---|
65 | * files
|
---|
66 | * @param targets object able to map as a resource a relative path
|
---|
67 | * at <b>destination</b>
|
---|
68 | * @param granularity The number of milliseconds leeway to give
|
---|
69 | * before deciding a target is out of date.
|
---|
70 | * @return array containing the source files which need to be
|
---|
71 | * copied or processed, because the targets are out of date or do
|
---|
72 | * not exist
|
---|
73 | * @since Ant 1.6.2
|
---|
74 | */
|
---|
75 | public static Resource[] selectOutOfDateSources(ProjectComponent logTo,
|
---|
76 | Resource[] source,
|
---|
77 | FileNameMapper mapper,
|
---|
78 | ResourceFactory targets,
|
---|
79 | long granularity) {
|
---|
80 | long now = (new java.util.Date()).getTime() + granularity;
|
---|
81 |
|
---|
82 | Vector vresult = new Vector();
|
---|
83 | for (int counter = 0; counter < source.length; counter++) {
|
---|
84 | if (source[counter].getLastModified() > now) {
|
---|
85 | logTo.log("Warning: " + source[counter].getName()
|
---|
86 | + " modified in the future.",
|
---|
87 | Project.MSG_WARN);
|
---|
88 | }
|
---|
89 |
|
---|
90 | String[] targetnames =
|
---|
91 | mapper.mapFileName(source[counter].getName()
|
---|
92 | .replace('/', File.separatorChar));
|
---|
93 | if (targetnames != null) {
|
---|
94 | boolean added = false;
|
---|
95 | StringBuffer targetList = new StringBuffer();
|
---|
96 | for (int ctarget = 0; !added && ctarget < targetnames.length;
|
---|
97 | ctarget++) {
|
---|
98 | Resource atarget =
|
---|
99 | targets.getResource(targetnames[ctarget]
|
---|
100 | .replace(File.separatorChar, '/'));
|
---|
101 | // if the target does not exist, or exists and
|
---|
102 | // is older than the source, then we want to
|
---|
103 | // add the resource to what needs to be copied
|
---|
104 | if (!atarget.isExists()) {
|
---|
105 | logTo.log(source[counter].getName() + " added as "
|
---|
106 | + atarget.getName()
|
---|
107 | + " doesn\'t exist.", Project.MSG_VERBOSE);
|
---|
108 | vresult.addElement(source[counter]);
|
---|
109 | added = true;
|
---|
110 | } else if (!atarget.isDirectory()
|
---|
111 | && SelectorUtils.isOutOfDate(source[counter],
|
---|
112 | atarget,
|
---|
113 | (int) granularity)) {
|
---|
114 | logTo.log(source[counter].getName() + " added as "
|
---|
115 | + atarget.getName()
|
---|
116 | + " is outdated.", Project.MSG_VERBOSE);
|
---|
117 | vresult.addElement(source[counter]);
|
---|
118 | added = true;
|
---|
119 | } else {
|
---|
120 | if (targetList.length() > 0) {
|
---|
121 | targetList.append(", ");
|
---|
122 | }
|
---|
123 | targetList.append(atarget.getName());
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | if (!added) {
|
---|
128 | logTo.log(source[counter].getName()
|
---|
129 | + " omitted as " + targetList.toString()
|
---|
130 | + (targetnames.length == 1 ? " is" : " are ")
|
---|
131 | + " up to date.", Project.MSG_VERBOSE);
|
---|
132 | }
|
---|
133 | } else {
|
---|
134 | logTo.log(source[counter].getName()
|
---|
135 | + " skipped - don\'t know how to handle it",
|
---|
136 | Project.MSG_VERBOSE);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | Resource[] result = new Resource[vresult.size()];
|
---|
140 | vresult.copyInto(result);
|
---|
141 | return result;
|
---|
142 | }
|
---|
143 | }
|
---|