Browse Source

resource collection support for xslt

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@292249 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 19 years ago
parent
commit
575148a663
4 changed files with 132 additions and 7 deletions
  1. +24
    -0
      docs/manual/CoreTasks/style.html
  2. +9
    -0
      src/etc/testcases/taskdefs/style/build.xml
  3. +90
    -6
      src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
  4. +9
    -1
      src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java

+ 24
- 0
docs/manual/CoreTasks/style.html View File

@@ -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 been matched. If this behavior is not what you want, set the
scanincludedirectories attribute to false.</p> scanincludedirectories attribute to false.</p>


<p>Starting with Ant 1.7 this task supports nested <a
href="../CoreTypes/resources.html#collection">resource collection</a>s
in addition to (or instead of, depending on the useImplicitFileset
attribute) the implicit fileset formed by this task.</p>

<p>This task supports the use of a nested <code>&lt;param&gt;</code> element which is used to pass values <p>This task supports the use of a nested <code>&lt;param&gt;</code> element which is used to pass values
to an <code>&lt;xsl:param&gt;</code> declaration.</p> to an <code>&lt;xsl:param&gt;</code> declaration.</p>
<p>This task supports the use of a nested <a href="../CoreTypes/xmlcatalog.html">xmlcatalog</a> <p>This task supports the use of a nested <a href="../CoreTypes/xmlcatalog.html">xmlcatalog</a>
@@ -176,8 +181,27 @@ element which is used to perform Entity and URI resolution</p>
Default is <code>false</code>. <em>Since Ant 1.5.2</em>.</td> Default is <code>false</code>. <em>Since Ant 1.5.2</em>.</td>
<td valign="top" align="center">No</td> <td valign="top" align="center">No</td>
</tr> </tr>
<tr>
<td valign="top">useImplicitFileset</td>
<td valign="top">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 <code>true</code>.
<em>Since Ant 1.7</em>.</td>
<td valign="top" align="center">No</td>
</tr>
</table> </table>
<h3>Parameters specified as nested elements</h3> <h3>Parameters specified as nested elements</h3>

<h4>any <a href="../CoreTypes/resources.html#collection">resource
collection</a></h4>

<p><em>since Ant 1.7</em></p>

<p>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.</p>

<h4>classpath</h4> <h4>classpath</h4>
<p>The classpath to load the processor from can be specified via a <p>The classpath to load the processor from can be specified via a
nested <code>&lt;classpath&gt;</code>, as well - that is, a nested <code>&lt;classpath&gt;</code>, as well - that is, a


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

@@ -57,6 +57,15 @@
</xslt> </xslt>
</target> </target>


<target name="testExplicitFileset">
<property name="value" value="myvalue"/>
<xslt style="printParams.xsl" destDir="${out.dir}"
useImplicitFileset="false" basedir="..">
<param name="set" expression="${value}"/>
<fileset dir="."/>
</xslt>
</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"/>


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

@@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs;


import java.io.File; import java.io.File;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException; 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.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.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.XMLCatalog; 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.FileNameMapper;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;


@@ -140,6 +145,20 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
*/ */
private Mapper mapperElement = null; 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. * Creates a new XSLTProcess Task.
*/ */
@@ -183,6 +202,17 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
mapperElement = mapper; 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. * Executes the task.
* *
@@ -248,11 +278,10 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
* in batch processing mode. * 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); scanner = getDirectoryScanner(baseDir);
log("Transforming into " + destDir, Project.MSG_INFO); 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 { } finally {
if (loader != null) { if (loader != null) {
loader.resetThreadContextLoader(); loader.resetThreadContextLoader();
@@ -283,7 +318,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
baseDir = savedBaseDir; baseDir = savedBaseDir;
} }
} }
/** /**
* Set whether to check dependencies, or always generate; * Set whether to check dependencies, or always generate;
* optional, default is false. * optional, default is false.
@@ -376,6 +411,18 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
this.processor = processor; this.processor = processor;
} }


/**
* Whether to use the implicit fileset.
*
* <p>Set this to false if you want explicit control with nested
* resource collections.</p>
*
* @since Ant 1.7
*/
public void setUseImplicitFileset(boolean b) {
useImplicitFileset = b;
}

/** /**
* Add the catalog to our internal catalog * Add the catalog to our internal catalog
* *
@@ -451,6 +498,43 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
this.inFile = inFile; 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 * Processes the given input XML file and stores the result
* in the given resultFile. * in the given resultFile.


+ 9
- 1
src/testcases/org/apache/tools/ant/taskdefs/StyleTest.java View File

@@ -84,8 +84,16 @@ public class StyleTest extends BuildFileTest {




public void testDefaultMapper() throws Exception { 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()); assertTrue(!getProject().resolveFile("out/data.html").exists());
expectFileContains("testDefaultMapper",
expectFileContains(target,
"out/data.html", "out/data.html",
"set='myvalue'"); "set='myvalue'");
} }


Loading…
Cancel
Save