FileDocCategorySizeDatePackage
LoadResource.javaAPI DocApache Ant 1.707214Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs

LoadResource

public class LoadResource extends org.apache.tools.ant.Task
Load a resource into a property
since
Ant 1.7
ant.task
category="utility"

Fields Summary
private org.apache.tools.ant.types.Resource
src
The resource to load.
private boolean
failOnError
what to do when it goes pear-shaped
private boolean
quiet
suppress error message if it goes pear-shaped, sets failOnError=false
private String
encoding
Encoding to use for filenames, defaults to the platform's default encoding.
private String
property
name of property
private final Vector
filterChains
Holds FilterChains
Constructors Summary
Methods Summary
public voidaddConfigured(org.apache.tools.ant.types.ResourceCollection a)
Set the source resource.

param
a the resource to load as a single element Resource collection.

        if (a.size() != 1) {
            throw new BuildException("only single argument resource collections"
                                     + " are supported");
        }
        src = (Resource) a.iterator().next();
    
public final voidaddFilterChain(org.apache.tools.ant.types.FilterChain filter)
Add the FilterChain element.

param
filter the filter to add

        filterChains.addElement(filter);
    
public final voidexecute()
read in a source file to a property

exception
BuildException if something goes wrong with the build

        //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);
            } else {
                log(message, quiet ? Project.MSG_WARN : Project.MSG_ERR);
                return;
            }
        }
        InputStream is = null;
        BufferedInputStream bis = null;
        Reader instream = null;
        log("loading " + src + " into property " + property,
            Project.MSG_VERBOSE);
        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
            is = src.getInputStream();
            bis = new BufferedInputStream(is);
            if (encoding == null) {
                instream = new InputStreamReader(bis);
            } else {
                instream = new InputStreamReader(bis, encoding);
            }

            String text = "";
            if (size != 0) {
                ChainReaderHelper crh = new ChainReaderHelper();
                if (len != Resource.UNKNOWN_SIZE) {
                    crh.setBufferSize(size);
                }
                crh.setPrimaryReader(instream);
                crh.setFilterChains(filterChains);
                crh.setProject(getProject());
                instream = crh.getAssembledReader();

                text = crh.readFully(instream);
            }

            if (text != null) {
                if (text.length() > 0) {
                    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.toString();
            if (failOnError) {
                throw new BuildException(message, ioe, getLocation());
            } else {
                log(message, quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
            }
        } catch (final BuildException be) {
            if (failOnError) {
                throw be;
            } else {
                log(be.getMessage(),
                    quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
            }
        } finally {
            FileUtils.close(is);
        }
    
public final voidsetEncoding(java.lang.String encoding)
Encoding to use for input, defaults to the platform's default encoding.

For a list of possible values see http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html .

param
encoding The new Encoding value


                                      

          
        this.encoding = encoding;
    
public final voidsetFailonerror(boolean fail)
If true, fail on load error.

param
fail The new Failonerror value

        failOnError = fail;
    
public final voidsetProperty(java.lang.String property)
Property name to save to.

param
property The new Property value

        this.property = property;
    
public voidsetQuiet(boolean quiet)
If true, suppress the load error report and set the the failonerror value to false.

param
quiet The new Quiet value

        this.quiet = quiet;
        if (quiet) {
            this.failOnError = false;
        }