Browse Source

Added an *OPTIONAL* task which will allow a script, embedded or external,

to be executed.


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267636 13f79535-47bb-0310-9956-ffa450edef68
master
Sam Ruby 25 years ago
parent
commit
e9f55d77d1
5 changed files with 191 additions and 2 deletions
  1. +5
    -2
      build.xml
  2. +9
    -0
      src/main/org/apache/tools/ant/Project.java
  3. +26
    -0
      src/main/org/apache/tools/ant/ProjectHelper.java
  4. +3
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  5. +148
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/Script.java

+ 5
- 2
build.xml View File

@@ -28,6 +28,8 @@

<property name="build.compiler" value="classic"/>

<available property="bsf.present" classname="com.ibm.bsf.BSFManager" />

<!-- Give user a chance to override without editing this file
(and without typing -D each time it compiles it -->
<property file="${user.home}/.ant.properties" />
@@ -50,8 +52,9 @@
classpath="${classpath}"
debug="on"
deprecation="on"
optimize="on"
/>
optimize="on" >
<exclude name="**/Script.java" unless="bsf.present" />
</javac>
</target>

<!-- =================================================================== -->


+ 9
- 0
src/main/org/apache/tools/ant/Project.java View File

@@ -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;
}
}

+ 26
- 0
src/main/org/apache/tools/ant/ProjectHelper.java View File

@@ -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);


+ 3
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

@@ -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

+ 148
- 0
src/main/org/apache/tools/ant/taskdefs/optional/Script.java View File

@@ -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
* <http://www.apache.org/>.
*/

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 <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
*/
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 (<task id="foo">)
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 (<property name="foo">)
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, "<ANT>", 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;
}
}

Loading…
Cancel
Save