Browse Source

Add force attribute to <style> task to allow it to run independent of

file modification times.

Submitted by:	Craeg K Strong <cstrong@arielpartners.com>

Allow classpath from which the processor will be loaded to be
specified.

PR:             2144

Make sure Liaison classes close their output streams.

PR:             1848


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269217 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
1e57e83036
3 changed files with 104 additions and 22 deletions
  1. +91
    -20
      src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
  2. +7
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java
  3. +6
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/XslpLiaison.java

+ 91
- 20
src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java View File

@@ -61,6 +61,8 @@ import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.tools.ant.*;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;


/**
@@ -102,9 +104,13 @@ public class XSLTProcess extends MatchingTask {
private File outFile = null;

private String processor;
private Path classpath = null;
private XSLTLiaison liaison;
private boolean stylesheetLoaded = false;

private boolean force = false;

/**
* Creates a new XSLTProcess Task.
**/
@@ -178,6 +184,13 @@ public class XSLTProcess extends MatchingTask {
}
} //-- execute

/**
* Set whether to check dependencies, or always generate.
**/
public void setForce(boolean force) {
this.force = force;
} //-- setForce

/**
* Set the base directory.
**/
@@ -210,24 +223,72 @@ public class XSLTProcess extends MatchingTask {
this.xslFile = xslFile;
}

public void setProcessor(String processor) throws Exception {
/**
* Set the classpath to load the Processor through (attribute).
*/
public void setClasspath(Path classpath) {
createClasspath().append(classpath);
}

/**
* Set the classpath to load the Processor through (nested element).
*/
public Path createClasspath() {
if (classpath == null) {
classpath = new Path(project);
}
return classpath.createPath();
}

/**
* Set the classpath to load the Processor through via reference
* (attribute).
*/
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
}


if (processor.equals("trax")) {
public void setProcessor(String processor) {
this.processor = processor;
}

/**
* Load processor here instead of in setProcessor - this will be
* called from within execute, so we have access to the latest
* classpath.
*/
private void resolveProcessor(String proc) throws Exception {
if (proc.equals("trax")) {
final Class clazz =
Class.forName("org.apache.tools.ant.taskdefs.optional.TraXLiaison");
loadClass("org.apache.tools.ant.taskdefs.optional.TraXLiaison");
liaison = (XSLTLiaison)clazz.newInstance();
} else if (processor.equals("xslp")) {
} else if (proc.equals("xslp")) {
final Class clazz =
Class.forName("org.apache.tools.ant.taskdefs.optional.XslpLiaison");
loadClass("org.apache.tools.ant.taskdefs.optional.XslpLiaison");
liaison = (XSLTLiaison) clazz.newInstance();
} else if (processor.equals("xalan")) {
} else if (proc.equals("xalan")) {
final Class clazz =
Class.forName("org.apache.tools.ant.taskdefs.optional.XalanLiaison");
loadClass("org.apache.tools.ant.taskdefs.optional.XalanLiaison");
liaison = (XSLTLiaison)clazz.newInstance();
} else {
liaison = (XSLTLiaison) Class.forName(processor).newInstance();
liaison = (XSLTLiaison) loadClass(proc).newInstance();
}
}

/**
* Load named class either via the system classloader or a given
* custom classloader.
*/
private Class loadClass(String classname) throws Exception {
if (classpath == null) {
return Class.forName(classname);
} else {
AntClassLoader al = new AntClassLoader(project, classpath);
Class c = al.loadClass(classname);
AntClassLoader.initializeClass(c);
return c;
}
}

/**
@@ -265,7 +326,8 @@ public class XSLTProcess extends MatchingTask {
}else{
outFile = new File(destDir,xmlFile+fileExt);
}
if (inFile.lastModified() > outFile.lastModified() ||
if (force ||
inFile.lastModified() > outFile.lastModified() ||
styleSheetLastModified > outFile.lastModified()) {
ensureDirectoryFor( outFile );
log("Transforming into "+destDir);
@@ -293,7 +355,8 @@ public class XSLTProcess extends MatchingTask {
log("In file "+inFile+" time: " + inFile.lastModified() , Project.MSG_DEBUG);
log("Out file "+outFile+" time: " + outFile.lastModified() , Project.MSG_DEBUG);
log("Style file "+xslFile+" time: " + styleSheetLastModified , Project.MSG_DEBUG);
if (inFile.lastModified() > outFile.lastModified() ||
if (force ||
inFile.lastModified() > outFile.lastModified() ||
styleSheetLastModified > outFile.lastModified()) {
ensureDirectoryFor( outFile );
log("Processing " + inFile + " to " + outFile, Project.MSG_INFO);
@@ -321,18 +384,26 @@ public class XSLTProcess extends MatchingTask {
// if processor wasn't specified, see if TraX is available. If not,
// default it to xslp or xalan, depending on which is in the classpath
if (liaison == null) {
try {
setProcessor("trax");
} catch (Throwable e1) {
if (processor != null) {
try {
resolveProcessor(processor);
} catch (Exception e) {
throw new BuildException(e);
}
} else {
try {
setProcessor("xslp");
} catch (Throwable e2) {
resolveProcessor("trax");
} catch (Throwable e1) {
try {
setProcessor("xalan");
} catch (Throwable e3) {
e2.printStackTrace();
e3.printStackTrace();
throw new BuildException(e1);
resolveProcessor("xslp");
} catch (Throwable e2) {
try {
resolveProcessor("xalan");
} catch (Throwable e3) {
e3.printStackTrace();
e2.printStackTrace();
throw new BuildException(e1);
}
}
}
}


+ 7
- 1
src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java View File

@@ -93,7 +93,13 @@ public class TraXLiaison implements XSLTLiaison {
};

public void transform(String infile, String outfile) throws Exception {
transformer.transform(new StreamSource(normalize(infile)), new StreamResult(new FileOutputStream(outfile)));
FileOutputStream out = new FileOutputStream(outfile);
try {
transformer.transform(new StreamSource(normalize(infile)),
new StreamResult(out));
} finally {
out.close();
}
}

protected String normalize(String fileName) {


+ 6
- 1
src/main/org/apache/tools/ant/taskdefs/optional/XslpLiaison.java View File

@@ -82,7 +82,12 @@ public class XslpLiaison implements XSLTLiaison {
};

public void transform(String infile, String outfile) throws Exception {
processor.process(infile, xslSheet, new FileWriter(outfile));
FileWriter out = new FileWriter(outfile);
try {
processor.process(infile, xslSheet, out);
} finally {
out.close();
}
}

public void addParam(String name, String expression){


Loading…
Cancel
Save