conor 2003/01/31 20:39:08 Modified: src/main/org/apache/tools/ant/taskdefs XSLTProcess.java src/main/org/apache/tools/ant/taskdefs/optional TraXLiaison.java docs/manual/CoreTasks style.html Log: 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 Revision Changes Path 1.58 +6 -7 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java Index: XSLTProcess.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java,v retrieving revision 1.57 retrieving revision 1.58 diff -u -w -u -r1.57 -r1.58 --- XSLTProcess.java 24 Jan 2003 14:34:47 -0000 1.57 +++ XSLTProcess.java 1 Feb 2003 04:39:06 -0000 1.58 @@ -182,16 +182,15 @@ } /** - * Whether to reuse the transformer instance when transforming - * multiple files. + * Controls whether the stylesheet is reloaded for every transform * - * <p>Setting this to false may get around a bug in certain - * Xalan-J version, default is true.</p> + * <p>Setting this to true may get around a bug in certain + * Xalan-J versions, default is false.</p> * * @since Ant 1.6 */ - public void setReuseLoadedStylesheet(boolean b) { - reuseLoadedStylesheet = b; + public void setReloadStylesheet(boolean b) { + reuseLoadedStylesheet = !b; } /** 1.23 +63 -30 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java Index: TraXLiaison.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -w -u -r1.22 -r1.23 --- TraXLiaison.java 31 Oct 2002 14:30:07 -0000 1.22 +++ TraXLiaison.java 1 Feb 2003 04:39:07 -0000 1.23 @@ -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.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 @@ /** 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 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 @@ fis.close(); } } catch (IOException ignored) { + // ignore } try { if (fos != null) { fos.close(); } } catch (IOException ignored) { + // ignore } } } @@ -198,14 +220,10 @@ } /** - * 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,15 +231,37 @@ // 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(); + templates = getFactory().newTemplates(src); + } finally { + if (xslStream != null) { + xslStream.close(); + } + } + } + + /** + * 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); @@ -235,13 +275,6 @@ for (int i = 0; i < outputProperties.size(); i++) { final String[] pair = (String[]) outputProperties.elementAt(i); transformer.setOutputProperty(pair[0], pair[1]); - } - return transformer; - } finally { - try { - xslStream.close(); - } catch (IOException ignored) { - } } } 1.25 +4 -4 jakarta-ant/docs/manual/CoreTasks/style.html Index: style.html =================================================================== RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/style.html,v retrieving revision 1.24 retrieving revision 1.25 diff -u -w -u -r1.24 -r1.25 --- style.html 15 Nov 2002 11:59:31 -0000 1.24 +++ style.html 1 Feb 2003 04:39:07 -0000 1.25 @@ -152,11 +152,11 @@ <td valign="top" align="center">No</td> </tr> <tr> - <td valign="top">reuseloadedstylesheet</td> - <td valign="top">Reuse the same transformer when transforming - multiple files. If you set this to false, performance will + <td valign="top">reloadstylesheet</td> + <td valign="top">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 <code>true</code>. <em>Since Ant 1.6</em>.</td> + Default is <code>false</code>. <em>Since Ant 1.6</em>.</td> <td valign="top" align="center">No</td> </tr> </table>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]