Search in sources :

Example 1 with JavaResource

use of org.apache.tools.ant.types.resources.JavaResource in project ant by apache.

the class LoadProperties method getRequiredJavaResource.

private synchronized JavaResource getRequiredJavaResource() {
    if (src == null) {
        src = new JavaResource();
        src.setProject(getProject());
    } else if (!(src instanceof JavaResource)) {
        throw new BuildException("expected a java resource as source");
    }
    return (JavaResource) src;
}
Also used : BuildException(org.apache.tools.ant.BuildException) JavaResource(org.apache.tools.ant.types.resources.JavaResource)

Example 2 with JavaResource

use of org.apache.tools.ant.types.resources.JavaResource 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)

Aggregations

BuildException (org.apache.tools.ant.BuildException)2 JavaResource (org.apache.tools.ant.types.resources.JavaResource)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Charset (java.nio.charset.Charset)1 Properties (java.util.Properties)1 ChainReaderHelper (org.apache.tools.ant.filters.util.ChainReaderHelper)1 ChainReader (org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader)1