use of org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader 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());
}
}
use of org.apache.tools.ant.filters.util.ChainReaderHelper.ChainReader 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);
}
}
Aggregations