conor 2003/07/20 02:34:21
Modified: . build.xml
src/main/org/apache/tools/ant/taskdefs/optional Script.java
src/main/org/apache/tools/ant/taskdefs/optional/script
ScriptDef.java
src/main/org/apache/tools/ant/types/optional
ScriptFilter.java
src/testcases/org/apache/tools/ant/taskdefs/optional/script
ScriptDefTest.java
Added: src/main/org/apache/tools/ant/util ScriptRunner.java
Log:
Refactor script related tasks into single ScriptRunner utility.
Revision Changes Path
1.389 +1 -0 ant/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/ant/build.xml,v
retrieving revision 1.388
retrieving revision 1.389
diff -u -w -u -r1.388 -r1.389
--- build.xml 16 Jul 2003 14:39:30 -0000 1.388
+++ build.xml 20 Jul 2003 09:34:21 -0000 1.389
@@ -233,6 +233,7 @@
<filename name="${optional.package}/Script*"/>
<filename name="${optional.package}/script/**/*"/>
<filename name="${optional.type.package}/Script*"/>
+ <filename name="${util.package}/Script*"/>
</or>
</selector>
1.23 +16 -80
ant/src/main/org/apache/tools/ant/taskdefs/optional/Script.java
Index: Script.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/Script.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -w -u -r1.22 -r1.23
--- Script.java 18 Jul 2003 12:45:57 -0000 1.22
+++ Script.java 20 Jul 2003 09:34:21 -0000 1.23
@@ -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 @@
* @author Sam Ruby <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
*/
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 @@
* @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());
-
- beans.put("project", getProject());
-
- 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());
- }
+ runner.addBeans(getProject().getProperties());
+ runner.addBeans(getProject().getUserProperties());
+ runner.addBeans(getProject().getTargets());
+ runner.addBeans(getProject().getReferences());
- // execute the script
- manager.exec(language, "<ANT>", 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.addBean("project", getProject());
+ runner.addBean("self", this);
+
+ runner.executeScript("<ANT>");
}
/**
@@ -140,7 +91,7 @@
* @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 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 @@
* @param text a component of the script text to be added.
*/
public void addText(String text) {
- this.script += text;
+ runner.addText(text);
}
}
1.5 +45 -42
ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java
Index: ScriptDef.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/script/ScriptDef.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -u -r1.4 -r1.5
--- ScriptDef.java 19 Jul 2003 11:20:20 -0000 1.4
+++ ScriptDef.java 20 Jul 2003 09:34:21 -0000 1.5
@@ -65,9 +65,9 @@
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 @@
* @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 @@
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 language the scripting language used by this script.
+ * @param attributeName the name of the attribute.
+ *
+ * @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 @@
+ "name the script");
}
- if (language == null) {
- throw new BuildException("scriptdef requires a language
attribute "
+ if (runner.getLanguage() == null) {
+ throw new BuildException("<scriptdef> requires a language
attribute "
+ "to specify the script language");
}
@@ -277,6 +272,12 @@
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 @@
* @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;
+ 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);
}
- throw new BuildException(t);
- }
+
+ /**
+ * 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);
}
}
1.6 +20 -111
ant/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java
Index: ScriptFilter.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/types/optional/ScriptFilter.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -w -u -r1.5 -r1.6
--- ScriptFilter.java 18 Jul 2003 12:45:58 -0000 1.5
+++ ScriptFilter.java 20 Jul 2003 09:34:21 -0000 1.6
@@ -55,13 +55,8 @@
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 @@
* 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;
- }
-
-
- /**
- * 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));
+ runner.setLanguage(language);
}
- 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 @@
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 String filter(String token) {
init();
setToken(token);
- try {
- manager.exec(language, "<ANT>", 0, 0, script);
+ runner.executeScript("<ANT-Filter>");
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);
- }
}
+
/**
* 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 @@
* @param text a component of the script text to be added.
*/
public void addText(String text) {
- this.script += text;
+ runner.addText(text);
}
}
1.1 ant/src/main/org/apache/tools/ant/util/ScriptRunner.java
Index: ScriptRunner.java
===================================================================
/*
* 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 [EMAIL PROTECTED]
*
* 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.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 <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
* @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;
}
}
1.4 +21 -21
ant/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
Index: ScriptDefTest.java
===================================================================
RCS file:
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -w -u -r1.3 -r1.4
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]