diff --git a/build.xml b/build.xml
index 65d0ed17a..598fd0b22 100644
--- a/build.xml
+++ b/build.xml
@@ -233,6 +233,7 @@
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/Script.java b/src/main/org/apache/tools/ant/taskdefs/optional/Script.java
index a9ea464ae..3fdd4de09 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/Script.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/Script.java
@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
+ * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -54,14 +54,9 @@
package org.apache.tools.ant.taskdefs.optional;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import org.apache.bsf.BSFException;
-import org.apache.bsf.BSFManager;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.util.ScriptRunner;
/**
* Executes a script.
@@ -70,29 +65,8 @@ import org.apache.tools.ant.Task;
* @author Sam Ruby rubys@us.ibm.com
*/
public class Script extends Task {
- private String language;
- private String script = "";
- private Hashtable beans = new Hashtable();
-
- /**
- * Add a list of named objects to the list to be exported to the script
- */
- private void addBeans(Hashtable dictionary) {
- for (Enumeration e = dictionary.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
-
- boolean isValid = key.length() > 0
- && Character.isJavaIdentifierStart(key.charAt(0));
-
- for (int i = 1; isValid && i < key.length(); i++) {
- isValid = Character.isJavaIdentifierPart(key.charAt(i));
- }
-
- if (isValid) {
- beans.put(key, dictionary.get(key));
- }
- }
- }
+ /** Used to run the script */
+ private ScriptRunner runner = new ScriptRunner();
/**
* Do the work.
@@ -100,38 +74,15 @@ public class Script extends Task {
* @exception BuildException if someting goes wrong with the build
*/
public void execute() throws BuildException {
- try {
- addBeans(getProject().getProperties());
- addBeans(getProject().getUserProperties());
- addBeans(getProject().getTargets());
- addBeans(getProject().getReferences());
+ runner.addBeans(getProject().getProperties());
+ runner.addBeans(getProject().getUserProperties());
+ runner.addBeans(getProject().getTargets());
+ runner.addBeans(getProject().getReferences());
- beans.put("project", getProject());
+ runner.addBean("project", getProject());
+ runner.addBean("self", this);
- beans.put("self", this);
-
- BSFManager manager = new BSFManager ();
-
- for (Enumeration e = beans.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
- Object value = beans.get(key);
- manager.declareBean(key, value, value.getClass());
- }
-
- // execute the script
- manager.exec(language, "", 0, 0, script);
- } catch (BSFException be) {
- Throwable t = be;
- Throwable te = be.getTargetException();
- if (te != null) {
- if (te instanceof BuildException) {
- throw (BuildException) te;
- } else {
- t = te;
- }
- }
- throw new BuildException(t);
- }
+ runner.executeScript("");
}
/**
@@ -140,7 +91,7 @@ public class Script extends Task {
* @param language the scripting language name for the script.
*/
public void setLanguage(String language) {
- this.language = language;
+ runner.setLanguage(language);
}
/**
@@ -150,22 +101,7 @@ public class Script extends Task {
*/
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);
+ runner.setSrc(file);
}
/**
@@ -174,6 +110,6 @@ public class Script extends Task {
* @param text a component of the script text to be added.
*/
public void addText(String text) {
- this.script += text;
+ runner.addText(text);
}
}
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
index c74b78316..29cfe32ff 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
@@ -65,9 +65,9 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
+import java.io.File;
-import org.apache.bsf.BSFException;
-import org.apache.bsf.BSFManager;
+import org.apache.tools.ant.util.ScriptRunner;
/**
* Define a task using a script
@@ -76,15 +76,12 @@ import org.apache.bsf.BSFManager;
* @since Ant 1.6
*/
public class ScriptDef extends Task {
+ /** Used to run the script */
+ private ScriptRunner runner = new ScriptRunner();
+
/** the name by which this script will be activated */
private String name;
- /** the scripting language used by the script */
- private String language;
-
- /** the script itself */
- private String script = "";
-
/** Attributes definitions of this script */
private List attributes = new ArrayList();
@@ -107,17 +104,15 @@ public class ScriptDef extends Task {
this.name = name;
}
- public boolean isAttributeSupported(String attributeName) {
- return attributeSet.contains(attributeName);
- }
-
/**
- * Set the scripting language used by this script
+ * Indicates whether the task supports a given attribute name
+ *
+ * @param attributeName the name of the attribute.
*
- * @param language the scripting language used by this script.
+ * @return true if the attribute is supported by the script.
*/
- public void setLanguage(String language) {
- this.language = language;
+ public boolean isAttributeSupported(String attributeName) {
+ return attributeSet.contains(attributeName);
}
/**
@@ -211,8 +206,8 @@ public class ScriptDef extends Task {
+ "name the script");
}
- if (language == null) {
- throw new BuildException("scriptdef requires a language attribute "
+ if (runner.getLanguage() == null) {
+ throw new BuildException(" requires a language attribute "
+ "to specify the script language");
}
@@ -277,6 +272,12 @@ public class ScriptDef extends Task {
project.addTaskDefinition(name, ScriptDefBase.class);
}
+ /**
+ * Create a nested element to be configured.
+ *
+ * @param elementName the name of the nested element.
+ * @return object representing the element name.
+ */
public Object createNestedElement(String elementName) {
NestedElement definition
= (NestedElement) nestedElementMap.get(elementName);
@@ -336,36 +337,38 @@ public class ScriptDef extends Task {
* @param elements a list of nested element values.
*/
public void executeScript(Map attributes, Map elements) {
- try {
- BSFManager manager = new BSFManager();
- // execute the script
- manager.declareBean("attributes", attributes,
- attributes.getClass());
- manager.declareBean("elements", elements,
- elements.getClass());
- manager.declareBean("project", getProject(), Project.class);
- manager.exec(language, "scriptdef <" + name + ">", 0, 0, script);
- } catch (BSFException e) {
- Throwable t = e;
- Throwable te = e.getTargetException();
- if (te != null) {
- if (te instanceof BuildException) {
- throw (BuildException) te;
- } else {
- t = te;
- }
- }
- throw new BuildException(t);
- }
+ runner.addBean("attributes", attributes);
+ runner.addBean("elements", elements);
+ runner.addBean("project", getProject());
+ runner.executeScript("scriptdef <" + name + ">");
+ }
+
+
+ /**
+ * Defines the language (required).
+ *
+ * @param language the scripting language name for the script.
+ */
+ public void setLanguage(String language) {
+ runner.setLanguage(language);
+ }
+
+ /**
+ * Load the script from an external file ; optional.
+ *
+ * @param file the file containing the script source.
+ */
+ public void setSrc(File file) {
+ runner.setSrc(file);
}
/**
- * Ass the scipt text.
+ * Set the script text.
*
- * @param text appended to the script text.
+ * @param text a component of the script text to be added.
*/
public void addText(String text) {
- this.script += text;
+ runner.addText(text);
}
}
diff --git a/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java b/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java
index fbc2e6c7f..7ee44675c 100644
--- a/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java
+++ b/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java
@@ -55,13 +55,8 @@ package org.apache.tools.ant.types.optional;
import org.apache.tools.ant.filters.TokenFilter;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import org.apache.bsf.BSFException;
-import org.apache.bsf.BSFManager;
import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.util.ScriptRunner;
/**
@@ -74,58 +69,28 @@ import org.apache.tools.ant.BuildException;
* set self.token in the reply.
*
* @author Not Specified.
+ *
+ * @since Ant 1.6
*/
public class ScriptFilter extends TokenFilter.ChainableReaderFilter {
- /** The language - attribute of element */
- private String language;
- /** The script - inline text or external file */
- private String script = "";
- /** The beans - see ScriptTask */
- private Hashtable beans = new Hashtable();
/** Has this object been initialized ? */
private boolean initialized = false;
- /** the BSF manager */
- private BSFManager manager;
/** the token used by the script */
private String token;
+ private ScriptRunner runner = new ScriptRunner();
+
/**
* Defines the language (required).
*
* @param language the scripting language name for the script.
*/
public void setLanguage(String language) {
- this.language = language;
+ runner.setLanguage(language);
}
-
- /**
- * Add a list of named objects to the list to be exported to the script
- * CAP from taskdefs.optional.Script
- */
- private void addBeans(Hashtable dictionary) {
- for (Enumeration e = dictionary.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
-
- boolean isValid = key.length() > 0
- && Character.isJavaIdentifierStart(key.charAt(0));
-
- for (int i = 1; isValid && i < key.length(); i++) {
- isValid = Character.isJavaIdentifierPart(key.charAt(i));
- }
-
- try {
- if (isValid) {
- beans.put(key, dictionary.get(key));
- }
- } catch (Throwable t) {
- throw new BuildException(t);
- //System.err.println("What the helll");
- }
- }
- }
/**
- * Initialize, mostly CAP from taskdefs.option.Script#execute()
+ * Initialize.
*
* @exception BuildException if someting goes wrong
*/
@@ -134,42 +99,14 @@ public class ScriptFilter extends TokenFilter.ChainableReaderFilter {
return;
}
initialized = true;
- if (language == null) {
- throw new BuildException(
- "scriptfilter: language is not defined");
- }
-
- try {
- addBeans(getProject().getProperties());
- addBeans(getProject().getUserProperties());
- addBeans(getProject().getTargets());
- addBeans(getProject().getReferences());
- beans.put("project", getProject());
-
- beans.put("self", this);
-
- manager = new BSFManager ();
-
- for (Enumeration e = beans.keys(); e.hasMoreElements();) {
- String key = (String) e.nextElement();
- Object value = beans.get(key);
- manager.declareBean(key, value, value.getClass());
- }
-
- } catch (BSFException e) {
- Throwable t = e;
- Throwable te = e.getTargetException();
- if (te != null) {
- if (te instanceof BuildException) {
- throw (BuildException) te;
- } else {
- t = te;
- }
- }
- throw new BuildException(t);
- }
+ runner.addBeans(getProject().getProperties());
+ runner.addBeans(getProject().getUserProperties());
+ runner.addBeans(getProject().getTargets());
+ runner.addBeans(getProject().getReferences());
+ runner.addBean("project", getProject());
+ runner.addBean("self", this);
}
/**
@@ -201,45 +138,17 @@ public class ScriptFilter extends TokenFilter.ChainableReaderFilter {
public String filter(String token) {
init();
setToken(token);
- try {
- manager.exec(language, "", 0, 0, script);
- return getToken();
- } catch (BSFException be) {
- Throwable t = be;
- Throwable te = be.getTargetException();
- if (te != null) {
- if (te instanceof BuildException) {
- throw (BuildException) te;
- } else {
- t = te;
- }
- }
- throw new BuildException(t);
- }
+ runner.executeScript("");
+ return getToken();
}
+
/**
* Load the script from an external file ; optional.
*
- * @param fileName the name of the file containing the script source.
+ * @param file the file containing the script source.
*/
- 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);
+ public void setSrc(File file) {
+ runner.setSrc(file);
}
/**
@@ -248,6 +157,6 @@ public class ScriptFilter extends TokenFilter.ChainableReaderFilter {
* @param text a component of the script text to be added.
*/
public void addText(String text) {
- this.script += text;
+ runner.addText(text);
}
}
diff --git a/src/main/org/apache/tools/ant/util/ScriptRunner.java b/src/main/org/apache/tools/ant/util/ScriptRunner.java
new file mode 100644
index 000000000..4e00064b3
--- /dev/null
+++ b/src/main/org/apache/tools/ant/util/ScriptRunner.java
@@ -0,0 +1,203 @@
+/*
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2003 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 "Ant" 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.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import org.apache.bsf.BSFException;
+import org.apache.bsf.BSFManager;
+import org.apache.tools.ant.BuildException;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * This class is used to run BSF scripts
+ *
+ * @author Sam Ruby rubys@us.ibm.com
+ * @author Conor MacNeill
+ */
+public class ScriptRunner {
+ /** Script language */
+ private String language;
+
+ /** Script content */
+ private String script = "";
+
+ /** Beans to be provided to the script */
+ private Map beans = new HashMap();
+
+
+ /**
+ * Add a list of named objects to the list to be exported to the script
+ *
+ * @param dictionary a map of objects to be placed into the script context
+ * indexed by String names.
+ */
+ public void addBeans(Map dictionary) {
+ for (Iterator i = dictionary.keySet().iterator(); i.hasNext();) {
+ String key = (String) i.next();
+ addBean(key, dictionary.get(key));
+ }
+ }
+
+ /**
+ * Add a single object into the script context.
+ *
+ * @param key the name in the context this object is to stored under.
+ * @param bean the object to be stored in the script context.
+ */
+ public void addBean(String key, Object bean) {
+ boolean isValid = key.length() > 0
+ && Character.isJavaIdentifierStart(key.charAt(0));
+
+ for (int i = 1; isValid && i < key.length(); i++) {
+ isValid = Character.isJavaIdentifierPart(key.charAt(i));
+ }
+
+ if (isValid) {
+ beans.put(key, bean);
+ }
+ }
+
+ /**
+ * Do the work.
+ *
+ * @param execName the name that will be passed to BSF for this script
+ * execution.
+ *
+ * @exception BuildException if someting goes wrong exectuing the script.
+ */
+ public void executeScript(String execName) throws BuildException {
+ if (language == null) {
+ throw new BuildException("script language must be specified");
+ }
+
+ try {
+ BSFManager manager = new BSFManager ();
+
+ for (Iterator i = beans.keySet().iterator(); i.hasNext();) {
+ String key = (String) i.next();
+ Object value = beans.get(key);
+ manager.declareBean(key, value, value.getClass());
+ }
+
+ // execute the script
+ manager.exec(language, execName, 0, 0, script);
+ } catch (BSFException be) {
+ Throwable t = be;
+ Throwable te = be.getTargetException();
+ if (te != null) {
+ if (te instanceof BuildException) {
+ throw (BuildException) te;
+ } else {
+ t = te;
+ }
+ }
+ throw new BuildException(t);
+ }
+ }
+
+ /**
+ * Defines the language (required).
+ *
+ * @param language the scripting language name for the script.
+ */
+ public void setLanguage(String language) {
+ this.language = language;
+ }
+
+ /**
+ * Get the script language
+ *
+ * @return the script language
+ */
+ public String getLanguage() {
+ return language;
+ }
+
+ /**
+ * Load the script from an external file ; optional.
+ *
+ * @param file the file containing the script source.
+ */
+ public void setSrc(File file) {
+ if (!file.exists()) {
+ throw new BuildException("file " + file.getPath() + " 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);
+ }
+
+ /**
+ * Set the script text.
+ *
+ * @param text a component of the script text to be added.
+ */
+ public void addText(String text) {
+ this.script += text;
+ }
+}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
index 659ed80a0..0451a481b 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
@@ -76,7 +76,7 @@ public class ScriptDefTest extends BuildFileTest {
public void setUp() {
configureProject("src/etc/testcases/taskdefs/optional/script/scriptdef.xml");
}
-
+
public void testSimple() {
executeTarget("simple");
// get the fileset and its basedir
@@ -84,25 +84,25 @@ public class ScriptDefTest extends BuildFileTest {
FileSet fileset = (FileSet) project.getReference("testfileset");
File baseDir = fileset.getDir(project);
String log = getLog();
- assertTrue("Expecting attribute value printed",
+ assertTrue("Expecting attribute value printed",
log.indexOf("Attribute attr1 = test") != -1);
-
- assertTrue("Expecting nested element value printed",
+
+ assertTrue("Expecting nested element value printed",
log.indexOf("Fileset basedir = " + baseDir.getAbsolutePath()) != -1);
}
public void testNoLang() {
- expectBuildExceptionContaining("nolang",
- "Absence of language attribute not detected",
+ expectBuildExceptionContaining("nolang",
+ "Absence of language attribute not detected",
"requires a language attribute");
}
public void testNoName() {
- expectBuildExceptionContaining("noname",
- "Absence of name attribute not detected",
+ expectBuildExceptionContaining("noname",
+ "Absence of name attribute not detected",
"scriptdef requires a name attribute");
}
-
+
public void testNestedByClassName() {
executeTarget("nestedbyclassname");
// get the fileset and its basedir
@@ -110,35 +110,35 @@ public class ScriptDefTest extends BuildFileTest {
FileSet fileset = (FileSet) project.getReference("testfileset");
File baseDir = fileset.getDir(project);
String log = getLog();
- assertTrue("Expecting attribute value to be printed",
+ assertTrue("Expecting attribute value to be printed",
log.indexOf("Attribute attr1 = test") != -1);
-
- assertTrue("Expecting nested element value to be printed",
+
+ assertTrue("Expecting nested element value to be printed",
log.indexOf("Fileset basedir = " + baseDir.getAbsolutePath()) != -1);
}
public void testNoElement() {
expectOutput("noelement", "Attribute attr1 = test");
}
-
+
public void testException() {
- expectBuildExceptionContaining("exception",
- "Should have thrown an exception in the script",
+ expectBuildExceptionContaining("exception",
+ "Should have thrown an exception in the script",
"TypeError");
}
-
+
public void testDoubleDef() {
executeTarget("doubledef");
String log = getLog();
- assertTrue("Task1 did not execute",
+ assertTrue("Task1 did not execute",
log.indexOf("Task1") != -1);
- assertTrue("Task2 did not execute",
+ assertTrue("Task2 did not execute",
log.indexOf("Task2") != -1);
}
public void testDoubleAttribute() {
- expectBuildExceptionContaining("doubleAttributeDef",
- "Should have detected duplicate attribute definition",
+ expectBuildExceptionContaining("doubleAttributeDef",
+ "Should have detected duplicate attribute definition",
"attr1 attribute more than once");
}
@@ -147,7 +147,7 @@ public class ScriptDefTest extends BuildFileTest {
// get the fileset and its basedir
Project project = getProject();
String log = getLog();
- assertTrue("Expecting property in attribute value replaced",
+ assertTrue("Expecting property in attribute value replaced",
log.indexOf("Attribute value = test") != -1);
}