git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276322 13f79535-47bb-0310-9956-ffa450edef68master
@@ -141,6 +141,8 @@ Other changes: | |||||
* <sshexec> now also captures stderr output. Bugzilla Report 28349. | * <sshexec> now also captures stderr output. Bugzilla Report 28349. | ||||
* <xslt> now supports a nested <mapper>. Bugzilla Report 11249. | |||||
Changes from Ant 1.6.0 to Ant 1.6.1 | Changes from Ant 1.6.0 to Ant 1.6.1 | ||||
============================================= | ============================================= | ||||
@@ -56,7 +56,8 @@ element which is used to perform Entity and URI resolution</p> | |||||
<tr> | <tr> | ||||
<td valign="top">extension</td> | <td valign="top">extension</td> | ||||
<td valign="top">desired file extension to be used for the targets. If not | <td valign="top">desired file extension to be used for the targets. If not | ||||
specified, the default is ".html".</td> | |||||
specified, the default is ".html". Will be ignored if | |||||
a nested <mapper> has been specified.</td> | |||||
<td align="center" valign="top">No</td> | <td align="center" valign="top">No</td> | ||||
</tr> | </tr> | ||||
<tr> | <tr> | ||||
@@ -288,6 +289,16 @@ And in Saxon 7.x: | |||||
</table> | </table> | ||||
</blockquote> | </blockquote> | ||||
<h4>mapper</h4> | |||||
<p><em>since Ant 1.6.2</em></p> | |||||
<p>You can define filename transformations by using a nested <a | |||||
href="../CoreTypes/mapper.html">mapper</a> element. The default mapper | |||||
used by <code><xslt></code> removes the file extension from the | |||||
source file and adds the extension specified via the extension | |||||
attribute.</p> | |||||
<h3>Examples</h3> | <h3>Examples</h3> | ||||
<blockquote> | <blockquote> | ||||
<pre> | <pre> | ||||
@@ -334,6 +345,13 @@ And in Saxon 7.x: | |||||
<attribute name="http://xml.apache.org/xalan/features/optimize" value="true"/> | <attribute name="http://xml.apache.org/xalan/features/optimize" value="true"/> | ||||
</factory> | </factory> | ||||
</xslt></pre> | </xslt></pre> | ||||
<h4>Using a mapper</h4> | |||||
<pre><xslt basedir="in" destdir="out" | |||||
style="style/apache.xsl"> | |||||
<mapper type="glob" from="*.xml.en" to="*.html.en"/> | |||||
</xslt></pre> | |||||
</blockquote> | </blockquote> | ||||
<hr> | <hr> | ||||
<p align="center">Copyright © 2000-2004 The Apache Software Foundation. All rights | <p align="center">Copyright © 2000-2004 The Apache Software Foundation. All rights | ||||
@@ -42,6 +42,21 @@ | |||||
</style> | </style> | ||||
</target> | </target> | ||||
<target name="testDefaultMapper"> | |||||
<property name="value" value="myvalue"/> | |||||
<style style="printParams.xsl" destDir="${out.dir}" basedir="."> | |||||
<param name="set" expression="${value}"/> | |||||
</style> | |||||
</target> | |||||
<target name="testCustomMapper"> | |||||
<property name="value" value="myvalue"/> | |||||
<style style="printParams.xsl" destDir="${out.dir}" basedir="."> | |||||
<param name="set" expression="${value}"/> | |||||
<mapper type="glob" from="data.*" to="out.*"/> | |||||
</style> | |||||
</target> | |||||
<target name="testNewerStylesheet"> | <target name="testNewerStylesheet"> | ||||
<antcall target="copyXsl"> | <antcall target="copyXsl"> | ||||
<param name="xsl.value" value="old-value"/> | <param name="xsl.value" value="old-value"/> | ||||
@@ -25,9 +25,11 @@ import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.DirectoryScanner; | import org.apache.tools.ant.DirectoryScanner; | ||||
import org.apache.tools.ant.DynamicConfigurator; | import org.apache.tools.ant.DynamicConfigurator; | ||||
import org.apache.tools.ant.Project; | 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.Path; | ||||
import org.apache.tools.ant.types.Reference; | import org.apache.tools.ant.types.Reference; | ||||
import org.apache.tools.ant.types.XMLCatalog; | import org.apache.tools.ant.types.XMLCatalog; | ||||
import org.apache.tools.ant.util.FileNameMapper; | |||||
import org.apache.tools.ant.util.FileUtils; | import org.apache.tools.ant.util.FileUtils; | ||||
/** | /** | ||||
@@ -132,6 +134,13 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { | |||||
*/ | */ | ||||
private AntClassLoader loader = null; | 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. | * Creates a new XSLTProcess Task. | ||||
*/ | */ | ||||
@@ -162,6 +171,21 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { | |||||
reuseLoadedStylesheet = !b; | 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. | * Executes the task. | ||||
* | * | ||||
@@ -437,7 +461,6 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { | |||||
File stylesheet) | File stylesheet) | ||||
throws BuildException { | throws BuildException { | ||||
String fileExt = targetExtension; | |||||
File outFile = null; | File outFile = null; | ||||
File inFile = null; | File inFile = null; | ||||
@@ -451,13 +474,26 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { | |||||
return; | 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 { | } 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 | if (force | ||||
|| inFile.lastModified() > outFile.lastModified() | || inFile.lastModified() > outFile.lastModified() | ||||
|| styleSheetLastModified > outFile.lastModified()) { | || styleSheetLastModified > outFile.lastModified()) { | ||||
@@ -922,4 +958,25 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { | |||||
} // -- class Factory | } // -- class Factory | ||||
/** | |||||
* Mapper implementation of the "traditional" way <xslt> | |||||
* mapped filenames. | |||||
* | |||||
* <p>If the file has an extension, chop it off. Append whatever | |||||
* the user has specified as extension or ".html".</p> | |||||
* | |||||
* @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}; | |||||
} | |||||
} | |||||
} | } |
@@ -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 ************* | // ************* copied from ConcatTest ************* | ||||
// ------------------------------------------------------ | // ------------------------------------------------------ | ||||