diff options
author | fotis <fotis@unknown> | 2000-03-14 09:43:09 +0000 |
---|---|---|
committer | fotis <fotis@unknown> | 2000-03-14 09:43:09 +0000 |
commit | 6851a8efe0544cb6d17f19a7d05a225400ccf0bb (patch) | |
tree | fc9bdcd8d31f26c65eb0d2e4dd79741b86fac063 /lib | |
parent | 019ff14759c79605c1e491e007db62fc7191c8a0 (diff) | |
download | xmlgraphics-fop-6851a8efe0544cb6d17f19a7d05a225400ccf0bb.tar.gz xmlgraphics-fop-6851a8efe0544cb6d17f19a7d05a225400ccf0bb.zip |
new fop examples and tests
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193299 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Compare.class | bin | 0 -> 5086 bytes | |||
-rw-r--r-- | lib/Compare.java | 146 | ||||
-rw-r--r-- | lib/Fop.class | bin | 0 -> 4214 bytes | |||
-rw-r--r-- | lib/Fop.java | 122 | ||||
-rw-r--r-- | lib/Xslt.class | bin | 2614 -> 3929 bytes | |||
-rw-r--r-- | lib/Xslt.java | 350 |
6 files changed, 482 insertions, 136 deletions
diff --git a/lib/Compare.class b/lib/Compare.class Binary files differnew file mode 100644 index 000000000..714ffface --- /dev/null +++ b/lib/Compare.class diff --git a/lib/Compare.java b/lib/Compare.java new file mode 100644 index 000000000..66e02005d --- /dev/null +++ b/lib/Compare.java @@ -0,0 +1,146 @@ +/* +// header - edit "Data/yourJavaHeader" to customize +// contents - edit "EventHandlers/Java file/onCreate" to customize +// +*/ + +import java.util.*; +import java.io.*; +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import java.text.DateFormat; + + +public class Compare { + private String referenceDirectory, testDirectory; + private String [] filenameList; + private String filenames; + private static boolean IDENTICAL_FILES = true; + private static boolean NOTIDENTICAL_FILES = false; + private BufferedInputStream oldfileInput; + private BufferedInputStream newfileInput; + + //sets directory for test files + public void setTestDirectory(String testDirectory) { + if (!(testDirectory.endsWith("/") | testDirectory.endsWith("\\"))) { + testDirectory += File.separator; + } + this.testDirectory = testDirectory; + } + + //sets directory for reference files + public void setReferenceDirectory(String referenceDirectory) { + if (!(referenceDirectory.endsWith("/") | referenceDirectory.endsWith("\\"))) { + referenceDirectory += File.separator; + } + this.referenceDirectory = referenceDirectory; + } + + public void setFilenames (String filenames) { + StringTokenizer tokens = new StringTokenizer(filenames,","); + Vector filenameListTmp = new Vector(20); + while (tokens.hasMoreTokens()) { + filenameListTmp.add(tokens.nextToken()); + } + filenameList = new String [filenameListTmp.size()] ; + filenameListTmp.copyInto((String[]) filenameList); + } + + private boolean compareBytes (File oldFile, File newFile) { + try { + oldfileInput = new BufferedInputStream(new FileInputStream(oldFile)); + newfileInput = new BufferedInputStream(new FileInputStream(newFile)); + int charactO = 0; + int charactN = 0; + boolean identical = true; + + while (identical & (charactO != -1)) { + if (charactO == charactN) { + charactO = oldfileInput.read(); + charactN = newfileInput.read(); + } else { + return NOTIDENTICAL_FILES; + } + } + return IDENTICAL_FILES; + } catch (IOException io) { + System.err.println("Task Compare - Error: \n" + io.toString()); + } + return NOTIDENTICAL_FILES; + } + + private boolean compareFileSize(File oldFile, File newFile) { + if (oldFile.length() != newFile.length()) { + return NOTIDENTICAL_FILES; + } else { + return IDENTICAL_FILES; + } + } //end: compareBytes + + private boolean filesExist (File oldFile, File newFile) { + if (!oldFile.exists()) { + System.err.println("Task Compare - ERROR: File " + + referenceDirectory + oldFile.getName() + + " doesn't exist!"); + return false; + } else if (!newFile.exists()) { + System.err.println("Task Compare - ERROR: File " + + testDirectory + newFile.getName() + " doesn't exist!"); + return false; + } else { + return true; + } + } + + public void writeHeader (PrintWriter results) { + String dateTime = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM).format(new Date()); + results.println("<html><head><title>Test Results</title></head><body>\n"); + results.println("<h2>Compare Results<br>"); + results.println("<font size='1'>created " + dateTime + "</font></h2>"); + results.println("<table cellpadding='10' border='2'><thead><th align='center'>reference file</th><th align='center'>test file</th>" + + "<th align='center'>identical?</th></thead>"); + + + } + + //main method of task compare + public void execute () throws BuildException { + boolean identical = false; + File oldFile; + File newFile; + try { + PrintWriter results = new PrintWriter (new FileWriter("results.html"),true); + this.writeHeader(results); + for (int i = 0; i < filenameList.length; i++) { + oldFile = new File (referenceDirectory + filenameList[i]); + newFile = new File (testDirectory + filenameList[i]); + if (filesExist(oldFile, newFile)) { + identical = compareFileSize(oldFile, newFile); + if (identical) { + identical = compareBytes(oldFile,newFile); + } + if (!identical) { + System.out.println("Task Compare: \nFiles " + referenceDirectory + + oldFile.getName()+ " - " + testDirectory + + newFile.getName() + " are *not* identical."); + results.println("<tr><td><a href='" + referenceDirectory + oldFile.getName() + "'>" + + oldFile.getName() + "</a> </td><td> <a href='" + + testDirectory + newFile.getName() +"'>" + + newFile.getName() +"</a>" + + " </td><td><font color='red'>No</font></td></tr>"); + } else { + results.println("<tr><td><a href='" + referenceDirectory + oldFile.getName() + "'>" + + oldFile.getName() + "</a> </td><td> <a href='" + + testDirectory + newFile.getName() + "'>" + + newFile.getName() + "</a>" + + " </td><td>Yes</td></tr>"); + } + } + } + results.println("</table></html>"); + } catch (IOException ioe) { + System.err.println("ERROR: " + ioe); + } + } //end: execute() +} + diff --git a/lib/Fop.class b/lib/Fop.class Binary files differnew file mode 100644 index 000000000..b4c595116 --- /dev/null +++ b/lib/Fop.class diff --git a/lib/Fop.java b/lib/Fop.java new file mode 100644 index 000000000..aa342945c --- /dev/null +++ b/lib/Fop.java @@ -0,0 +1,122 @@ +// Ant +import org.apache.tools.ant.Task; +import org.apache.tools.ant.BuildException; +import org.apache.fop.apps.*; + +// SAX +import org.xml.sax.Parser; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.SAXParseException; + +// Java +import java.io.FileReader; +import java.io.File; +import java.io.FileWriter; +import java.io.PrintWriter; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.net.URL; + +/* the code is adapted from Fops CommandLine class */ +public class Fop { + String fofile, pdffile; + + public void setFofile(String fofile) { + this.fofile = fofile; + } + + public void setPdffile(String pdffile) { + this.pdffile = pdffile; + } + + + /** + * creates a SAX parser, using the value of org.xml.sax.parser + * defaulting to org.apache.xerces.parsers.SAXParser + * + * @return the created SAX parser + */ + static Parser createParser() { + String parserClassName = System.getProperty("org.xml.sax.parser"); + if (parserClassName == null) { + parserClassName = "org.apache.xerces.parsers.SAXParser"; + } + System.err.println("using SAX parser " + parserClassName); + + try { + return (Parser) Class.forName(parserClassName).newInstance(); + } catch (ClassNotFoundException e) { + System.err.println("Could not find " + parserClassName); + } catch (InstantiationException e) { + System.err.println("Could not instantiate " + parserClassName); + } catch (IllegalAccessException e) { + System.err.println("Could not access " + parserClassName); + } catch (ClassCastException e) { + System.err.println(parserClassName + " is not a SAX driver"); + } + return null; + } + + /** + * create an InputSource from a file name + * + * @param filename the name of the file + * @return the InputSource created + */ + protected static InputSource fileInputSource(String filename) { + + /* this code adapted from James Clark's in XT */ + File file = new File(filename); + String path = file.getAbsolutePath(); + String fSep = System.getProperty("file.separator"); + if (fSep != null && fSep.length() == 1) + path = path.replace(fSep.charAt(0), '/'); + if (path.length() > 0 && path.charAt(0) != '/') + path = '/' + path; + try { + return new InputSource(new URL("file", null, + path).toString()); + } + catch (java.net.MalformedURLException e) { + throw new Error("unexpected MalformedURLException"); + } + } + + public void execute () throws BuildException { + boolean errors = false; + String version = Version.getVersion(); + System.out.println("=======================\nTask " + version + + "\nconverting file " + fofile + " to " + pdffile); + + if (!(new File(fofile).exists())) { + errors = true; + System.err.println("Task Fop - ERROR: Formatting objects file " + fofile + " missing."); + } + + Parser parser = createParser(); + + if (parser == null) { + System.err.println("Task Fop - ERROR: Unable to create SAX parser"); + errors = true; + } + + if (!errors) { + try { + Driver driver = new Driver(); + driver.setRenderer("org.apache.fop.render.pdf.PDFRenderer", version); + driver.addElementMapping("org.apache.fop.fo.StandardElementMapping"); + driver.addElementMapping("org.apache.fop.svg.SVGElementMapping"); + driver.setWriter(new PrintWriter(new FileWriter(pdffile))); + driver.buildFOTree(parser, fileInputSource(fofile)); + driver.format(); + driver.render(); + } catch (Exception e) { + System.err.println("Task Fop - FATAL ERROR: " + e.getMessage()); + System.exit(1); + } + } + System.out.println("=======================\n"); + } +} + diff --git a/lib/Xslt.class b/lib/Xslt.class Binary files differindex 2c24a38cd..63a56aa9f 100644 --- a/lib/Xslt.class +++ b/lib/Xslt.class diff --git a/lib/Xslt.java b/lib/Xslt.java index c24c1b132..b2daa1ee8 100644 --- a/lib/Xslt.java +++ b/lib/Xslt.java @@ -9,28 +9,28 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. * * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowlegement may appear in the software itself, - * if and wherever such third-party acknowlegements normally appear. + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software - * Foundation" must not be used to endorse or promote products derived - * from this software without prior written permission. For written - * permission, please contact apache@apache.org. + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" - * nor may "Apache" appear in their names without prior written - * permission of the Apache Group. + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES @@ -51,12 +51,13 @@ * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ - + //package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.Task; import java.net.*; import java.io.*; +import java.util.*; import org.apache.xalan.xslt.*; /** @@ -70,142 +71,219 @@ import org.apache.xalan.xslt.*; * <li>xsltfile * <li>outfile * <li>smart + * <li>dependent * </ul> * <p> * Of these arguments, <b>infile, outfile</b> and <b>xsltfile</b> are required. - * smart defaults to 'no'. The other allowed value is 'yes'. If smart is set to 'yes' + * <p>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. + * <p>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 + * they have been modified, initiate a restart of the xslt process, like external entities etc. * <p> * @author Fotis Jannidis <a href="mailto:fotis@jannidis.de">fotis@jannidis.de</a> */ public class Xslt extends Task { - private String infile, outfile, xsltfile; - private String smart = "no"; //defaults to do conversion everytime task is called + private String infile, outfile, xsltfile; + private String smart = "no"; //defaults to do conversion everytime task is called + private String dependent = "none"; //defaults to no dependencies + private boolean startXslt = false; - /** - * Calls Xalan and does the transformation - * - */ + /** + * Sets the input file + * + */ + public void setInfile (String infile) { + this.infile = infile; + } - private void transform(String xmlSourceURL, String xslURL, String outputURL) - throws java.io.IOException, - java.net.MalformedURLException, - org.xml.sax.SAXException { - - // Use XSLTProcessor to instantiate an XSLTProcessor. - org.apache.xalan.xslt.XSLTProcessor processor = - org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor(); - - // Create the 3 objects the XSLTProcessor needs to perform the transformation. - org.apache.xalan.xslt.XSLTInputSource xmlSource = - new org.apache.xalan.xslt.XSLTInputSource (infile); - org.apache.xalan.xslt.XSLTInputSource xslSheet = - new org.apache.xalan.xslt.XSLTInputSource (xsltfile); - 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("============================"); - processor.process(xmlSource, xslSheet, xmlResult); + /** + * 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: + * <ul> + * <li>yes: check whether output file is older than input or stylesheet + * <li>no: (default) do conversion everytime task is called + * </ul> + */ + public void setSmart (String smart) { + this.smart = smart; + } + +/** + * Sets the value for dependent + * + * @param option valid values: + * <ul> + * <li>none: (default) + * <li>comma delimited list of files whose existence and date is checked + * against the output file + * </ul> + */ + public void setDependent (String dependent) { + this.dependent = dependent; + } + + /** + * 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 + { + // Use XSLTProcessor to instantiate an XSLTProcessor. + org.apache.xalan.xslt.XSLTProcessor processor = + org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor(); + + // Create the 3 objects the XSLTProcessor needs to perform the transformation. + org.apache.xalan.xslt.XSLTInputSource xmlSource = + new org.apache.xalan.xslt.XSLTInputSource (infile); + org.apache.xalan.xslt.XSLTInputSource xslSheet = + new org.apache.xalan.xslt.XSLTInputSource (xsltfile); + 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("============================"); + processor.process(xmlSource, xslSheet, 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; } - - /** - * Sets the input file - * - */ - public void setInfile (String infile) { - this.infile = infile; - } + } 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; - /** - * 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: - * <ul> - * <li>yes: check whether output file is older than input or stylesheet - * <li>no: (default) do conversion everytime task is called - * </ul> - */ - public void setSmart (String smart) { - this.smart = smart; - } - + //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; + } - /** - * 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("SAX Error: " + saxerror); - } catch (MalformedURLException urlerror) { - System.out.println("URL Error: " + urlerror); - } catch (IOException ioerror) { - System.out.println("IO Error: " + ioerror); - } - } - - /** - * 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 { - if (smart.equals("no")) { - startTransform(); - } else if (smart.equals("yes")) { - File outfileF = new File (outfile); - //checks for existence of output file - if (outfileF.exists()) { - //checks whether output file is older than input file or xslt stylesheet file - if ((outfileF.lastModified() < new File(infile).lastModified()) | (outfileF.lastModified() < new -File(xsltfile).lastModified())) { - startTransform(); - } - } else { - startTransform(); - } - //returns error message, if smart has another value as 'yes' or 'no' - } else { - System.err.println("Error: Allowed values for the attribute smart are 'yes' or 'no'"); - } - - } - //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.execute(); - } - */ - + //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(); + } */ + } |