Search in sources :

Example 1 with ChainReaderHelper

use of org.apache.tools.ant.filters.util.ChainReaderHelper in project ant by apache.

the class ResourceUtils method filterWith.

private static Reader filterWith(Project project, String encoding, Vector<FilterChain> filterChains, InputStream input) {
    Reader r = new InputStreamReader(input, charsetFor(encoding));
    if (filterChains != null && !filterChains.isEmpty()) {
        final ChainReaderHelper crh = new ChainReaderHelper();
        crh.setBufferSize(FileUtils.BUF_SIZE);
        crh.setPrimaryReader(r);
        crh.setFilterChains(filterChains);
        crh.setProject(project);
        r = crh.getAssembledReader();
    }
    return new BufferedReader(r);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper)

Example 2 with ChainReaderHelper

use of org.apache.tools.ant.filters.util.ChainReaderHelper in project ant by apache.

the class Redirector method createStreams.

/**
 * Create the input, error and output streams based on the configuration
 * options.
 */
public void createStreams() {
    synchronized (outMutex) {
        outStreams();
        if (alwaysLogOut || outputStream == null) {
            final OutputStream outputLog = new LogOutputStream(managingTask, Project.MSG_INFO);
            outputStream = (outputStream == null) ? outputLog : new TeeOutputStream(outputLog, outputStream);
        }
        if ((outputFilterChains != null && outputFilterChains.size() > 0) || !(outputEncoding.equalsIgnoreCase(inputEncoding))) {
            try {
                final LeadPipeInputStream snk = new LeadPipeInputStream();
                snk.setManagingComponent(managingTask);
                InputStream outPumpIn = snk;
                Reader reader = new InputStreamReader(outPumpIn, inputEncoding);
                if (outputFilterChains != null && outputFilterChains.size() > 0) {
                    final ChainReaderHelper helper = new ChainReaderHelper();
                    helper.setProject(managingTask.getProject());
                    helper.setPrimaryReader(reader);
                    helper.setFilterChains(outputFilterChains);
                    reader = helper.getAssembledReader();
                }
                outPumpIn = new ReaderInputStream(reader, outputEncoding);
                final Thread t = new Thread(threadGroup, new StreamPumper(outPumpIn, outputStream, true), "output pumper");
                t.setPriority(Thread.MAX_PRIORITY);
                outputStream = new PipedOutputStream(snk);
                t.start();
            } catch (final IOException eyeOhEx) {
                throw new BuildException("error setting up output stream", eyeOhEx);
            }
        }
    }
    synchronized (errMutex) {
        errorStreams();
        if (alwaysLogErr || errorStream == null) {
            final OutputStream errorLog = new LogOutputStream(managingTask, Project.MSG_WARN);
            errorStream = (errorStream == null) ? errorLog : new TeeOutputStream(errorLog, errorStream);
        }
        if ((errorFilterChains != null && errorFilterChains.size() > 0) || !(errorEncoding.equalsIgnoreCase(inputEncoding))) {
            try {
                final LeadPipeInputStream snk = new LeadPipeInputStream();
                snk.setManagingComponent(managingTask);
                InputStream errPumpIn = snk;
                Reader reader = new InputStreamReader(errPumpIn, inputEncoding);
                if (errorFilterChains != null && errorFilterChains.size() > 0) {
                    final ChainReaderHelper helper = new ChainReaderHelper();
                    helper.setProject(managingTask.getProject());
                    helper.setPrimaryReader(reader);
                    helper.setFilterChains(errorFilterChains);
                    reader = helper.getAssembledReader();
                }
                errPumpIn = new ReaderInputStream(reader, errorEncoding);
                final Thread t = new Thread(threadGroup, new StreamPumper(errPumpIn, errorStream, true), "error pumper");
                t.setPriority(Thread.MAX_PRIORITY);
                errorStream = new PipedOutputStream(snk);
                t.start();
            } catch (final IOException eyeOhEx) {
                throw new BuildException("error setting up error stream", eyeOhEx);
            }
        }
    }
    synchronized (inMutex) {
        // whatever warnings are needed
        if (input != null && input.length > 0) {
            managingTask.log("Redirecting input from file" + ((input.length == 1) ? "" : "s"), Project.MSG_VERBOSE);
            try {
                inputStream = new ConcatFileInputStream(input);
            } catch (final IOException eyeOhEx) {
                throw new BuildException(eyeOhEx);
            }
            ((ConcatFileInputStream) inputStream).setManagingComponent(managingTask);
        } else if (inputString != null) {
            final StringBuffer buf = new StringBuffer("Using input ");
            if (logInputString) {
                buf.append('"').append(inputString).append('"');
            } else {
                buf.append("string");
            }
            managingTask.log(buf.toString(), Project.MSG_VERBOSE);
            inputStream = new ByteArrayInputStream(inputString.getBytes());
        }
        if (inputStream != null && inputFilterChains != null && inputFilterChains.size() > 0) {
            final ChainReaderHelper helper = new ChainReaderHelper();
            helper.setProject(managingTask.getProject());
            try {
                helper.setPrimaryReader(new InputStreamReader(inputStream, inputEncoding));
            } catch (final IOException eyeOhEx) {
                throw new BuildException("error setting up input stream", eyeOhEx);
            }
            helper.setFilterChains(inputFilterChains);
            inputStream = new ReaderInputStream(helper.getAssembledReader(), inputEncoding);
        }
    }
}
Also used : TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) InputStreamReader(java.io.InputStreamReader) LeadPipeInputStream(org.apache.tools.ant.util.LeadPipeInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.apache.tools.ant.util.ReaderInputStream) ConcatFileInputStream(org.apache.tools.ant.util.ConcatFileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeepAliveOutputStream(org.apache.tools.ant.util.KeepAliveOutputStream) OutputStream(java.io.OutputStream) LazyFileOutputStream(org.apache.tools.ant.util.LazyFileOutputStream) PipedOutputStream(java.io.PipedOutputStream) TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) PipedOutputStream(java.io.PipedOutputStream) IOException(java.io.IOException) ConcatFileInputStream(org.apache.tools.ant.util.ConcatFileInputStream) ReaderInputStream(org.apache.tools.ant.util.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LeadPipeInputStream(org.apache.tools.ant.util.LeadPipeInputStream) ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper) BuildException(org.apache.tools.ant.BuildException)

Example 3 with ChainReaderHelper

use of org.apache.tools.ant.filters.util.ChainReaderHelper in project ant by apache.

the class Concat method getFilteredReader.

private Reader getFilteredReader(Reader r) {
    if (filterChains == null) {
        return r;
    }
    ChainReaderHelper helper = new ChainReaderHelper();
    helper.setBufferSize(BUFFER_SIZE);
    helper.setPrimaryReader(r);
    helper.setFilterChains(filterChains);
    helper.setProject(getProject());
    // used to be a BufferedReader here, but we should be buffering lower:
    return helper.getAssembledReader();
}
Also used : ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper)

Example 4 with ChainReaderHelper

use of org.apache.tools.ant.filters.util.ChainReaderHelper in project ant by apache.

the class LoadProperties method execute.

/**
 * load Ant properties from the source file or resource
 *
 * @exception BuildException if something goes wrong with the build
 */
@Override
public final void execute() throws BuildException {
    // validation
    if (src == null) {
        throw new BuildException("A source resource is required.");
    }
    if (!src.isExists()) {
        if (src instanceof JavaResource) {
            // dreaded backwards compatibility
            log("Unable to find resource " + src, Project.MSG_WARN);
            return;
        }
        throw new BuildException("Source resource does not exist: " + src);
    }
    Charset charset = encoding == null ? Charset.defaultCharset() : Charset.forName(encoding);
    try (ChainReader instream = new ChainReaderHelper(getProject(), new InputStreamReader(new BufferedInputStream(src.getInputStream()), charset), filterChains).getAssembledReader()) {
        String text = instream.readFully();
        if (text != null && !text.isEmpty()) {
            if (!text.endsWith("\n")) {
                text = text + "\n";
            }
            ByteArrayInputStream tis = new ByteArrayInputStream(text.getBytes(ResourceUtils.ISO_8859_1));
            final Properties props = new Properties();
            props.load(tis);
            Property propertyTask = new Property();
            propertyTask.bindToOwner(this);
            propertyTask.setPrefix(prefix);
            propertyTask.setPrefixValues(prefixValues);
            propertyTask.addProperties(props);
        }
    } catch (final IOException ioe) {
        throw new BuildException("Unable to load file: " + ioe, ioe, getLocation());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ChainReader(org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) Properties(java.util.Properties) JavaResource(org.apache.tools.ant.types.resources.JavaResource)

Example 5 with ChainReaderHelper

use of org.apache.tools.ant.filters.util.ChainReaderHelper in project ant by apache.

the class LoadResource method execute.

/**
 * read in a source file to a property
 *
 * @exception BuildException if something goes wrong with the build
 */
@Override
public final void execute() throws BuildException {
    // validation
    if (src == null) {
        throw new BuildException("source resource not defined");
    }
    if (property == null) {
        throw new BuildException("output property not defined");
    }
    if (quiet && failOnError) {
        throw new BuildException("quiet and failonerror cannot both be set to true");
    }
    if (!src.isExists()) {
        String message = src + " doesn't exist";
        if (failOnError) {
            throw new BuildException(message);
        }
        log(message, quiet ? Project.MSG_WARN : Project.MSG_ERR);
        return;
    }
    log("loading " + src + " into property " + property, Project.MSG_VERBOSE);
    Charset charset = encoding == null ? Charset.defaultCharset() : Charset.forName(encoding);
    try {
        final long len = src.getSize();
        log("resource size = " + (len != Resource.UNKNOWN_SIZE ? String.valueOf(len) : "unknown"), Project.MSG_DEBUG);
        // discard most of really big resources
        final int size = (int) len;
        // open up the resource
        String text;
        if (size != 0) {
            try (ChainReader chainReader = new ChainReaderHelper(getProject(), new InputStreamReader(new BufferedInputStream(src.getInputStream()), charset), filterChains).with(crh -> {
                if (src.getSize() != Resource.UNKNOWN_SIZE) {
                    crh.setBufferSize(size);
                }
            }).getAssembledReader()) {
                text = chainReader.readFully();
            }
        } else {
            log("Do not set property " + property + " as its length is 0.", quiet ? Project.MSG_VERBOSE : Project.MSG_INFO);
            text = null;
        }
        if (text != null && !text.isEmpty()) {
            getProject().setNewProperty(property, text);
            log("loaded " + text.length() + " characters", Project.MSG_VERBOSE);
            log(property + " := " + text, Project.MSG_DEBUG);
        }
    } catch (final IOException ioe) {
        final String message = "Unable to load resource: " + ioe;
        if (failOnError) {
            throw new BuildException(message, ioe, getLocation());
        }
        log(message, quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
    } catch (final BuildException be) {
        if (failOnError) {
            throw be;
        }
        log(be.getMessage(), quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
    }
}
Also used : Resource(org.apache.tools.ant.types.Resource) BufferedInputStream(java.io.BufferedInputStream) Task(org.apache.tools.ant.Task) FilterChain(org.apache.tools.ant.types.FilterChain) ChainReader(org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) InputStreamReader(java.io.InputStreamReader) ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper) List(java.util.List) Vector(java.util.Vector) Charset(java.nio.charset.Charset) Project(org.apache.tools.ant.Project) InputStreamReader(java.io.InputStreamReader) ChainReader(org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader) BufferedInputStream(java.io.BufferedInputStream) Charset(java.nio.charset.Charset) ChainReaderHelper(org.apache.tools.ant.filters.util.ChainReaderHelper) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException)

Aggregations

ChainReaderHelper (org.apache.tools.ant.filters.util.ChainReaderHelper)7 InputStreamReader (java.io.InputStreamReader)5 BuildException (org.apache.tools.ant.BuildException)4 BufferedInputStream (java.io.BufferedInputStream)3 IOException (java.io.IOException)3 Reader (java.io.Reader)3 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringReader (java.io.StringReader)2 Charset (java.nio.charset.Charset)2 Vector (java.util.Vector)2 ChainReader (org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader)2 FilterChain (org.apache.tools.ant.types.FilterChain)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 List (java.util.List)1 Properties (java.util.Properties)1 Project (org.apache.tools.ant.Project)1