Search in sources :

Example 6 with FilterChain

use of org.apache.tools.ant.types.FilterChain in project ant by apache.

the class ResourceListTest method testEmptyElementWithReference.

@Test
public void testEmptyElementWithReference() {
    ResourceList rl = new ResourceList();
    rl.setEncoding("foo");
    try {
        rl.setRefid(new Reference(buildRule.getProject(), "dummyref"));
        fail("Can add reference to ResourceList with encoding attribute set.");
    } catch (BuildException be) {
        assertEquals("You must not specify more than one attribute when using refid", be.getMessage());
    }
    rl = new ResourceList();
    rl.setRefid(new Reference(buildRule.getProject(), "dummyref"));
    try {
        rl.setEncoding("foo");
        fail("Can set encoding in ResourceList that is a reference");
    } catch (BuildException be) {
        assertEquals("You must not specify more than one attribute when using refid", be.getMessage());
    }
    rl = new ResourceList();
    rl.add(new FileResource(buildRule.getProject(), "."));
    try {
        rl.setRefid(new Reference(buildRule.getProject(), "dummyref"));
        fail("Can add reference to ResourceList with nested resource collection.");
    } catch (BuildException be) {
        assertEquals("You must not specify nested elements when using refid", be.getMessage());
    }
    rl = new ResourceList();
    rl.setRefid(new Reference(buildRule.getProject(), "dummyref"));
    try {
        rl.add(new FileResource(buildRule.getProject(), "."));
        fail("Can add reference to ResourceList with nested resource collection.");
    } catch (BuildException be) {
        assertEquals("You must not specify nested elements when using refid", be.getMessage());
    }
    rl = new ResourceList();
    rl.addFilterChain(new FilterChain());
    try {
        rl.setRefid(new Reference(buildRule.getProject(), "dummyref"));
        fail("Can add reference to ResourceList with nested filter chain.");
    } catch (BuildException be) {
        assertEquals("You must not specify nested elements when using refid", be.getMessage());
    }
    rl = new ResourceList();
    rl.setRefid(new Reference(buildRule.getProject(), "dummyref"));
    try {
        rl.addFilterChain(new FilterChain());
        fail("Can add reference to ResourceList with nested filter chain.");
    } catch (BuildException be) {
        assertEquals("You must not specify nested elements when using refid", be.getMessage());
    }
}
Also used : Reference(org.apache.tools.ant.types.Reference) FilterChain(org.apache.tools.ant.types.FilterChain) BuildException(org.apache.tools.ant.BuildException) Test(org.junit.Test)

Example 7 with FilterChain

use of org.apache.tools.ant.types.FilterChain in project ant by apache.

the class ChainReaderHelper method getAssembledReader.

/**
 * Assemble the reader
 * @return the assembled reader
 * @exception BuildException if an error occurs
 */
public ChainReader getAssembledReader() throws BuildException {
    if (primaryReader == null) {
        throw new BuildException("primaryReader must not be null.");
    }
    Reader instream = primaryReader;
    final List<AntClassLoader> classLoadersToCleanUp = new ArrayList<>();
    final List<Object> finalFilters = filterChains.stream().map(FilterChain::getFilterReaders).flatMap(Collection::stream).collect(Collectors.toList());
    if (!finalFilters.isEmpty()) {
        boolean success = false;
        try {
            for (Object o : finalFilters) {
                if (o instanceof AntFilterReader) {
                    instream = expandReader((AntFilterReader) o, instream, classLoadersToCleanUp);
                } else if (o instanceof ChainableReader) {
                    setProjectOnObject(o);
                    instream = ((ChainableReader) o).chain(instream);
                    setProjectOnObject(instream);
                }
            }
            success = true;
        } finally {
            if (!success && !classLoadersToCleanUp.isEmpty()) {
                cleanUpClassLoaders(classLoadersToCleanUp);
            }
        }
    }
    return new ChainReader(instream, classLoadersToCleanUp);
}
Also used : AntFilterReader(org.apache.tools.ant.types.AntFilterReader) FilterChain(org.apache.tools.ant.types.FilterChain) ArrayList(java.util.ArrayList) FilterReader(java.io.FilterReader) BaseFilterReader(org.apache.tools.ant.filters.BaseFilterReader) Reader(java.io.Reader) ChainableReader(org.apache.tools.ant.filters.ChainableReader) AntFilterReader(org.apache.tools.ant.types.AntFilterReader) AntClassLoader(org.apache.tools.ant.AntClassLoader) BuildException(org.apache.tools.ant.BuildException) ChainableReader(org.apache.tools.ant.filters.ChainableReader)

Aggregations

FilterChain (org.apache.tools.ant.types.FilterChain)7 BuildException (org.apache.tools.ant.BuildException)5 Reader (java.io.Reader)2 File (java.io.File)1 FilterReader (java.io.FilterReader)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1 AntClassLoader (org.apache.tools.ant.AntClassLoader)1 UnsupportedAttributeException (org.apache.tools.ant.UnsupportedAttributeException)1 BaseFilterReader (org.apache.tools.ant.filters.BaseFilterReader)1 ChainableReader (org.apache.tools.ant.filters.ChainableReader)1 LineContainsRegExp (org.apache.tools.ant.filters.LineContainsRegExp)1 TokenFilter (org.apache.tools.ant.filters.TokenFilter)1 ChainReaderHelper (org.apache.tools.ant.filters.util.ChainReaderHelper)1 AntFilterReader (org.apache.tools.ant.types.AntFilterReader)1 Path (org.apache.tools.ant.types.Path)1 RedirectorElement (org.apache.tools.ant.types.RedirectorElement)1 Reference (org.apache.tools.ant.types.Reference)1