From 0c6dc7f04d26e5b5182bb4adc583a771dbaa182b Mon Sep 17 00:00:00 2001 From: fotis Date: Sun, 12 Nov 2000 20:28:43 +0000 Subject: code look beautified and standardized by JStyle git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193790 13f79535-47bb-0310-9956-ffa450edef68 --- lib/Xslt.java | 555 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 284 insertions(+), 271 deletions(-) (limited to 'lib') diff --git a/lib/Xslt.java b/lib/Xslt.java index af50d9b27..cbb25a0fc 100644 --- a/lib/Xslt.java +++ b/lib/Xslt.java @@ -51,7 +51,7 @@ * information on the Apache Software Foundation, please see * . */ - + //package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.Task; @@ -65,9 +65,9 @@ import org.w3c.dom.*; import org.xml.sax.SAXException; /** - * Task to call the XSLT processor Xalan (part of xml.apache.org), which converts xml files + * Task to call the XSLT processor Xalan (part of xml.apache.org), which converts xml files * from a source to an output using a stylesheet file - * + * *

* This task can take the following arguments: *

- *

+ *

* Of these arguments, infile, outfile and xsltfile are required. *

smart defaults to 'no'. The other allowed value is 'yes'. If smart is set to 'yes' *

* xalan is only called if either the outfile is older than the infile or the stylesheet - * or the outfile doesn't exist. + * or the outfile doesn't exist. *

*

dependent defaults to 'none'. Other possible values: a comma delimited list of file names - * which date is checked against the output file. This way you can name files which, if + * which date is checked against the output file. This way you can name files which, if * they have been modified, initiate a restart of the xslt process, like external entities etc. *

* @author Fotis Jannidis fotis@jannidis.de @@ -94,281 +94,294 @@ import org.xml.sax.SAXException; public class Xslt extends Task { - private String infile, outfile, xsltfile, mergefile; - private String smart = "no"; //defaults to do conversion everytime task is called - private String dependent = "none"; //defaults to no dependencies - private boolean startXslt = false; - - - - /** - * Sets the input file - * - */ - public void setInfile (String infile) { - this.infile = infile; - } - - public void setMergefile (String mergefile) - { - this.mergefile = mergefile; - } - - /** - * Sets the stylesheet file - * - */ - public void setXsltfile (String xsltfile) { - this.xsltfile = xsltfile; - } - - /** - * Sets the output file - * - */ - public void setOutfile (String outfile) { - this.outfile = outfile; - } - - /** - * Sets the value for smart - * - * @param option valid values: - *

- */ - public void setSmart (String smart) { - this.smart = smart; - } - -/** - * Sets the value for dependent - * - * @param option valid values: - * - */ - public void setDependent (String dependent) { - this.dependent = dependent; - } - + private String infile, outfile, xsltfile, mergefile; + private String smart = "no"; //defaults to do conversion everytime task is called + private String dependent = "none"; //defaults to no dependencies + private boolean startXslt = false; + + /** - * Builds a document from the given file, merging the mergefile onto the end of the root node + * Sets the input file + * */ - private org.w3c.dom.Document buildDocument(String xmlFile) - throws IOException, SAXException - { - try { - - javax.xml.parsers.DocumentBuilder docBuilder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document doc = docBuilder.parse(new FileInputStream(xmlFile)); - - if (mergefile != null && !mergefile.equals("")) { - - File mergefileF = new File(mergefile); - - Document mergedoc = docBuilder.parse(new FileInputStream(mergefileF)); - Node mergenode = doc.importNode(mergedoc.getDocumentElement(), true); - doc.getDocumentElement().appendChild(mergenode); - } - - return doc; - } - catch (javax.xml.parsers.ParserConfigurationException e) { - System.out.println("Task xslt - SAX ERROR:\n " + e.getMessage()); - } - return null; - + public void setInfile (String infile) { + this.infile = infile; } - - - /** - * Calls Xalan and does the transformation - * - */ - private void transform(String xmlSourceURL, String xslURL, String outputURL) - throws java.io.IOException, - java.net.MalformedURLException, - org.xml.sax.SAXException - { - StylesheetRoot stylesheet = StylesheetCache.getStylesheet(xsltfile); - - org.w3c.dom.Document source = buildDocument(infile); - - - // Create the 3 objects the XSLTProcessor needs to perform the transformation. - org.apache.xalan.xslt.XSLTInputSource xmlSource = - new org.apache.xalan.xslt.XSLTInputSource (source); - org.apache.xalan.xslt.XSLTResultTarget xmlResult = - new org.apache.xalan.xslt.XSLTResultTarget (outfile); - - // Perform the transformation. - System.out.println("============================"); - System.out.println("xslt \nin: " + infile + "\nstyle: " + xsltfile + "\nout: " + outfile); - System.out.println("============================"); - - stylesheet.process(xmlSource, xmlResult); - - } //end transform - - /** - * Catches the errors transform() can throw and - * returns meaningfull error messages - */ - private void startTransform () { - try { - transform (infile,xsltfile,outfile); - } catch (org.xml.sax.SAXException saxerror) { - System.out.println("Task xslt - SAX ERROR:\n " + saxerror); - } catch (MalformedURLException urlerror) { - System.out.println("Task xslt - URL ERROR:\n " + urlerror); - } catch (IOException ioerror) { - System.out.println("Task xslt - IO ERROR:\n " + ioerror); + + public void setMergefile (String mergefile) { + this.mergefile = mergefile; } - } //end startTransform - - /** - * Checks for existence of output file and compares - * dates with input and stylesheet file - */ - private boolean smartCheck (File outfileF, long outfileLastModified, File infileF, File xsltfileF) { - - if (outfileF.exists()) { - //checks whether output file is older than input file or xslt stylesheet file - if ((outfileLastModified < infileF.lastModified()) | - (outfileLastModified < xsltfileF.lastModified())) { - return true; - } - } else { - //if output file does not exist, start xslt process - return true; + + /** + * Sets the stylesheet file + * + */ + public void setXsltfile (String xsltfile) { + this.xsltfile = xsltfile; } - return false; - } //end smartCheck - - /** - * Checks for existence and date of dependent files - * This could be folded together with smartCheck by using - * a general routine but it wouldn't be as fast as now - */ - private boolean dependenciesCheck(File outfileF, long outfileLastModified) { - String dependentFileName; - File dependentFile; - StringTokenizer tokens = new StringTokenizer(dependent,","); - while (tokens.hasMoreTokens()) { - dependentFileName = (String) tokens.nextToken(); - dependentFile = new File (dependentFileName); - //check: does dependent file exist - if (dependentFile.exists()) { - //check dates - if ((outfileLastModified < dependentFile.lastModified()) ) { - return true; - } - } else { - System.err.println("Task xslt - ERROR in attribute 'dependent':\n file " + dependentFileName + " does not exist."); - } + + /** + * Sets the output file + * + */ + public void setOutfile (String outfile) { + this.outfile = outfile; } - return false; - } //end dependenciesCheck - -/** - * Main method, which is called by ant. - * Checks for the value of smart and calls startTransform accordingly - */ - public void execute () throws org.apache.tools.ant.BuildException { - - File outfileF = new File (outfile); - File infileF = new File(infile); - File xsltfileF = new File (xsltfile); - long outfileLastModified = outfileF.lastModified(); - boolean startFileExist = true; - - //checks whether input and stylesheet exist. - //this could be left to the parser, but this solution does make problems if smart is set to yes - if (!infileF.exists()) { - System.err.println("Task xslt - ERROR:\n Input file " + infile + " does not exist!"); - startFileExist = false; - } else if (!xsltfileF.exists()) { - System.err.println("Task xslt - ERROR:\n Stylesheet file " + xsltfile + " does not exist!"); - startFileExist = false; - } - - //checks attribute 'smart' - if (smart.equals("no")) { - startXslt = true; - //if attribute smart = 'yes' - } else if (smart.equals("yes")) { - startXslt = smartCheck (outfileF,outfileLastModified,infileF,xsltfileF); - //checks dependent files against output file, makes only sense if smartCheck returns false - if (!dependent.equals("none") & (startXslt == false)) { - startXslt = dependenciesCheck(outfileF,outfileLastModified); - } - //returns error message, if smart has another value as 'yes' or 'no' - } else { - System.err.println("Task xslt - ERROR: Allowed values for the attribute smart are 'yes' or 'no'"); + + /** + * Sets the value for smart + * + * @param option valid values: + * + */ + public void setSmart (String smart) { + this.smart = smart; } - if (startFileExist & startXslt) { - startTransform(); + + /** + * Sets the value for dependent + * + * @param option valid values: + * + */ + public void setDependent (String dependent) { + this.dependent = dependent; } - } //end execute - - //quick access for debugging - //usage XSLT infile xsltfile outfile (smart is 'yes') - /* - public static void main (String args[]) { - Xslt xslt = new Xslt(); - xslt.setInfile(args[0]); - xslt.setXsltfile(args[1]); - xslt.setOutfile(args[2]); - xslt.setSmart("yes"); - xslt.setDependent("test1,test2"); - xslt.execute(); - } */ - - /** - * Cache for stylesheets we've already processed + + + /** + * Builds a document from the given file, merging the mergefile onto the end of the root node */ - protected static class StylesheetCache - { - /** Cache of compiled stylesheets (filename, StylesheetRoot) */ - private static Hashtable _stylesheetCache = new Hashtable(); - - /** - * Returns a compiled StylesheetRoot object for a given filename - */ - public static StylesheetRoot getStylesheet(String xsltFilename) - throws org.xml.sax.SAXException - { - if (_stylesheetCache.containsKey(xsltFilename)) { - return (StylesheetRoot)_stylesheetCache.get(xsltFilename); - } - - // Use XSLTProcessor to instantiate an XSLTProcessor. - org.apache.xalan.xslt.XSLTProcessor processor = - org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison()); - - - org.apache.xalan.xslt.XSLTInputSource xslSheet = - new org.apache.xalan.xslt.XSLTInputSource (xsltFilename); - - // Perform the transformation. - System.out.println("****************************"); - System.out.println("xslt compile \nin: " + xsltFilename); - System.out.println("****************************"); - - StylesheetRoot compiledSheet = processor.processStylesheet(xslSheet); - - _stylesheetCache.put(xsltFilename, compiledSheet); - return compiledSheet; - } + private org.w3c.dom.Document buildDocument(String xmlFile) + throws IOException, SAXException { + try { + + javax.xml.parsers.DocumentBuilder docBuilder = + javax.xml.parsers.DocumentBuilderFactory.newInstance(). + newDocumentBuilder(); + Document doc = docBuilder.parse(new FileInputStream(xmlFile)); + + if (mergefile != null && !mergefile.equals("")) { + + File mergefileF = new File(mergefile); + + Document mergedoc = + docBuilder.parse(new FileInputStream(mergefileF)); + Node mergenode = + doc.importNode(mergedoc.getDocumentElement(), true); + doc.getDocumentElement().appendChild(mergenode); + } + + return doc; + } catch (javax.xml.parsers.ParserConfigurationException e) { + System.out.println("Task xslt - SAX ERROR:\n " + + e.getMessage()); + } + return null; } - - + + /** + * Calls Xalan and does the transformation + * + */ + private void transform(String xmlSourceURL, String xslURL, + String outputURL) throws java.io.IOException, + java.net.MalformedURLException, org.xml.sax.SAXException { + StylesheetRoot stylesheet = StylesheetCache.getStylesheet(xsltfile); + + org.w3c.dom.Document source = buildDocument(infile); + + + // Create the 3 objects the XSLTProcessor needs to perform the transformation. + org.apache.xalan.xslt.XSLTInputSource xmlSource = + new org.apache.xalan.xslt.XSLTInputSource (source); + org.apache.xalan.xslt.XSLTResultTarget xmlResult = + new org.apache.xalan.xslt.XSLTResultTarget (outfile); + + // Perform the transformation. + System.out.println("============================"); + System.out.println("xslt \nin: " + infile + "\nstyle: " + + xsltfile + "\nout: " + outfile); + System.out.println("============================"); + + stylesheet.process(xmlSource, xmlResult); + + } //end transform + + /** + * Catches the errors transform() can throw and + * returns meaningfull error messages + */ + private void startTransform () { + try { + transform (infile, xsltfile, outfile); + } catch (org.xml.sax.SAXException saxerror) { + System.out.println("Task xslt - SAX ERROR:\n " + saxerror); + } + catch (MalformedURLException urlerror) { + System.out.println("Task xslt - URL ERROR:\n " + urlerror); + } + catch (IOException ioerror) { + System.out.println("Task xslt - IO ERROR:\n " + ioerror); + } + } //end startTransform + + /** + * Checks for existence of output file and compares + * dates with input and stylesheet file + */ + private boolean smartCheck (File outfileF, + long outfileLastModified, File infileF, File xsltfileF) { + + if (outfileF.exists()) { + //checks whether output file is older than input file or xslt stylesheet file + if ((outfileLastModified < infileF.lastModified()) | + (outfileLastModified < xsltfileF.lastModified())) { + return true; + } + } else { + //if output file does not exist, start xslt process + return true; + } + return false; + } //end smartCheck + + /** + * Checks for existence and date of dependent files + * This could be folded together with smartCheck by using + * a general routine but it wouldn't be as fast as now + */ + private boolean dependenciesCheck(File outfileF, + long outfileLastModified) { + String dependentFileName; + File dependentFile; + StringTokenizer tokens = new StringTokenizer(dependent, ","); + while (tokens.hasMoreTokens()) { + dependentFileName = (String) tokens.nextToken(); + dependentFile = new File (dependentFileName); + //check: does dependent file exist + if (dependentFile.exists()) { + //check dates + if ((outfileLastModified < dependentFile.lastModified())) { + return true; + } + } else { + System.err.println( + "Task xslt - ERROR in attribute 'dependent':\n file " + + dependentFileName + " does not exist."); + } + } + return false; + } //end dependenciesCheck + + /** + * Main method, which is called by ant. + * Checks for the value of smart and calls startTransform accordingly + */ + public void execute () throws org.apache.tools.ant.BuildException { + + File outfileF = new File (outfile); + File infileF = new File(infile); + File xsltfileF = new File (xsltfile); + long outfileLastModified = outfileF.lastModified(); + boolean startFileExist = true; + + //checks whether input and stylesheet exist. + //this could be left to the parser, but this solution does make problems if smart is set to yes + if (!infileF.exists()) { + System.err.println( + "Task xslt - ERROR:\n Input file " + infile + + " does not exist!"); + startFileExist = false; + } else if (!xsltfileF.exists()) { + System.err.println( + "Task xslt - ERROR:\n Stylesheet file " + + xsltfile + " does not exist!"); + startFileExist = false; + } + + //checks attribute 'smart' + if (smart.equals("no")) { + startXslt = true; + //if attribute smart = 'yes' + } else if (smart.equals("yes")) { + startXslt = smartCheck (outfileF, outfileLastModified, + infileF, xsltfileF); + //checks dependent files against output file, makes only sense if smartCheck returns false + if (!dependent.equals("none") & (startXslt == false)) { + startXslt = + dependenciesCheck(outfileF, outfileLastModified); + } + //returns error message, if smart has another value as 'yes' or 'no' + } else { + System.err.println("Task xslt - ERROR: Allowed values for the attribute smart are 'yes' or 'no'"); + } + if (startFileExist & startXslt) { + startTransform(); + } + } //end execute + + //quick access for debugging + //usage XSLT infile xsltfile outfile (smart is 'yes') + /* + public static void main (String args[]) { + Xslt xslt = new Xslt(); + xslt.setInfile(args[0]); + xslt.setXsltfile(args[1]); + xslt.setOutfile(args[2]); + xslt.setSmart("yes"); + xslt.setDependent("test1,test2"); + xslt.execute(); +} */ + + /** + * Cache for stylesheets we've already processed + */ + protected static class StylesheetCache { + /** Cache of compiled stylesheets (filename, StylesheetRoot) */ + private static Hashtable _stylesheetCache = new Hashtable(); + + /** + * Returns a compiled StylesheetRoot object for a given filename + */ + public static StylesheetRoot getStylesheet(String xsltFilename) + throws org.xml.sax.SAXException { + if (_stylesheetCache.containsKey(xsltFilename)) { + return (StylesheetRoot)_stylesheetCache.get(xsltFilename); + } + + // Use XSLTProcessor to instantiate an XSLTProcessor. + org.apache.xalan.xslt.XSLTProcessor processor = + org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor( + new org.apache.xalan.xpath.xdom.XercesLiaison()); + + + org.apache.xalan.xslt.XSLTInputSource xslSheet = + new org.apache.xalan.xslt.XSLTInputSource ( + xsltFilename); + + // Perform the transformation. + System.out.println("****************************"); + System.out.println("xslt compile \nin: " + xsltFilename); + System.out.println("****************************"); + + StylesheetRoot compiledSheet = + processor.processStylesheet(xslSheet); + + _stylesheetCache.put(xsltFilename, compiledSheet); + return compiledSheet; + } + } + + + } -- cgit v1.2.3