@@ -1,7 +1,7 @@
/*
/*
* The Apache Software License, Version 1.1
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
* reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* Redistribution and use in source and binary forms, with or without
@@ -75,6 +75,7 @@ import javax.xml.transform.URIResolver;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerConfigurationException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.XSLTLiaison;
import org.apache.tools.ant.taskdefs.XSLTLiaison;
import org.apache.tools.ant.taskdefs.XSLTLogger;
import org.apache.tools.ant.taskdefs.XSLTLogger;
@@ -114,6 +115,15 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware
/** transformer to use for processing files */
/** transformer to use for processing files */
private Transformer transformer;
private Transformer transformer;
/** The In memory version of the stylesheet */
private Templates templates;
/**
* The modification time of the stylesheet from which the templates
* are read
*/
private long templatesModTime;
/** possible resolver for URIs */
/** possible resolver for URIs */
private URIResolver uriResolver;
private URIResolver uriResolver;
@@ -130,12 +140,22 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware
}
}
public void setStylesheet(File stylesheet) throws Exception {
public void setStylesheet(File stylesheet) throws Exception {
if (this.stylesheet != null) {
// resetting the stylesheet - reset transformer
transformer = null;
// do we need to reset templates as well
if (!this.stylesheet.equals(stylesheet)
|| (stylesheet.lastModified() != templatesModTime)) {
templates = null;
}
}
this.stylesheet = stylesheet;
this.stylesheet = stylesheet;
}
}
public void transform(File infile, File outfile) throws Exception {
public void transform(File infile, File outfile) throws Exception {
if (transformer == null) {
if (transformer == null) {
transformer = newTransformer();
create Transformer();
}
}
InputStream fis = null;
InputStream fis = null;
@@ -157,12 +177,14 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware
fis.close();
fis.close();
}
}
} catch (IOException ignored) {
} catch (IOException ignored) {
// ignore
}
}
try {
try {
if (fos != null) {
if (fos != null) {
fos.close();
fos.close();
}
}
} catch (IOException ignored) {
} catch (IOException ignored) {
// ignore
}
}
}
}
}
}
@@ -198,14 +220,10 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware
}
}
/**
/**
* Create a new transformer based on the liaison settings
* @return the newly created and configured transformer.
* @throws Exception thrown if there is an error during creation.
* @see #setStylesheet(java.io.File)
* @see #addParam(java.lang.String, java.lang.String)
* @see #setOutputProperty(java.lang.String, java.lang.String)
* Read in templates from the stylsheet
*/
*/
private Transformer newTransformer() throws Exception {
private void readTemplates()
throws IOException, TransformerConfigurationException {
// WARN: Don't use the StreamSource(File) ctor. It won't work with
// WARN: Don't use the StreamSource(File) ctor. It won't work with
// xalan prior to 2.2 because of systemid bugs.
// xalan prior to 2.2 because of systemid bugs.
@@ -213,37 +231,52 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware
// and avoid keeping the handle until the object is garbaged.
// and avoid keeping the handle until the object is garbaged.
// (always keep control), otherwise you won't be able to delete
// (always keep control), otherwise you won't be able to delete
// the file quickly on windows.
// the file quickly on windows.
InputStream xslStream = new BufferedInputStream(
new FileInputStream(stylesheet));
InputStream xslStream = null;
try {
try {
xslStream
= new BufferedInputStream(new FileInputStream(stylesheet));
templatesModTime = stylesheet.lastModified();
StreamSource src = new StreamSource(xslStream);
StreamSource src = new StreamSource(xslStream);
// Always set the systemid to the source for imports, includes...
// Always set the systemid to the source for imports, includes...
// in xsl and xml...
// in xsl and xml...
src.setSystemId(JAXPUtils.getSystemId(stylesheet));
src.setSystemId(JAXPUtils.getSystemId(stylesheet));
Templates templates = getFactory().newTemplates(src);
Transformer transformer = templates.newTransformer();
// configure the transformer...
transformer.setErrorListener(this);
if (uriResolver != null) {
transformer.setURIResolver(uriResolver);
}
for (int i = 0; i < params.size(); i++) {
final String[] pair = (String[]) params.elementAt(i);
transformer.setParameter(pair[0], pair[1]);
}
for (int i = 0; i < outputProperties.size(); i++) {
final String[] pair = (String[]) outputProperties.elementAt(i);
transformer.setOutputProperty(pair[0], pair[1]);
}
return transformer;
templates = getFactory().newTemplates(src);
} finally {
} finally {
try {
if (xslStream != null) {
xslStream.close();
xslStream.close();
} catch (IOException ignored) {
}
}
}
}
}
}
/**
* Create a new transformer based on the liaison settings
* @return the newly created and configured transformer.
* @throws Exception thrown if there is an error during creation.
* @see #setStylesheet(java.io.File)
* @see #addParam(java.lang.String, java.lang.String)
* @see #setOutputProperty(java.lang.String, java.lang.String)
*/
private void createTransformer() throws Exception {
if (templates == null) {
readTemplates();
}
transformer = templates.newTransformer();
// configure the transformer...
transformer.setErrorListener(this);
if (uriResolver != null) {
transformer.setURIResolver(uriResolver);
}
for (int i = 0; i < params.size(); i++) {
final String[] pair = (String[]) params.elementAt(i);
transformer.setParameter(pair[0], pair[1]);
}
for (int i = 0; i < outputProperties.size(); i++) {
final String[] pair = (String[]) outputProperties.elementAt(i);
transformer.setOutputProperty(pair[0], pair[1]);
}
}
/**
/**
* return the Transformer factory associated to this liaison.
* return the Transformer factory associated to this liaison.