diff --git a/build.xml b/build.xml index 14cb16038..7357672f0 100644 --- a/build.xml +++ b/build.xml @@ -28,6 +28,8 @@ + + @@ -50,8 +52,9 @@ classpath="${classpath}" debug="on" deprecation="on" - optimize="on" - /> + optimize="on" > + + diff --git a/src/main/org/apache/tools/ant/Project.java b/src/main/org/apache/tools/ant/Project.java index 39b13268d..8da398bbb 100644 --- a/src/main/org/apache/tools/ant/Project.java +++ b/src/main/org/apache/tools/ant/Project.java @@ -98,6 +98,7 @@ public class Project { private Hashtable properties = new Hashtable(); private Hashtable userProperties = new Hashtable(); + private Hashtable references = new Hashtable(); private String defaultTarget; private Hashtable taskClassDefinitions = new Hashtable(); private Hashtable targets = new Hashtable(); @@ -773,4 +774,12 @@ public class Project { } while(!c.equals(end)); return new BuildException(new String(sb)); } + + public void addReference(String name, Object value) { + references.put(name,value); + } + + public Hashtable getReferences() { + return references; + } } diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index 0e5abd864..52df93c34 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -137,6 +137,7 @@ public class ProjectHelper { String targetName = element.getAttribute("name"); String targetDep = element.getAttribute("depends"); String targetCond = element.getAttribute("if"); + String targetId = element.getAttribute("id"); // all targets must have a name if (targetName.equals("")) { @@ -149,6 +150,9 @@ public class ProjectHelper { target.setCondition(targetCond); project.addTarget(targetName, target); + if (targetId != null && !targetId.equals("")) + project.addReference(targetId,target); + // take care of dependencies if (targetDep.length() > 0) { @@ -214,6 +218,23 @@ public class ProjectHelper { // right now, all we are interested in is element nodes // not quite sure what to do with others except drop 'em + if (node.getNodeType() == Node.TEXT_NODE) { + String text = ((Text)node).getData(); + try { + Method addProp = targetClass.getMethod( + "addText", new Class[]{"".getClass()}); + Object child = addProp.invoke(target, new Object[] {text}); + } catch (NoSuchMethodException nsme) { + if (text.trim().length() > 0) + throw new BuildException(targetClass + + " does not support nested text elements"); + } catch (InvocationTargetException ite) { + throw new BuildException(ite.getMessage()); + } catch (IllegalAccessException iae) { + throw new BuildException(iae.getMessage()); + } + } + if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element)node; String propType = element.getTagName(); @@ -299,6 +320,11 @@ public class ProjectHelper { Method setMethod = (Method)propertySetters.get(attr.getName()); if (setMethod == null) { + if (attr.getName().equals("id")) { + project.addReference(attr.getValue(), target); + continue; + } + String msg = "Configuration property \"" + attr.getName() + "\" does not have a setMethod in " + target.getClass(); throw new BuildException(msg); diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties index 055feb169..4adf5085d 100644 --- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties +++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties @@ -27,6 +27,9 @@ available=org.apache.tools.ant.taskdefs.Available filter=org.apache.tools.ant.taskdefs.Filter fixcrlf=org.apache.tools.ant.taskdefs.FixCRLF +# optional tasks +script=org.apache.tools.ant.taskdefs.optional.Script + # deprecated ant tasks (kept for back compatibility) javadoc2=org.apache.tools.ant.taskdefs.Javadoc keysubst=org.apache.tools.ant.taskdefs.KeySubst diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/Script.java b/src/main/org/apache/tools/ant/taskdefs/optional/Script.java new file mode 100644 index 000000000..e99100b67 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/optional/Script.java @@ -0,0 +1,148 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 1999 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.tools.ant.taskdefs.optional; + +import org.apache.tools.ant.*; +import java.io.*; +import java.util.*; +import com.ibm.bsf.*; + +/** + * Execute a script + * + * @author Sam Ruby rubys@us.ibm.com + */ +public class Script extends Task { + private String language; + private String script = ""; + + /** + * Do the work. + * + * @exception BuildException if someting goes wrong with the build + */ + public void execute() throws BuildException { + BSFManager manager = new BSFManager (); + try { + // add id references () + Hashtable references = project.getReferences(); + for (Enumeration e = references.keys() ; e.hasMoreElements() ;) { + String key = (String)e.nextElement(); + Object value = references.get(key); + manager.declareBean(key, value, value.getClass()); + } + + // add properties () + Hashtable properties = project.getProperties(); + for (Enumeration e = properties.keys() ; e.hasMoreElements() ;) { + String key = (String)e.nextElement(); + if (!references.contains(key)) { + Object value = properties.get(key); + manager.declareBean(key, value, value.getClass()); + } + } + + // execute the script + manager.exec(language, "", 0, 0, script); + } catch (BSFException be) { + Exception e = be; + Throwable te = be.getTargetException(); + if (te != null && te instanceof Exception) e = (Exception) te; + throw new BuildException(e); + } + } + + /** + * Defines the language (required). + * + * @param msg Sets the value for the script variable. + */ + public void setLanguage(String language) { + this.language = language; + } + + /** + * Load the script from an external file + * + * @param msg Sets the value for the script variable. + */ + public void setSrc(String fileName) { + File file = new File(fileName); + if (!file.exists()) + throw new BuildException("file " + fileName + " not found."); + + int count = (int)file.length(); + byte data[] = new byte[count]; + + try { + FileInputStream inStream = new FileInputStream(file); + inStream.read(data); + inStream.close(); + } catch (IOException e) { + throw new BuildException(e); + } + + script += new String(data); + } + + /** + * Defines the script. + * + * @param msg Sets the value for the script variable. + */ + public void addText(String text) { + this.script += text; + } +}