bodewig 01/04/03 05:37:32 Modified: src/main/org/apache/tools/ant/taskdefs XSLTProcess.java Log: Don't parse the XSLT stylesheet unless we know a file is going to be transformed. This should improve performance in cases where the generated files are up to date significantly. Revision Changes Path 1.20 +51 -36 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.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- XSLTProcess.java 2001/03/19 12:27:23 1.19 +++ XSLTProcess.java 2001/04/03 12:37:31 1.20 @@ -84,7 +84,8 @@ * @author Keith Visco * @author Sam Ruby * @author Russell Gold - * @version $Revision: 1.19 $ $Date: 2001/03/19 12:27:23 $ + * @author Stefan Bodewig + * @version $Revision: 1.20 $ $Date: 2001/04/03 12:37:31 $ */ public class XSLTProcess extends MatchingTask { @@ -102,6 +103,7 @@ private File outFile = null; private XSLTLiaison liaison; + private boolean stylesheetLoaded = false; /** * Creates a new XSLTProcess Task. @@ -118,47 +120,33 @@ String[] list; String[] dirs; + if (xslFile == null) { + throw new BuildException("no stylesheet specified", location); + } + if (baseDir == null) { baseDir = project.resolveFile("."); } liaison = getLiaison(); log("Using "+liaison.getClass().toString(), Project.MSG_VERBOSE); - - long styleSheetLastModified = 0; - if (xslFile != null) { - try { - File file = project.resolveFile(xslFile, project.getBaseDir()); - if (!file.exists()) { - file = project.resolveFile(xslFile, baseDir); - /* - * shouldn't throw out deprectaion warnings before we know, - * the wrong version has been used. - */ - if (file.exists()) { - log("DEPRECATED - the style attribute should be relative to the project\'s"); - log(" basedir, not the tasks\'s basedir."); - } - } - - // Create a new XSL processor with the specified stylesheet - styleSheetLastModified = file.lastModified(); - log( "Loading stylesheet " + file, Project.MSG_INFO); - liaison.setStylesheet( file.toString() ); - for(Enumeration e = params.elements();e.hasMoreElements();) { - Param p = (Param)e.nextElement(); - liaison.addParam( p.getName(), p.getExpression() ); - } - } catch (Exception ex) { - log("Failed to read stylesheet " + xslFile, Project.MSG_INFO); - throw new BuildException(ex); + File stylesheet = project.resolveFile(xslFile, project.getBaseDir()); + if (!stylesheet.exists()) { + stylesheet = project.resolveFile(xslFile, baseDir); + /* + * shouldn't throw out deprecation warnings before we know, + * the wrong version has been used. + */ + if (stylesheet.exists()) { + log("DEPRECATED - the style attribute should be relative to the project\'s"); + log(" basedir, not the tasks\'s basedir."); } } // if we have an in file and out then process them if (inFile != null && outFile != null) { - process(inFile, outFile, styleSheetLastModified); + process(inFile, outFile, stylesheet); return; } @@ -178,7 +166,7 @@ // Process all the files marked for styling list = scanner.getIncludedFiles(); for (int i = 0;i < list.length; ++i) { - process( baseDir, list[i], destDir, styleSheetLastModified ); + process( baseDir, list[i], destDir, stylesheet ); } // Process all the directoried marked for styling @@ -186,7 +174,7 @@ for (int j = 0;j < dirs.length;++j){ list=new File(baseDir,dirs[j]).list(); for (int i = 0;i < list.length;++i) - process( baseDir, list[i], destDir, styleSheetLastModified ); + process( baseDir, list[i], destDir, stylesheet ); } } //-- execute @@ -211,7 +199,7 @@ * @param name the extension to use **/ public void setExtension(String name) { - targetExtension = name; + targetExtension = name; } //-- setDestDir /** @@ -261,14 +249,15 @@ * in the given resultFile. **/ private void process(File baseDir, String xmlFile, File destDir, - long styleSheetLastModified) + File stylesheet) throws BuildException { String fileExt=targetExtension; File outFile=null; File inFile=null; - + try { + long styleSheetLastModified = stylesheet.lastModified(); inFile = new File(baseDir,xmlFile); int dotPos = xmlFile.lastIndexOf('.'); if(dotPos>0){ @@ -281,6 +270,7 @@ ensureDirectoryFor( outFile ); log("Transforming into "+destDir); + configureLiaison(stylesheet); liaison.transform(inFile.toString(), outFile.toString()); } } @@ -297,8 +287,9 @@ } //-- processXML - private void process(File inFile, File outFile, long styleSheetLastModified) throws BuildException { + private void process(File inFile, File outFile, File stylesheet) throws BuildException { try{ + long styleSheetLastModified = stylesheet.lastModified(); 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); @@ -306,6 +297,7 @@ styleSheetLastModified > outFile.lastModified()) { ensureDirectoryFor( outFile ); log("Processing " + inFile + " to " + outFile, Project.MSG_INFO); + configureLiaison(stylesheet); liaison.transform(inFile.toString(), outFile.toString()); } }catch (Exception ex) { @@ -374,4 +366,27 @@ return expression; } } + + /** + * Loads the stylesheet and set xsl:param parameters. + */ + protected void configureLiaison(File stylesheet) throws BuildException { + if (stylesheetLoaded) { + return; + } + stylesheetLoaded = true; + + try { + log( "Loading stylesheet " + stylesheet, Project.MSG_INFO); + liaison.setStylesheet( stylesheet.toString() ); + for(Enumeration e = params.elements();e.hasMoreElements();) { + Param p = (Param)e.nextElement(); + liaison.addParam( p.getName(), p.getExpression() ); + } + } catch (Exception ex) { + log("Failed to read stylesheet " + stylesheet, Project.MSG_INFO); + throw new BuildException(ex); + } + } + } //-- XSLTProcess