ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stuart Halloway" <...@develop.com>
Subject proposal: load large property strings from a file
Date Fri, 02 Mar 2001 16:12:02 GMT
I am working on a project with a code generation step that combines pieces
of several different files.  Here's the ant-based solution I came up with:
1. make a master file with token filters for other files that need be merged
in, and 2. load the token values from a file.

The filter task does not lend itself to this, so I considered (briefly)
adding an attribute to load an entire file as the value of a token.  Then I
hit on just adding the capability to the property task instead, which is
more general.

The code change is pretty small, and included in its entirety below.  I have
not participated in Ant development before, but would be glad to learn what
else I need to do to formalize adding this.  I would also appreciate being
told if there are other, more obvious ways to do what I am asking that are
already part of Ant. :-)

Stuart Halloway
DevelopMentor
http://staff.develop.com/halloway

//------------------------------------------------------------
//add to org.apache.tools.ant.taskdefs.Property
    protected File fileContents;

    public void setFileContents(File fileContents) {
        this.fileContents = fileContents;
    }

    //add this line to execute()
    if ((name != null ) && (fileContents != null)) loadFileContents(name,
fileContents);

    protected void loadFileContents (String name, File file) throws
BuildException {
        Properties props = new Properties();
        log("Loading contents " + file.getAbsolutePath(),
Project.MSG_VERBOSE);
        try {
            if (file.exists()) {
                FileInputStream fis = new FileInputStream(file);
		long size = file.length();
		if (size > Integer.MAX_VALUE) {
		  throw new IllegalArgumentException("File too large " +
file.getAbsolutePath());
		}
		byte[] buf = new byte[(int)size];
                try {
		  fis.read(buf);
                } finally {
                    if (fis != null) {
                        fis.close();
                    }
                }
		String val = new String(buf);
		addProperty(name, new String(buf));
            } else {
                log("Unable to find " + file.getAbsolutePath(),
                    Project.MSG_VERBOSE);
            }
        } catch(Exception ex) {
            throw new BuildException(ex.getMessage(), ex, location);
        }
    }


Mime
View raw message