Search in sources :

Example 1 with StringResource

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;
}
Also used : StringResource(org.apache.tools.ant.types.resources.StringResource) Intersect(org.apache.tools.ant.types.resources.Intersect) Restrict(org.apache.tools.ant.types.resources.Restrict) FileResource(org.apache.tools.ant.types.resources.FileResource) LogOutputResource(org.apache.tools.ant.types.resources.LogOutputResource) Resource(org.apache.tools.ant.types.Resource) StringResource(org.apache.tools.ant.types.resources.StringResource) BuildException(org.apache.tools.ant.BuildException)

Example 2 with StringResource

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());
        }
    }
}
Also used : StringResource(org.apache.tools.ant.types.resources.StringResource) FileProvider(org.apache.tools.ant.types.resources.FileProvider) IOException(java.io.IOException) File(java.io.File) Touchable(org.apache.tools.ant.types.resources.Touchable)

Example 3 with StringResource

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());
}
Also used : StringResource(org.apache.tools.ant.types.resources.StringResource) Test(org.junit.Test)

Example 4 with StringResource

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);
}
Also used : ProjectHelper2(org.apache.tools.ant.helper.ProjectHelper2) StringResource(org.apache.tools.ant.types.resources.StringResource) Resource(org.apache.tools.ant.types.Resource) StringResource(org.apache.tools.ant.types.resources.StringResource) FileResource(org.apache.tools.ant.types.resources.FileResource) FileResource(org.apache.tools.ant.types.resources.FileResource) File(java.io.File) Test(org.junit.Test)

Example 5 with StringResource

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());
}
Also used : StringResource(org.apache.tools.ant.types.resources.StringResource) ImmutableResourceException(org.apache.tools.ant.types.resources.ImmutableResourceException) Test(org.junit.Test)

Aggregations

StringResource (org.apache.tools.ant.types.resources.StringResource)5 Test (org.junit.Test)3 File (java.io.File)2 Resource (org.apache.tools.ant.types.Resource)2 FileResource (org.apache.tools.ant.types.resources.FileResource)2 IOException (java.io.IOException)1 BuildException (org.apache.tools.ant.BuildException)1 ProjectHelper2 (org.apache.tools.ant.helper.ProjectHelper2)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 ImmutableResourceException (org.apache.tools.ant.types.resources.ImmutableResourceException)1 Intersect (org.apache.tools.ant.types.resources.Intersect)1 LogOutputResource (org.apache.tools.ant.types.resources.LogOutputResource)1 Restrict (org.apache.tools.ant.types.resources.Restrict)1 Touchable (org.apache.tools.ant.types.resources.Touchable)1