use of org.apache.tools.ant.types.resources.StringResource in project ant by apache.
the class Concat method getResources.
/**
* Get the resources to concatenate.
*/
private ResourceCollection getResources() {
if (rc == null) {
return new StringResource(getProject(), textBuffer.toString());
}
if (dest != null) {
Intersect checkDestNotInSources = new Intersect();
checkDestNotInSources.setProject(getProject());
checkDestNotInSources.add(rc);
checkDestNotInSources.add(dest);
if (checkDestNotInSources.size() > 0) {
throw new BuildException("Destination resource %s was specified as an input resource.", dest);
}
}
Restrict noexistRc = new Restrict();
noexistRc.add(NOT_EXISTS);
noexistRc.add(rc);
for (Resource r : noexistRc) {
log(r + " does not exist.", Project.MSG_ERR);
}
Restrict result = new Restrict();
result.add(EXISTS);
result.add(rc);
return result;
}
use of org.apache.tools.ant.types.resources.StringResource in project ant by apache.
the class ResourceUtils method copyResource.
/**
* 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 <code>dest</code> file should be made
* equal to the last modified time of <code>source</code>.
*
* @param source the Resource to copy from.
* Must not be <code>null</code>.
* @param dest the Resource to copy to.
* Must not be <code>null</code>.
* @param filters the collection of filters to apply to this copy.
* @param filterChains filterChains to apply during the copy.
* @param overwrite Whether or not the destination Resource should be
* overwritten if it already exists.
* @param preserveLastModified Whether or not the last modified time of
* the destination Resource should be set to that
* of the source.
* @param append Whether to append to an Appendable Resource.
* @param inputEncoding the encoding used to read the files.
* @param outputEncoding the encoding used to write the files.
* @param project the project instance.
* @param force whether read-only target files will be overwritten
*
* @throws IOException if the copying fails.
*
* @since Ant 1.8.2
*/
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 {
if (!overwrite && !SelectorUtils.isOutOfDate(source, dest, FileUtils.getFileUtils().getFileTimestampGranularity())) {
return;
}
final boolean filterSetsAvailable = (filters != null && filters.hasFilters());
final boolean filterChainsAvailable = (filterChains != null && !filterChains.isEmpty());
String effectiveInputEncoding;
if (source instanceof StringResource) {
effectiveInputEncoding = ((StringResource) source).getEncoding();
} else {
effectiveInputEncoding = inputEncoding;
}
File destFile = null;
if (dest.as(FileProvider.class) != null) {
destFile = dest.as(FileProvider.class).getFile();
}
if (destFile != null && destFile.isFile() && !destFile.canWrite()) {
if (!force) {
throw new ReadOnlyTargetFileException(destFile);
}
if (!FILE_UTILS.tryHardToDelete(destFile)) {
throw new IOException("failed to delete read-only destination file " + destFile);
}
}
if (filterSetsAvailable) {
copyWithFilterSets(source, dest, filters, filterChains, append, effectiveInputEncoding, outputEncoding, project);
} else if (filterChainsAvailable || (effectiveInputEncoding != null && !effectiveInputEncoding.equals(outputEncoding)) || (effectiveInputEncoding == null && outputEncoding != null)) {
copyWithFilterChainsOrTranscoding(source, dest, filterChains, append, effectiveInputEncoding, outputEncoding, project);
} else {
boolean copied = false;
if (source.as(FileProvider.class) != null && destFile != null && !append) {
final File sourceFile = source.as(FileProvider.class).getFile();
try {
copyUsingFileChannels(sourceFile, destFile, project);
copied = true;
} catch (final IOException ex) {
String msg = "Attempt to copy " + sourceFile + " to " + destFile + " using NIO Channels" + " failed due to '" + ex.getMessage() + "'. Falling back to streams.";
if (project != null) {
project.log(msg, Project.MSG_WARN);
} else {
System.err.println(msg);
}
}
}
if (!copied) {
copyUsingStreams(source, dest, append, project);
}
}
if (preserveLastModified) {
final Touchable t = dest.as(Touchable.class);
if (t != null) {
setLastModified(t, source.getLastModified());
}
}
}
use of org.apache.tools.ant.types.resources.StringResource in project ant by apache.
the class ResourceOutputTest method teststringoutput1.
@Test
public void teststringoutput1() {
StringResource r = new StringResource();
testoutputbe(r);
assertEquals("foo", r.getValue());
}
use of org.apache.tools.ant.types.resources.StringResource in project ant by apache.
the class ProjectHelperRepositoryTest method testFind.
@Test
public void testFind() {
ProjectHelperRepository repo = ProjectHelperRepository.getInstance();
repo.registerProjectHelper(SomeHelper.class);
Resource r = new FileResource(new File("test.xml"));
ProjectHelper helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof ProjectHelper2);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof ProjectHelper2);
r = new FileResource(new File("test.myext"));
helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof SomeHelper);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof SomeHelper);
r = new StringResource("test.myext");
helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof ProjectHelper2);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof ProjectHelper2);
r = new StringResource("test.other");
helper = repo.getProjectHelperForBuildFile(r);
assertTrue(helper instanceof ProjectHelper2);
helper = repo.getProjectHelperForAntlib(r);
assertTrue(helper instanceof ProjectHelper2);
}
use of org.apache.tools.ant.types.resources.StringResource in project ant by apache.
the class ResourceOutputTest method teststringoutput2.
@Test
public void teststringoutput2() throws IOException {
StringResource r = new StringResource("bar");
try {
testoutput(r);
fail("should have caught ImmutableResourceException");
} catch (ImmutableResourceException e) {
// TODO assert exception message
}
assertEquals("bar", r.getValue());
}
Aggregations