From 575148a6633379d3f2c61d0cc838050b68929852 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
Date: Wed, 28 Sep 2005 18:46:40 +0000
Subject: [PATCH] resource collection support for xslt
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@292249 13f79535-47bb-0310-9956-ffa450edef68
---
docs/manual/CoreTasks/style.html | 24 +++++
src/etc/testcases/taskdefs/style/build.xml | 9 ++
.../tools/ant/taskdefs/XSLTProcess.java | 96 +++++++++++++++++--
.../apache/tools/ant/taskdefs/StyleTest.java | 10 +-
4 files changed, 132 insertions(+), 7 deletions(-)
diff --git a/docs/manual/CoreTasks/style.html b/docs/manual/CoreTasks/style.html
index 6ab17dbe3..9c38e3aa6 100644
--- a/docs/manual/CoreTasks/style.html
+++ b/docs/manual/CoreTasks/style.html
@@ -42,6 +42,11 @@ the files matched by those patterns because the parent directory has
been matched. If this behavior is not what you want, set the
scanincludedirectories attribute to false.
+Starting with Ant 1.7 this task supports nested resource collections
+in addition to (or instead of, depending on the useImplicitFileset
+attribute) the implicit fileset formed by this task.
+
This task supports the use of a nested <param>
element which is used to pass values
to an <xsl:param>
declaration.
This task supports the use of a nested xmlcatalog
@@ -176,8 +181,27 @@ element which is used to perform Entity and URI resolution
Default is false
. Since Ant 1.5.2.
No |
+
+ useImplicitFileset |
+ Whether the implicit fileset formed by this task
+ shall be used. If you set this to false you must use nested
+ resource collections - or the in attribute, in which case this
+ attribute has no impact anyway. Default is true .
+ Since Ant 1.7. |
+ No |
+
Parameters specified as nested elements
+
+
+
+since Ant 1.7
+
+Use resource collections to specify resources that the stylesheet
+should be applied to. Use a nested mapper and the task's destdir
+attribute to specify the output files.
+
classpath
The classpath to load the processor from can be specified via a
nested <classpath>
, as well - that is, a
diff --git a/src/etc/testcases/taskdefs/style/build.xml b/src/etc/testcases/taskdefs/style/build.xml
index 9cebe7b6f..5d5bb978a 100644
--- a/src/etc/testcases/taskdefs/style/build.xml
+++ b/src/etc/testcases/taskdefs/style/build.xml
@@ -57,6 +57,15 @@
+
+
+
+
+
+
+
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
index f890529b7..d01bac32c 100644
--- a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
+++ b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
@@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs;
import java.io.File;
import java.util.Enumeration;
+import java.util.Iterator;
import java.util.Vector;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
@@ -28,7 +29,11 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.Path;
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.XMLCatalog;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
@@ -140,6 +145,20 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
*/
private Mapper mapperElement = null;
+ /**
+ * Additional resource collections to process.
+ *
+ * @since Ant 1.7
+ */
+ private Union resources = new Union();
+
+ /**
+ * Whether to use the implicit fileset.
+ *
+ * @since Ant 1.7
+ */
+ private boolean useImplicitFileset = true;
+
/**
* Creates a new XSLTProcess Task.
*/
@@ -183,6 +202,17 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
mapperElement = mapper;
}
+ /**
+ * Adds a collection of resources to style in addition to the
+ * given file or the implicit fileset.
+ *
+ * @param rc the collection of resources to style
+ * @since Ant 1.7
+ */
+ public void add(ResourceCollection rc) {
+ resources.add(rc);
+ }
+
/**
* Executes the task.
*
@@ -248,11 +278,10 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
* in batch processing mode.
*/
- //-- make sure Source directory exists...
- if (destDir == null) {
- String msg = "destdir attributes must be set!";
- throw new BuildException(msg);
- }
+ //-- make sure destination directory exists...
+ checkDest();
+
+ if (useImplicitFileset) {
scanner = getDirectoryScanner(baseDir);
log("Transforming into " + destDir, Project.MSG_INFO);
@@ -272,6 +301,12 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
}
}
}
+ } else { // only resource collections, there better be some
+ if (resources.size() == 0) {
+ throw new BuildException("no resources specified");
+ }
+ }
+ processResources(stylesheet);
} finally {
if (loader != null) {
loader.resetThreadContextLoader();
@@ -283,7 +318,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
baseDir = savedBaseDir;
}
}
-
+
/**
* Set whether to check dependencies, or always generate;
* optional, default is false.
@@ -376,6 +411,18 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
this.processor = processor;
}
+ /**
+ * Whether to use the implicit fileset.
+ *
+ * Set this to false if you want explicit control with nested
+ * resource collections.
+ *
+ * @since Ant 1.7
+ */
+ public void setUseImplicitFileset(boolean b) {
+ useImplicitFileset = b;
+ }
+
/**
* Add the catalog to our internal catalog
*
@@ -451,6 +498,43 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
this.inFile = inFile;
}
+ /**
+ * Throws a BuildException if the destination directory hasn't
+ * been specified.
+ * @since Ant 1.7
+ */
+ private void checkDest() {
+ if (destDir == null) {
+ String msg = "destdir attributes must be set!";
+ throw new BuildException(msg);
+ }
+ }
+
+ /**
+ * Styles all existing resources.
+ *
+ * @since Ant 1.7
+ */
+ private void processResources(File stylesheet) {
+ Iterator iter = resources.iterator();
+ while (iter.hasNext()) {
+ Resource r = (Resource) iter.next();
+ if (!r.isExists()) {
+ continue;
+ }
+ File base = baseDir;
+ String name = r.getName();
+ if (r instanceof FileResource) {
+ FileResource f = (FileResource) r;
+ base = f.getBaseDir();
+ if (base == null) {
+ name = f.getFile().getAbsolutePath();
+ }
+ }
+ process(base, name, destDir, stylesheet);
+ }
+ }
+
/**
* Processes the given input XML file and stores the result
* in the given resultFile.
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java b/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
index b9ba7fa59..e892d1af4 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
@@ -84,8 +84,16 @@ public class StyleTest extends BuildFileTest {
public void testDefaultMapper() throws Exception {
+ testDefaultMapper("testDefaultMapper");
+ }
+
+ public void testExplicitFileset() throws Exception {
+ testDefaultMapper("testExplicitFileset");
+ }
+
+ public void testDefaultMapper(String target) throws Exception {
assertTrue(!getProject().resolveFile("out/data.html").exists());
- expectFileContains("testDefaultMapper",
+ expectFileContains(target,
"out/data.html",
"set='myvalue'");
}