Top Description Inners Fields Constructors Methods
org.apache.tools.ant.util

public Class ResourceUtils

extends Object
Class Inheritance
Imports
java.io.BufferedInputStream, .BufferedReader, .BufferedWriter, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .Reader, java.nio.channels.FileChannel, java.nio.charset.Charset, java.nio.file.StandardOpenOption, java.util.Arrays, .Vector, org.apache.tools.ant.Project, .ProjectComponent, org.apache.tools.ant.filters.util.ChainReaderHelper, org.apache.tools.ant.types.FilterChain, .FilterSetCollection, .Resource, .ResourceCollection, .ResourceFactory, .TimeComparison, org.apache.tools.ant.types.resources.Appendable, .FileProvider, .FileResource, .Resources, .Restrict, .StringResource, .Touchable, .Union, org.apache.tools.ant.types.resources.selectors.Date, .ResourceSelector, org.apache.tools.ant.types.selectors.SelectorUtils

This class provides utility methods to process Resources.
Since
Ant 1.5.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static interface

Field Summary

Modifier and TypeField and Description
private static final FileUtils
FILE_UTILS

Utilities used for file operations

public static final String
ISO_8859_1

Name of charset "ISO Latin Alphabet No.

private static final long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static boolean
areSame(final Resource resource1, final Resource resource2)

public static FileResource

Returns:

fileProvider if it is a FileResource instance, or a new FileResource with fileProvider's file.
asFileResource
(final FileProvider
input
fileProvider
)

Convenience method to turn any fileProvider into a basic FileResource with the file's immediate parent as the basedir, for tasks that need one.

private static int

Returns:

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
binaryCompare
(final Resource
the Resource whose content is to be compared.
r1
,
final Resource
the other Resource whose content is to be compared.
r2
)

Binary compares the contents of two Resources.

private static Charset
charsetFor(String encoding)

public static int

Returns:

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
compareContent
(final Resource
the Resource whose content is to be compared.
r1
,
final Resource
the other Resource whose content is to be compared.
r2
,
final boolean
true if the content is to be treated as text and differences in kind of line break are to be ignored.
text
)

Compare the content of two Resources.

public static boolean

Returns:

true if the content of the Resources is the same.
contentEquals
(final Resource
the Resource whose content is to be compared.
r1
,
final Resource
the other Resource whose content is to be compared.
r2
,
final boolean
true if the content is to be treated as text and differences in kind of line break are to be ignored.
text
)

Compares the contents of two Resources.

public static void
copyResource(final Resource
the Resource to copy from. Must not be null.
source
,
final Resource
the Resource to copy to. Must not be null.
dest
)

Convenience method to copy content from one Resource to another.

public static void
copyResource(final Resource
the Resource to copy from. Must not be null.
source
,
final Resource
the Resource to copy to. Must not be null.
dest
,
final Project
the project instance.
project
)

Convenience method to copy content from one Resource to another.

public static void
copyResource(final Resource
the Resource to copy from. Must not be null.
source
,
final Resource
the Resource to copy to. Must not be null.
dest
,
final FilterSetCollection
the collection of filters to apply to this copy.
filters
,
final Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
final boolean
Whether or not the destination Resource should be overwritten if it already exists.
overwrite
,
final boolean
Whether or not the last modified time of the destination Resource should be set to that of the source.
preserveLastModified
,
final String
the encoding used to read the files.
inputEncoding
,
final String
the encoding used to write the files.
outputEncoding
,
final Project
the project instance.
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.

public static void
copyResource(final Resource
the Resource to copy from. Must not be null.
source
,
final Resource
the Resource to copy to. Must not be null.
dest
,
final FilterSetCollection
the collection of filters to apply to this copy.
filters
,
final Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
final boolean
Whether or not the destination Resource should be overwritten if it already exists.
overwrite
,
final boolean
Whether or not the last modified time of the destination Resource should be set to that of the source.
preserveLastModified
,
final boolean
Whether to append to an Appendable Resource.
append
,
final String
the encoding used to read the files.
inputEncoding
,
final String
the encoding used to write the files.
outputEncoding
,
final Project
the project instance.
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.

public static void
copyResource(final Resource
the Resource to copy from. Must not be null.
source
,
final Resource
the Resource to copy to. Must not be null.
dest
,
final FilterSetCollection
the collection of filters to apply to this copy.
filters
,
final Vector<FilterChain>
filterChains to apply during the copy.
filterChains
,
final boolean
Whether or not the destination Resource should be overwritten if it already exists.
overwrite
,
final boolean
Whether or not the last modified time of the destination Resource should be set to that of the source.
preserveLastModified
,
final boolean
Whether to append to an Appendable Resource.
append
,
final String
the encoding used to read the files.
inputEncoding
,
final String
the encoding used to write the files.
outputEncoding
,
final Project
the project instance.
project
,
final boolean
whether read-only target files will be overwritten
force
)

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.

private static void
copyUsingFileChannels(final File sourceFile, final File destFile, final Project project)

private static void
copyUsingStreams(final Resource source, final Resource dest, final boolean append, final Project project)

private static void
copyWithFilterChainsOrTranscoding(final Resource source, final Resource dest, final Vector<FilterChain> filterChains, final boolean append, final String inputEncoding, final String outputEncoding, final Project project)

private static void
copyWithFilterSets(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean append, final String inputEncoding, final String outputEncoding, final Project project)

private static Reader
filterWith(Project project, String encoding, Vector<FilterChain> filterChains, InputStream input)

private static OutputStream
getOutputStream(final Resource resource, final boolean append, final Project project)

private static void
log(final Project project, final String message)

private static void
log(final Project project, final String message, final int level)

private static void
logFuture(final ProjectComponent
the ProjectComponent to do the logging.
logTo
,
final ResourceCollection
the collection of Resources to check.
rc
,
final long
the timestamp granularity to use.
granularity
)

Log which Resources (if any) have been modified in the future.

public static Resource[]

Returns:

array containing the source files which need to be copied or processed, because the targets are out of date or do not exist.
selectOutOfDateSources
(final ProjectComponent
where to send (more or less) interesting output.
logTo
,
final Resource[]
array of resources bearing relative path and last modification date.
source
,
final FileNameMapper
filename mapper indicating how to find the target files.
mapper
,
final ResourceFactory
object able to map as a resource a relative path at destination.
targets
)

Tells which source files should be reprocessed based on the last modification date of target files.

public static Resource[]

Returns:

array containing the source files which need to be copied or processed, because the targets are out of date or do not exist.
selectOutOfDateSources
(final ProjectComponent
where to send (more or less) interesting output.
logTo
,
final Resource[]
array of resources bearing relative path and last modification date.
source
,
final FileNameMapper
filename mapper indicating how to find the target files.
mapper
,
final ResourceFactory
object able to map as a resource a relative path at destination.
targets
,
final long
The number of milliseconds leeway to give before deciding a target is out of date.
granularity
)

Tells which source files should be reprocessed based on the last modification date of target files.

public static ResourceCollection

Returns:

ResourceCollection.
selectOutOfDateSources
(final ProjectComponent
where to send (more or less) interesting output.
logTo
,
final ResourceCollection
ResourceCollection.
source
,
final FileNameMapper
filename mapper indicating how to find the target Resources.
mapper
,
final ResourceFactory
object able to map a relative path as a Resource.
targets
,
final long
The number of milliseconds leeway to give before deciding a target is out of date.
granularity
)

Tells which sources should be reprocessed based on the last modification date of targets.

public static ResourceCollection

Returns:

ResourceCollection.
selectSources
(final ProjectComponent
where to send (more or less) interesting output.
logTo
,
ResourceCollection
ResourceCollection.
source
,
final FileNameMapper
filename mapper indicating how to find the target Resources.
mapper
,
final ResourceFactory
object able to map a relative path as a Resource.
targets
,
final ResourceUtils.ResourceSelectorProvider
returns a selector that is applied to target files. If it selects at least one target the source will be added to the returned collection.
selector
)

Tells which sources should be reprocessed because the given selector selects at least one target.

public static void
setLastModified(final Touchable
the Touchable whose modified time is to be set.
t
,
final long
the time to which the last modified time is to be set. if this is -1, the current time is used.
time
)

Set the last modified time of an object implementing org.apache.tools.ant.types.resources.Touchable .

private static int

Returns:

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
textCompare
(final Resource
the Resource whose content is to be compared.
r1
,
final Resource
the other Resource whose content is to be compared.
r2
)

Text compares the contents of two Resources.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FILE_UTILSback to summary
private static final FileUtils FILE_UTILS

Utilities used for file operations

ISO_8859_1back to summary
public static final String ISO_8859_1

Deprecated

use StandardCharsets.ISO_8859_1

Name of charset "ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1".

Annotations
@Deprecated
Since
Ant 1.8.1
MAX_IO_CHUNK_SIZEback to summary
private static final long MAX_IO_CHUNK_SIZE

Constructor Detail

ResourceUtilsback to summary
public ResourceUtils()

Method Detail

areSameback to summary
private static boolean areSame(final Resource resource1, final Resource resource2) throws IOException
asFileResourceback to summary
public static FileResource asFileResource(final FileProvider fileProvider)

Convenience method to turn any fileProvider into a basic FileResource with the file's immediate parent as the basedir, for tasks that need one.

Parameters
fileProvider:FileProvider

input

Returns:FileResource

fileProvider if it is a FileResource instance, or a new FileResource with fileProvider's file.

Since
Ant 1.8
binaryCompareback to summary
private static int binaryCompare(final Resource r1, final Resource r2) throws IOException

Binary compares the contents of two Resources.

simple but sub-optimal comparison algorithm. written for working rather than fast. Better would be a block read into buffers followed by long comparisons apart from the final 1-7 bytes.

Parameters
r1:Resource

the Resource whose content is to be compared.

r2:Resource

the other Resource whose content is to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Exceptions
IOException:
if the Resources cannot be read.
Since
Ant 1.7
charsetForback to summary
private static Charset charsetFor(String encoding)
compareContentback to summary
public static int compareContent(final Resource r1, final Resource r2, final boolean text) throws IOException

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.

Parameters
r1:Resource

the Resource whose content is to be compared.

r2:Resource

the other Resource whose content is to be compared.

text:boolean

true if the content is to be treated as text and differences in kind of line break are to be ignored.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Exceptions
IOException:
if the Resources cannot be read.
Since
Ant 1.7
contentEqualsback to summary
public static boolean contentEquals(final Resource r1, final Resource r2, final boolean text) throws IOException

Compares the contents of two Resources.

Parameters
r1:Resource

the Resource whose content is to be compared.

r2:Resource

the other Resource whose content is to be compared.

text:boolean

true if the content is to be treated as text and differences in kind of line break are to be ignored.

Returns:boolean

true if the content of the Resources is the same.

Exceptions
IOException:
if the Resources cannot be read.
Since
Ant 1.7
copyResourceback to summary
public static void copyResource(final Resource source, final Resource dest) throws IOException

Convenience method to copy content from one Resource to another. No filtering is performed.

Parameters
source:Resource

the Resource to copy from. Must not be null.

dest:Resource

the Resource to copy to. Must not be null.

Exceptions
IOException:
if the copying fails.
Since
Ant 1.7
copyResourceback to summary
public static void copyResource(final Resource source, final Resource dest, final Project project) throws IOException

Convenience method to copy content from one Resource to another. No filtering is performed.

Parameters
source:Resource

the Resource to copy from. Must not be null.

dest:Resource

the Resource to copy to. Must not be null.

project:Project

the project instance.

Exceptions
IOException:
if the copying fails.
Since
Ant 1.7
copyResourceback to summary
public static void copyResource(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean overwrite, final boolean preserveLastModified, final String inputEncoding, final String outputEncoding, final Project project) throws IOException

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.

Parameters
source:Resource

the Resource to copy from. Must not be null.

dest:Resource

the Resource to copy to. Must not be null.

filters:FilterSetCollection

the collection of filters to apply to this copy.

filterChains:Vector<FilterChain>

filterChains to apply during the copy.

overwrite:boolean

Whether or not the destination Resource should be overwritten if it already exists.

preserveLastModified:boolean

Whether or not the last modified time of the destination Resource should be set to that of the source.

inputEncoding:String

the encoding used to read the files.

outputEncoding:String

the encoding used to write the files.

project:Project

the project instance.

Exceptions
IOException:
if the copying fails.
Since
Ant 1.7
copyResourceback to summary
public static void copyResource(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean overwrite, final boolean preserveLastModified, final boolean append, final String inputEncoding, final String outputEncoding, final Project project) throws IOException

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.

Parameters
source:Resource

the Resource to copy from. Must not be null.

dest:Resource

the Resource to copy to. Must not be null.

filters:FilterSetCollection

the collection of filters to apply to this copy.

filterChains:Vector<FilterChain>

filterChains to apply during the copy.

overwrite:boolean

Whether or not the destination Resource should be overwritten if it already exists.

preserveLastModified:boolean

Whether or not the last modified time of the destination Resource should be set to that of the source.

append:boolean

Whether to append to an Appendable Resource.

inputEncoding:String

the encoding used to read the files.

outputEncoding:String

the encoding used to write the files.

project:Project

the project instance.

Exceptions
IOException:
if the copying fails.
Since
Ant 1.8
copyResourceback to summary
public static void copyResource(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean overwrite, final boolean preserveLastModified, final boolean append, final String inputEncoding, final String outputEncoding, final Project project, final boolean force) throws IOException

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.

Parameters
source:Resource

the Resource to copy from. Must not be null.

dest:Resource

the Resource to copy to. Must not be null.

filters:FilterSetCollection

the collection of filters to apply to this copy.

filterChains:Vector<FilterChain>

filterChains to apply during the copy.

overwrite:boolean

Whether or not the destination Resource should be overwritten if it already exists.

preserveLastModified:boolean

Whether or not the last modified time of the destination Resource should be set to that of the source.

append:boolean

Whether to append to an Appendable Resource.

inputEncoding:String

the encoding used to read the files.

outputEncoding:String

the encoding used to write the files.

project:Project

the project instance.

force:boolean

whether read-only target files will be overwritten

Exceptions
IOException:
if the copying fails.
Since
Ant 1.8.2
copyUsingFileChannelsback to summary
private static void copyUsingFileChannels(final File sourceFile, final File destFile, final Project project) throws IOException
copyUsingStreamsback to summary
private static void copyUsingStreams(final Resource source, final Resource dest, final boolean append, final Project project) throws IOException
copyWithFilterChainsOrTranscodingback to summary
private static void copyWithFilterChainsOrTranscoding(final Resource source, final Resource dest, final Vector<FilterChain> filterChains, final boolean append, final String inputEncoding, final String outputEncoding, final Project project) throws IOException
copyWithFilterSetsback to summary
private static void copyWithFilterSets(final Resource source, final Resource dest, final FilterSetCollection filters, final Vector<FilterChain> filterChains, final boolean append, final String inputEncoding, final String outputEncoding, final Project project) throws IOException
filterWithback to summary
private static Reader filterWith(Project project, String encoding, Vector<FilterChain> filterChains, InputStream input)
getOutputStreamback to summary
private static OutputStream getOutputStream(final Resource resource, final boolean append, final Project project) throws IOException
logback to summary
private static void log(final Project project, final String message)
logback to summary
private static void log(final Project project, final String message, final int level)
logFutureback to summary
private static void logFuture(final ProjectComponent logTo, final ResourceCollection rc, final long granularity)

Log which Resources (if any) have been modified in the future.

Parameters
logTo:ProjectComponent

the ProjectComponent to do the logging.

rc:ResourceCollection

the collection of Resources to check.

granularity:long

the timestamp granularity to use.

Since
Ant 1.7
selectOutOfDateSourcesback to summary
public static Resource[] selectOutOfDateSources(final ProjectComponent logTo, final Resource[] source, final FileNameMapper mapper, final ResourceFactory targets)

Tells which source files should be reprocessed based on the last modification date of target files.

Parameters
logTo:ProjectComponent

where to send (more or less) interesting output.

source:Resource[]

array of resources bearing relative path and last modification date.

mapper:FileNameMapper

filename mapper indicating how to find the target files.

targets:ResourceFactory

object able to map as a resource a relative path at destination.

Returns:Resource[]

array containing the source files which need to be copied or processed, because the targets are out of date or do not exist.

selectOutOfDateSourcesback to summary
public static Resource[] selectOutOfDateSources(final ProjectComponent logTo, final Resource[] source, final FileNameMapper mapper, final ResourceFactory targets, final long granularity)

Tells which source files should be reprocessed based on the last modification date of target files.

Parameters
logTo:ProjectComponent

where to send (more or less) interesting output.

source:Resource[]

array of resources bearing relative path and last modification date.

mapper:FileNameMapper

filename mapper indicating how to find the target files.

targets:ResourceFactory

object able to map as a resource a relative path at destination.

granularity:long

The number of milliseconds leeway to give before deciding a target is out of date.

Returns:Resource[]

array containing the source files which need to be copied or processed, because the targets are out of date or do not exist.

Since
Ant 1.6.2
selectOutOfDateSourcesback to summary
public static ResourceCollection selectOutOfDateSources(final ProjectComponent logTo, final ResourceCollection source, final FileNameMapper mapper, final ResourceFactory targets, final long granularity)

Tells which sources should be reprocessed based on the last modification date of targets.

Parameters
logTo:ProjectComponent

where to send (more or less) interesting output.

source:ResourceCollection

ResourceCollection.

mapper:FileNameMapper

filename mapper indicating how to find the target Resources.

targets:ResourceFactory

object able to map a relative path as a Resource.

granularity:long

The number of milliseconds leeway to give before deciding a target is out of date.

Returns:ResourceCollection

ResourceCollection.

Since
Ant 1.7
selectSourcesback to summary
public static ResourceCollection selectSources(final ProjectComponent logTo, ResourceCollection source, final FileNameMapper mapper, final ResourceFactory targets, final ResourceUtils.ResourceSelectorProvider selector)

Tells which sources should be reprocessed because the given selector selects at least one target.

Parameters
logTo:ProjectComponent

where to send (more or less) interesting output.

source:ResourceCollection

ResourceCollection.

mapper:FileNameMapper

filename mapper indicating how to find the target Resources.

targets:ResourceFactory

object able to map a relative path as a Resource.

selector:ResourceUtils.ResourceSelectorProvider

returns a selector that is applied to target files. If it selects at least one target the source will be added to the returned collection.

Returns:ResourceCollection

ResourceCollection.

Since
Ant 1.8.0
setLastModifiedback to summary
public static void setLastModified(final Touchable t, final long time)

Set the last modified time of an object implementing org.apache.tools.ant.types.resources.Touchable .

Parameters
t:Touchable

the Touchable whose modified time is to be set.

time:long

the time to which the last modified time is to be set. if this is -1, the current time is used.

Since
Ant 1.7
textCompareback to summary
private static int textCompare(final Resource r1, final Resource r2) throws IOException

Text compares the contents of two Resources. Ignores different kinds of line endings.

Parameters
r1:Resource

the Resource whose content is to be compared.

r2:Resource

the other Resource whose content is to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Exceptions
IOException:
if the Resources cannot be read.
Since
Ant 1.7
org.apache.tools.ant.util back to summary

public Class ResourceUtils.ReadOnlyTargetFileException

extends IOException
Class Inheritance

Since
Ant 1.9.4

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.io.IOException.serialVersionUID.

Constructor Detail

ReadOnlyTargetFileExceptionback to summary
public ReadOnlyTargetFileException(final File destFile)
org.apache.tools.ant.util back to summary

public Interface ResourceUtils.ResourceSelectorProvider


Method Summary

Modifier and TypeMethod and Description
public ResourceSelector

Method Detail

getTargetSelectorForSourceback to summary
public ResourceSelector getTargetSelectorForSource(Resource source)