diff --git a/WHATSNEW b/WHATSNEW
index c3759f428..36bbaa9d1 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -4,6 +4,11 @@ Changes from Ant 1.7.0 TO current SVN version
Changes that could break older environments:
-------------------------------------------
+* String resources only have properties single expanded. If you relied on
+ type="antlib:example.org:newtype"
No
+
+
+
any resource or resource collection
+ Since Ant1.7.1, this task can load scripts
+ from any resource supplied as a nested element. when
+ No
+ classpath
@@ -198,6 +205,7 @@ more information on writing scripts, please refer to the
for using this nested element.
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 02360512a..88dbf2953 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
@@ -23,6 +23,8 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectHelper;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.taskdefs.DefBase;
import java.util.Map;
@@ -250,25 +252,34 @@ public class ScriptDef extends DefBase {
}
// find the script repository - it is stored in the project
+ Map scriptRepository = lookupScriptRepository();
+ name = ProjectHelper.genComponentName(getURI(), name);
+ scriptRepository.put(name, this);
+ AntTypeDefinition def = new AntTypeDefinition();
+ def.setName(name);
+ def.setClass(ScriptDefBase.class);
+ ComponentHelper.getComponentHelper(
+ getProject()).addDataTypeDefinition(def);
+ }
+
+ /**
+ * Find or create the script repository - it is stored in the project.
+ * This method is synchronized on the project under {@link MagicNames#SCRIPT_REPOSITORY}
+ * @return the current script repository registered as a refrence.
+ */
+ private Map lookupScriptRepository() {
Map scriptRepository = null;
Project p = getProject();
synchronized (p) {
scriptRepository =
- (Map) p.getReference(MagicNames.SCRIPT_REPOSITORY);
+ (Map) p.getReference(MagicNames.SCRIPT_REPOSITORY);
if (scriptRepository == null) {
scriptRepository = new HashMap();
p.addReference(MagicNames.SCRIPT_REPOSITORY,
- scriptRepository);
+ scriptRepository);
}
}
-
- name = ProjectHelper.genComponentName(getURI(), name);
- scriptRepository.put(name, this);
- AntTypeDefinition def = new AntTypeDefinition();
- def.setName(name);
- def.setClass(ScriptDefBase.class);
- ComponentHelper.getComponentHelper(
- getProject()).addDataTypeDefinition(def);
+ return scriptRepository;
}
/**
@@ -382,5 +393,14 @@ public class ScriptDef extends DefBase {
public void addText(String text) {
helper.addText(text);
}
+
+ /**
+ * Add any source resource.
+ * @since Ant1.7.1
+ * @param resource source of script
+ */
+ public void add(ResourceCollection resource) {
+ helper.add(resource);
+ }
}
diff --git a/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java b/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
index d92acf710..5838f5757 100644
--- a/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
+++ b/src/main/org/apache/tools/ant/util/ScriptRunnerBase.java
@@ -21,10 +21,16 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.FileNotFoundException;
+import java.io.InputStreamReader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.Project;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.ResourceCollection;
import java.util.Map;
import java.util.HashMap;
@@ -186,27 +192,76 @@ public abstract class ScriptRunnerBase {
* @param file the file containing the script source.
*/
public void setSrc(File file) {
+ String filename = file.getPath();
if (!file.exists()) {
- throw new BuildException("file " + file.getPath() + " not found.");
+ throw new BuildException("file " + filename + " not found.");
}
+ try {
+ readSource(new FileReader(file), filename);
+ } catch (FileNotFoundException e) {
+ //this can only happen if the file got deleted a short moment ago
+ throw new BuildException("file " + filename + " not found.");
+ }
+ }
+
+ /**
+ * Read some source in from the given reader
+ * @param reader the reader; this is closed afterwards.
+ * @param name the name to use in error messages
+ */
+ private void readSource(Reader reader, String name) {
BufferedReader in = null;
try {
- in = new BufferedReader(new FileReader(file));
+ in = new BufferedReader(reader);
script += FileUtils.readFully(in);
} catch (IOException ex) {
- throw new BuildException(ex);
+ throw new BuildException("Failed to read "+ name,ex);
} finally {
FileUtils.close(in);
}
}
+
+ /**
+ * Add a resource to the source list.
+ * @since Ant 1.7.1
+ * @param sourceResource the resource to load
+ * @throws BuildException if the resource cannot be read
+ */
+ public void loadResource(Resource sourceResource) {
+ String name = sourceResource.toLongString();
+ InputStream in = null;
+ try {
+ in = sourceResource.getInputStream();
+ } catch (IOException e) {
+ throw new BuildException("Failed to open "+name,e);
+ } catch (UnsupportedOperationException e) {
+ throw new BuildException("Failed to open " + name+ " -it is not readable",e);
+ }
+ readSource(new InputStreamReader(in), name);
+ }
+
+ /**
+ * Add all resources in a resource collection to the source list.
+ * @since Ant 1.7.1
+ * @param collection the resource to load
+ * @throws BuildException if a resource cannot be read
+ */
+ public void loadResources(ResourceCollection collection) {
+ Iterator resources = collection.iterator();
+ while (resources.hasNext()) {
+ Resource resource = (Resource) resources.next();
+ loadResource(resource);
+ }
+ }
+
/**
- * Set the script text.
+ * Set the script text. Properties in the text are not expanded!
*
* @param text a component of the script text to be added.
*/
public void addText(String text) {
- this.script += text;
+ script += text;
}
/**
diff --git a/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java b/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
index b481513c5..cb5f8e690 100644
--- a/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
+++ b/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java
@@ -21,6 +21,9 @@ import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.types.Path;
import java.io.File;
import org.apache.tools.ant.types.Reference;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.ResourceCollection;
+import org.apache.tools.ant.types.resources.Union;
/**
@@ -36,6 +39,7 @@ public class ScriptRunnerHelper {
private boolean setBeans = true;
private ProjectComponent projectComponent;
private ClassLoader scriptLoader = null;
+ private Union resources=new Union();
/**
* Set the project component associated with this helper.
@@ -57,6 +61,9 @@ public class ScriptRunnerHelper {
if (text != null) {
runner.addText(text);
}
+ if (resources !=null) {
+ runner.loadResources(resources);
+ }
if (setBeans) {
runner.bindToComponent(projectComponent);
} else {
@@ -188,4 +195,14 @@ public class ScriptRunnerHelper {
projectComponent.getProject()).createRunner(
manager, language, generateClassLoader());
}
+
+ /**
+ * Add any source resource.
+ *
+ * @param resource source of script
+ * @since Ant1.7.1
+ */
+ public void add(ResourceCollection resource) {
+ resources.add(resource);
+ }
}
diff --git a/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml b/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
new file mode 100644
index 000000000..9ec4575a3
--- /dev/null
+++ b/src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml
@@ -0,0 +1,99 @@
+