From 8513609d9c7d547fa4907dde0522e409b0018095 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Sat, 1 Feb 2003 04:39:07 +0000 Subject: [PATCH] Rename the reuseloadedstylesheet to reloadstylesheet with inverted sense Make the TraxLiason cache the templates instance Reset the transformer if a new stylsheet is set PR: 13589 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273944 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/style.html | 8 +- .../tools/ant/taskdefs/XSLTProcess.java | 11 +-- .../ant/taskdefs/optional/TraXLiaison.java | 93 +++++++++++++------ 3 files changed, 72 insertions(+), 40 deletions(-) diff --git a/docs/manual/CoreTasks/style.html b/docs/manual/CoreTasks/style.html index fd7c18ac3..2b72fd799 100644 --- a/docs/manual/CoreTasks/style.html +++ b/docs/manual/CoreTasks/style.html @@ -152,11 +152,11 @@ element which is used to perform Entity and URI resolution

No - reuseloadedstylesheet - Reuse the same transformer when transforming - multiple files. If you set this to false, performance will + reloadstylesheet + Control whether the stylesheet transformer is created + anew for every transform opertaion. If you set this to true, performance may suffer, but you may work around a bug in certain Xalan-J versions. - Default is true. Since Ant 1.6. + Default is false. Since Ant 1.6. No diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java index 9a2f455cd..c215a01d4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java +++ b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java @@ -182,16 +182,15 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { } /** - * Whether to reuse the transformer instance when transforming - * multiple files. + * Controls whether the stylesheet is reloaded for every transform * - *

Setting this to false may get around a bug in certain - * Xalan-J version, default is true.

+ *

Setting this to true may get around a bug in certain + * Xalan-J versions, default is false.

* * @since Ant 1.6 */ - public void setReuseLoadedStylesheet(boolean b) { - reuseLoadedStylesheet = b; + public void setReloadStylesheet(boolean b) { + reuseLoadedStylesheet = !b; } /** diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java index dab1fb802..876aea3bf 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java @@ -1,7 +1,7 @@ /* * 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. * * 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.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.TransformerConfigurationException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.XSLTLiaison; import org.apache.tools.ant.taskdefs.XSLTLogger; @@ -114,6 +115,15 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware /** transformer to use for processing files */ 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 */ private URIResolver uriResolver; @@ -130,12 +140,22 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware } 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; } public void transform(File infile, File outfile) throws Exception { if (transformer == null) { - transformer = newTransformer(); + createTransformer(); } InputStream fis = null; @@ -157,12 +177,14 @@ public class TraXLiaison implements XSLTLiaison, ErrorListener, XSLTLoggerAware fis.close(); } } catch (IOException ignored) { + // ignore } try { if (fos != null) { fos.close(); } } 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 // 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. // (always keep control), otherwise you won't be able to delete // the file quickly on windows. - InputStream xslStream = new BufferedInputStream( - new FileInputStream(stylesheet)); + InputStream xslStream = null; try { + xslStream + = new BufferedInputStream(new FileInputStream(stylesheet)); + templatesModTime = stylesheet.lastModified(); StreamSource src = new StreamSource(xslStream); // Always set the systemid to the source for imports, includes... // in xsl and xml... 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 { - try { + if (xslStream != null) { 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.