ResourceUtilspublic class ResourceUtils extends Object This class provides utility methods to process Resources. |
Fields Summary |
---|
private static final FileUtils | FILE_UTILSUtilities used for file operations | private static final org.apache.tools.ant.types.resources.selectors.ResourceSelector | NOT_EXISTS |
Methods Summary |
---|
private static int | binaryCompare(org.apache.tools.ant.types.Resource r1, org.apache.tools.ant.types.Resource r2)Binary compares the contents of two Resources.
simple but sub-optimal comparision algorithm. written for working
rather than fast. Better would be a block read into buffers followed
by long comparisions apart from the final 1-7 bytes.
InputStream in1 = null;
InputStream in2 = null;
try {
in1 = new BufferedInputStream(r1.getInputStream());
in2 = new BufferedInputStream(r2.getInputStream());
for (int b1 = in1.read(); b1 != -1; b1 = in1.read()) {
int b2 = in2.read();
if (b1 != b2) {
return b1 > b2 ? 1 : -1;
}
}
return in2.read() == -1 ? 0 : -1;
} finally {
FileUtils.close(in1);
FileUtils.close(in2);
}
| public static int | compareContent(org.apache.tools.ant.types.Resource r1, org.apache.tools.ant.types.Resource r2, boolean text)Compare the content of two Resources. A nonexistent Resource's
content is "less than" that of an existing Resource; a directory-type
Resource's content is "less than" that of a file-type Resource.
if (r1.equals(r2)) {
return 0;
}
boolean e1 = r1.isExists();
boolean e2 = r2.isExists();
if (!(e1 || e2)) {
return 0;
}
if (e1 != e2) {
return e1 ? 1 : -1;
}
boolean d1 = r1.isDirectory();
boolean d2 = r2.isDirectory();
if (d1 && d2) {
return 0;
}
if (d1 || d2) {
return d1 ? -1 : 1;
}
return text ? textCompare(r1, r2) : binaryCompare(r1, r2);
| public static boolean | contentEquals(org.apache.tools.ant.types.Resource r1, org.apache.tools.ant.types.Resource r2, boolean text)Compares the contents of two Resources.
if (r1.isExists() != r2.isExists()) {
return false;
}
if (!r1.isExists()) {
// two not existing files are equal
return true;
}
// should the following two be switched? If r1 and r2 refer to the same file,
// isn't their content equal regardless of whether that file is a directory?
if (r1.isDirectory() || r2.isDirectory()) {
// don't want to compare directory contents for now
return false;
}
if (r1.equals(r2)) {
return true;
}
if (!text && r1.getSize() != r2.getSize()) {
return false;
}
return compareContent(r1, r2, text) == 0;
| public static void | copyResource(org.apache.tools.ant.types.Resource source, org.apache.tools.ant.types.Resource dest)Convenience method to copy content from one Resource to another.
No filtering is performed.
copyResource(source, dest, null);
| public static void | copyResource(org.apache.tools.ant.types.Resource source, org.apache.tools.ant.types.Resource dest, org.apache.tools.ant.Project project)Convenience method to copy content from one Resource to another.
No filtering is performed.
copyResource(source, dest, null, null, false,
false, null, null, project);
| public static void | copyResource(org.apache.tools.ant.types.Resource source, org.apache.tools.ant.types.Resource dest, org.apache.tools.ant.types.FilterSetCollection filters, java.util.Vector filterChains, boolean overwrite, boolean preserveLastModified, java.lang.String inputEncoding, java.lang.String outputEncoding, org.apache.tools.ant.Project project)Convenience method to copy content from one Resource to another
specifying whether token filtering must be used, whether filter chains
must be used, whether newer destination files may be overwritten and
whether the last modified time of dest file should be made
equal to the last modified time of source .
if (!overwrite) {
long slm = source.getLastModified();
if (dest.isExists() && slm != 0
&& dest.getLastModified() > slm) {
return;
}
}
final boolean filterSetsAvailable = (filters != null
&& filters.hasFilters());
final boolean filterChainsAvailable = (filterChains != null
&& filterChains.size() > 0);
if (filterSetsAvailable) {
BufferedReader in = null;
BufferedWriter out = null;
try {
InputStreamReader isr = null;
if (inputEncoding == null) {
isr = new InputStreamReader(source.getInputStream());
} else {
isr = new InputStreamReader(source.getInputStream(),
inputEncoding);
}
in = new BufferedReader(isr);
OutputStreamWriter osw = null;
if (outputEncoding == null) {
osw = new OutputStreamWriter(dest.getOutputStream());
} else {
osw = new OutputStreamWriter(dest.getOutputStream(),
outputEncoding);
}
out = new BufferedWriter(osw);
if (filterChainsAvailable) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(FileUtils.BUF_SIZE);
crh.setPrimaryReader(in);
crh.setFilterChains(filterChains);
crh.setProject(project);
Reader rdr = crh.getAssembledReader();
in = new BufferedReader(rdr);
}
LineTokenizer lineTokenizer = new LineTokenizer();
lineTokenizer.setIncludeDelims(true);
String newline = null;
String line = lineTokenizer.getToken(in);
while (line != null) {
if (line.length() == 0) {
// this should not happen, because the lines are
// returned with the end of line delimiter
out.newLine();
} else {
newline = filters.replaceTokens(line);
out.write(newline);
}
line = lineTokenizer.getToken(in);
}
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
} else if (filterChainsAvailable
|| (inputEncoding != null
&& !inputEncoding.equals(outputEncoding))
|| (inputEncoding == null && outputEncoding != null)) {
BufferedReader in = null;
BufferedWriter out = null;
try {
InputStreamReader isr = null;
if (inputEncoding == null) {
isr = new InputStreamReader(source.getInputStream());
} else {
isr = new InputStreamReader(source.getInputStream(),
inputEncoding);
}
in = new BufferedReader(isr);
OutputStreamWriter osw = null;
if (outputEncoding == null) {
osw = new OutputStreamWriter(dest.getOutputStream());
} else {
osw = new OutputStreamWriter(dest.getOutputStream(),
outputEncoding);
}
out = new BufferedWriter(osw);
if (filterChainsAvailable) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(FileUtils.BUF_SIZE);
crh.setPrimaryReader(in);
crh.setFilterChains(filterChains);
crh.setProject(project);
Reader rdr = crh.getAssembledReader();
in = new BufferedReader(rdr);
}
char[] buffer = new char[FileUtils.BUF_SIZE];
while (true) {
int nRead = in.read(buffer, 0, buffer.length);
if (nRead == -1) {
break;
}
out.write(buffer, 0, nRead);
}
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
} else {
InputStream in = null;
OutputStream out = null;
try {
in = source.getInputStream();
out = dest.getOutputStream();
byte[] buffer = new byte[FileUtils.BUF_SIZE];
int count = 0;
do {
out.write(buffer, 0, count);
count = in.read(buffer, 0, buffer.length);
} while (count != -1);
} finally {
FileUtils.close(out);
FileUtils.close(in);
}
}
if (preserveLastModified && dest instanceof Touchable) {
setLastModified((Touchable) dest, source.getLastModified());
}
| private static void | logFuture(org.apache.tools.ant.ProjectComponent logTo, org.apache.tools.ant.types.ResourceCollection rc, long granularity)Log which Resources (if any) have been modified in the future.
long now = System.currentTimeMillis() + granularity;
Date sel = new Date();
sel.setMillis(now);
sel.setWhen(TimeComparison.AFTER);
Restrict future = new Restrict();
future.add(sel);
future.add(rc);
for (Iterator iter = future.iterator(); iter.hasNext();) {
logTo.log("Warning: " + ((Resource) iter.next()).getName()
+ " modified in the future.", Project.MSG_WARN);
}
| public static org.apache.tools.ant.types.Resource[] | selectOutOfDateSources(org.apache.tools.ant.ProjectComponent logTo, org.apache.tools.ant.types.Resource[] source, FileNameMapper mapper, org.apache.tools.ant.types.ResourceFactory targets)Tells which source files should be reprocessed based on the
last modification date of target files.
return selectOutOfDateSources(logTo, source, mapper, targets,
FILE_UTILS.getFileTimestampGranularity());
| public static org.apache.tools.ant.types.Resource[] | selectOutOfDateSources(org.apache.tools.ant.ProjectComponent logTo, org.apache.tools.ant.types.Resource[] source, FileNameMapper mapper, org.apache.tools.ant.types.ResourceFactory targets, long granularity)Tells which source files should be reprocessed based on the
last modification date of target files.
Union u = new Union();
u.addAll(Arrays.asList(source));
ResourceCollection rc
= selectOutOfDateSources(logTo, u, mapper, targets, granularity);
return rc.size() == 0 ? new Resource[0] : ((Union) rc).listResources();
| public static org.apache.tools.ant.types.ResourceCollection | selectOutOfDateSources(org.apache.tools.ant.ProjectComponent logTo, org.apache.tools.ant.types.ResourceCollection source, FileNameMapper mapper, org.apache.tools.ant.types.ResourceFactory targets, long granularity)Tells which sources should be reprocessed based on the
last modification date of targets.
if (source.size() == 0) {
logTo.log("No sources found.", Project.MSG_VERBOSE);
return Resources.NONE;
}
source = Union.getInstance(source);
logFuture(logTo, source, granularity);
Union result = new Union();
for (Iterator iter = source.iterator(); iter.hasNext();) {
Resource sr = (Resource) iter.next();
String srName = sr.getName();
srName = srName == null
? srName : srName.replace('/", File.separatorChar);
String[] targetnames = null;
try {
targetnames = mapper.mapFileName(srName);
} catch (Exception e) {
logTo.log("Caught " + e + " mapping resource " + sr,
Project.MSG_VERBOSE);
}
if (targetnames == null || targetnames.length == 0) {
logTo.log(sr + " skipped - don\'t know how to handle it",
Project.MSG_VERBOSE);
continue;
}
Union targetColl = new Union();
for (int i = 0; i < targetnames.length; i++) {
targetColl.add(targets.getResource(
targetnames[i].replace(File.separatorChar, '/")));
}
//find the out-of-date targets:
Restrict r = new Restrict();
r.add(new And(new ResourceSelector[] {Type.FILE, new Or(
new ResourceSelector[] {NOT_EXISTS, new Outdated(sr, granularity)})}));
r.add(targetColl);
if (r.size() > 0) {
result.add(sr);
Resource t = (Resource) (r.iterator().next());
logTo.log(sr.getName() + " added as " + t.getName()
+ (t.isExists() ? " is outdated." : " doesn\'t exist."),
Project.MSG_VERBOSE);
continue;
}
//log uptodateness of all targets:
logTo.log(sr.getName()
+ " omitted as " + targetColl.toString()
+ (targetColl.size() == 1 ? " is" : " are ")
+ " up to date.", Project.MSG_VERBOSE);
}
return result;
| public static void | setLastModified(org.apache.tools.ant.types.resources.Touchable t, long time)Set the last modified time of an object implementing
org.apache.tools.ant.types.resources.Touchable .
t.touch((time < 0) ? System.currentTimeMillis() : time);
| private static int | textCompare(org.apache.tools.ant.types.Resource r1, org.apache.tools.ant.types.Resource r2)Text compares the contents of two Resources.
Ignores different kinds of line endings.
BufferedReader in1 = null;
BufferedReader in2 = null;
try {
in1 = new BufferedReader(new InputStreamReader(r1.getInputStream()));
in2 = new BufferedReader(new InputStreamReader(r2.getInputStream()));
String expected = in1.readLine();
while (expected != null) {
String actual = in2.readLine();
if (!expected.equals(actual)) {
return expected.compareTo(actual);
}
expected = in1.readLine();
}
return in2.readLine() == null ? 0 : -1;
} finally {
FileUtils.close(in1);
FileUtils.close(in2);
}
|
|