Browse Source

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
master
Stefan Bodewig 21 years ago
parent
commit
6dbabcb776
5 changed files with 113 additions and 7 deletions
  1. +2
    -0
      WHATSNEW
  2. +19
    -1
      docs/manual/CoreTasks/style.html
  3. +15
    -0
      src/etc/testcases/taskdefs/style/build.xml
  4. +63
    -6
      src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
  5. +14
    -0
      src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java

+ 2
- 0
WHATSNEW View File

@@ -141,6 +141,8 @@ Other changes:

* <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
=============================================



+ 19
- 1
docs/manual/CoreTasks/style.html View File

@@ -56,7 +56,8 @@ element which is used to perform Entity and URI resolution</p>
<tr>
<td valign="top">extension</td>
<td valign="top">desired file extension to be used for the targets. If not
specified, the default is &quot;.html&quot;.</td>
specified, the default is &quot;.html&quot;. Will be ignored if
a nested &lt;mapper&gt; has been specified.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
@@ -288,6 +289,16 @@ And in Saxon 7.x:
</table>
</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>&lt;xslt&gt;</code> removes the file extension from the
source file and adds the extension specified via the extension
attribute.</p>

<h3>Examples</h3>
<blockquote>
<pre>
@@ -334,6 +345,13 @@ And in Saxon 7.x:
&lt;attribute name=&quot;http://xml.apache.org/xalan/features/optimize&quot; value=&quot;true&quot;/&gt;
&lt;/factory&gt;
&lt;/xslt&gt;</pre>

<h4>Using a mapper</h4>
<pre>&lt;xslt basedir=&quot;in&quot; destdir=&quot;out&quot;
style=&quot;style/apache.xsl&quot;&gt;
&lt;mapper type=&quot;glob&quot; from=&quot;*.xml.en&quot; to=&quot;*.html.en&quot;/&gt;
&lt;/xslt&gt;</pre>

</blockquote>
<hr>
<p align="center">Copyright &copy; 2000-2004 The Apache Software Foundation. All rights


+ 15
- 0
src/etc/testcases/taskdefs/style/build.xml View File

@@ -42,6 +42,21 @@
</style>
</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">
<antcall target="copyXsl">
<param name="xsl.value" value="old-value"/>


+ 63
- 6
src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java View File

@@ -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 &lt;xslt&gt;
* 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};
}
}

}

+ 14
- 0
src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java View File

@@ -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 *************

// ------------------------------------------------------


Loading…
Cancel
Save