ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stover, Michael" <Michael.Sto...@usa.xerox.com>
Subject Some bug fixes and minor additions
Date Mon, 04 Sep 2000 16:21:47 GMT
Hi, this is my first post to Ant, but I've been working on developing a GUI
front-end to Ant so that it can be used as installer software (ala
InstallShield).  In doing this, I've run across a few bugs, and some things
I've added for convenience.

1.  In the task Available.java, the work is done in the init() method.  This
didn't work out well because my build file changes things as it runs, and I
expected available to determine whether a resource was available at that
point in the run, so I changed init() to execute().

2.  When using the "if" attribute for a target, it could only ask if
something exists, and execute if it did.  However, I wanted to ask if
something didn't exist, and execute the task only then.  At first, I tried
some very clumsy, awkward stuff in the build file, but then I got tired of
debugging that, so I added the ability to use the NOT operator within the if
value.  So, I can write <target name="install_apache" depends="init"
if="!apache_installed">, and it will install apache only if it's not already
installed (or, to be precise, only if the property, "apache_installed", does
NOT exist).

Here's the code for this change:
In Target.java:

public void execute() throws BuildException {
	/* Modified the conditional */
        if (("".equals(this.condition)) ||
(project.getProperty(this.condition) != null) ||
                (this.condition.startsWith("!") &&
project.getProperty(this.condition.substring(1)) == null) ) {
	/* No other changes */

            Enumeration enum = tasks.elements();
            while (enum.hasMoreElements()) {
                Task task = (Task) enum.nextElement();
                task.execute();
            }
        } else {
            project.log("Skipped because property '" + this.condition + "'
not set.", this.name, Project.MSG_VERBOSE);
        }
    } // End execute()

Possibly, it might be preferrable to have an "unless" attribute for the
target tag instead of this "parsing" of the if conditional, but this was so
simple, and it serves my needs completely, I'm not sure it'd be worth it.
However, if some see a need for more complex conditionals, or if there would
be temptation to grow the parsing function, then something like "unless"
might be a better approach.

3. Lastly, I fixed a bug with using filter to replace.  If you are replacing
tags like @year@ within a line that has other "@" characters, then I don't
think the current version works.  Here's my version of the
replace(String,Hashtable) method within Project.java:

	/**
     * Does replacement on the given string using the given token table.
     *
     * @returns the string with the token replaced.
     */
    private String replace(String s, Hashtable tokens) {
        int index = s.indexOf(TOKEN_START);
        StringTokenizer tokenizer = new StringTokenizer(s,TOKEN_START,true);
        StringBuffer b = new StringBuffer();
        boolean replaced = false;
        int numTokens = tokenizer.countTokens();
        int count = 0;
        while(tokenizer.hasMoreTokens())
        {
          count++;
          String temp = tokenizer.nextToken();
          if(tokens.containsKey(temp) && count < numTokens)
          {
            b.setLength(b.length()-TOKEN_START.length());
            b.append((String)tokens.get(temp));
            replaced = true;
          }
          else
          {
            if(!replaced)
              b.append(temp);
            replaced = false;
          }
        }
        return b.toString();
    }

The entire method is different, so this replaces it all.

Apologies if this is based on old code - I've got the latest release, but
not the latest from cvs.

-Mike Stover

Mime
View raw message