From 6dbabcb7764ee56eeb2e0806486f4f9f76c40356 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
Date: Fri, 16 Apr 2004 08:36:01 +0000
Subject: [PATCH] Add nested mappers to xslt, PR: 11249
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276322 13f79535-47bb-0310-9956-ffa450edef68
---
WHATSNEW | 2 +
docs/manual/CoreTasks/style.html | 20 +++++-
src/etc/testcases/taskdefs/style/build.xml | 15 ++++
.../tools/ant/taskdefs/XSLTProcess.java | 69 +++++++++++++++++--
.../apache/tools/ant/taskdefs/StyleTest.java | 14 ++++
5 files changed, 113 insertions(+), 7 deletions(-)
diff --git a/WHATSNEW b/WHATSNEW
index 5bc3012be..f3032b4c8 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -141,6 +141,8 @@ Other changes:
* now also captures stderr output. Bugzilla Report 28349.
+* now supports a nested . Bugzilla Report 11249.
+
Changes from Ant 1.6.0 to Ant 1.6.1
=============================================
diff --git a/docs/manual/CoreTasks/style.html b/docs/manual/CoreTasks/style.html
index 0cf2a39d1..c260f34c8 100644
--- a/docs/manual/CoreTasks/style.html
+++ b/docs/manual/CoreTasks/style.html
@@ -56,7 +56,8 @@ element which is used to perform Entity and URI resolution
@@ -288,6 +289,16 @@ And in Saxon 7.x:
+mapper
+
+since Ant 1.6.2
+
+You can define filename transformations by using a nested mapper element. The default mapper
+used by <xslt>
removes the file extension from the
+source file and adds the extension specified via the extension
+attribute.
+
Examples
@@ -334,6 +345,13 @@ And in Saxon 7.x:
<attribute name="http://xml.apache.org/xalan/features/optimize" value="true"/>
</factory>
</xslt>
+
+ Using a mapper
+<xslt basedir="in" destdir="out"
+ style="style/apache.xsl">
+ <mapper type="glob" from="*.xml.en" to="*.html.en"/>
+</xslt>
+
Copyright © 2000-2004 The Apache Software Foundation. All rights
diff --git a/src/etc/testcases/taskdefs/style/build.xml b/src/etc/testcases/taskdefs/style/build.xml
index 813f510d3..6561d9d10 100644
--- a/src/etc/testcases/taskdefs/style/build.xml
+++ b/src/etc/testcases/taskdefs/style/build.xml
@@ -42,6 +42,21 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
index fcfc39090..392db5df2 100644
--- a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
+++ b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
@@ -25,9 +25,11 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.DynamicConfigurator;
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.XMLCatalog;
+import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
/**
@@ -132,6 +134,13 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
*/
private AntClassLoader loader = null;
+ /**
+ * Mapper to use when a set of files gets processed.
+ *
+ * @since Ant 1.6.2
+ */
+ private Mapper mapperElement = null;
+
/**
* Creates a new XSLTProcess Task.
*/
@@ -162,6 +171,21 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
reuseLoadedStylesheet = !b;
}
+ /**
+ * Defines the mapper to map source to destination files.
+ * @return a mapper to be configured
+ * @exception BuildException if more than one mapper is defined
+ * @since Ant 1.6.2
+ */
+ public Mapper createMapper() throws BuildException {
+ if (mapperElement != null) {
+ throw new BuildException("Cannot define more than one mapper",
+ getLocation());
+ }
+ mapperElement = new Mapper(getProject());
+ return mapperElement;
+ }
+
/**
* Executes the task.
*
@@ -437,7 +461,6 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
File stylesheet)
throws BuildException {
- String fileExt = targetExtension;
File outFile = null;
File inFile = null;
@@ -451,13 +474,26 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
return;
}
- int dotPos = xmlFile.lastIndexOf('.');
- if (dotPos > 0) {
- outFile = new File(destDir,
- xmlFile.substring(0, xmlFile.lastIndexOf('.')) + fileExt);
+ FileNameMapper mapper = null;
+ if (mapperElement != null) {
+ mapper = mapperElement.getImplementation();
} else {
- outFile = new File(destDir, xmlFile + fileExt);
+ mapper = new StyleMapper();
+ }
+
+ String[] outFileName = mapper.mapFileName(xmlFile);
+ if (outFileName == null || outFileName.length == 0) {
+ log("Skipping " + inFile + " it cannot get mapped to output.",
+ Project.MSG_VERBOSE);
+ return;
+ } else if (outFileName == null || outFileName.length > 1) {
+ log("Skipping " + inFile + " its mapping is ambiguos.",
+ Project.MSG_VERBOSE);
+ return;
}
+
+ outFile = new File(destDir, outFileName[0]);
+
if (force
|| inFile.lastModified() > outFile.lastModified()
|| styleSheetLastModified > outFile.lastModified()) {
@@ -922,4 +958,25 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
} // -- class Factory
+ /**
+ * Mapper implementation of the "traditional" way <xslt>
+ * mapped filenames.
+ *
+ * If the file has an extension, chop it off. Append whatever
+ * the user has specified as extension or ".html".
+ *
+ * @since Ant 1.6.2
+ */
+ private class StyleMapper implements FileNameMapper {
+ public void setFrom(String from) {}
+ public void setTo(String to) {}
+ public String[] mapFileName(String xmlFile) {
+ int dotPos = xmlFile.lastIndexOf('.');
+ if (dotPos > 0) {
+ xmlFile = xmlFile.substring(0, dotPos);
+ }
+ return new String[] {xmlFile + targetExtension};
+ }
+ }
+
}
diff --git a/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java b/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
index 220ec9683..595543f1a 100644
--- a/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
+++ b/src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java
@@ -82,6 +82,20 @@ public class StyleTest extends BuildFileTest {
}
+ public void testDefaultMapper() throws Exception {
+ assertTrue(!getProject().resolveFile("out/data.html").exists());
+ expectFileContains("testDefaultMapper",
+ "out/data.html",
+ "set='myvalue'");
+ }
+
+ public void testCustomMapper() throws Exception {
+ assertTrue(!getProject().resolveFile("out/out.xml").exists());
+ expectFileContains("testCustomMapper",
+ "out/out.xml",
+ "set='myvalue'");
+ }
+
// ************* copied from ConcatTest *************
// ------------------------------------------------------