|
From: <mic...@us...> - 2003-12-03 18:54:10
|
Update of /cvsroot/babeldoc/modules/jfreereports/src/com/babeldoc/jfreereports/pipeline/stage In directory sc8-pr-cvs1:/tmp/cvs-serv28497/jfreereports/src/com/babeldoc/jfreereports/pipeline/stage Added Files: JFRPipelineStage.java Log Message: Initial commit after moving to modules directory --- NEW FILE: JFRPipelineStage.java --- /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * 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. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" 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 ap...@ap.... * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. * ==================================================================== * * Babeldoc: The Universal Document Processor * * $Header: /cvsroot/babeldoc/modules/jfreereports/src/com/babeldoc/jfreereports/pipeline/stage/JFRPipelineStage.java,v 1.1 2003/12/03 18:54:06 michaelansley Exp $ * $DateTime$ * $Author: michaelansley $ * */ package com.babeldoc.jfreereports.pipeline.stage; import com.babeldoc.core.I18n; import com.babeldoc.core.LogService; import com.babeldoc.core.NameValuePair; import com.babeldoc.core.option.ComplexConfigOptionType; import com.babeldoc.core.option.ConfigOption; import com.babeldoc.core.option.IConfigOptionType; import com.babeldoc.core.pipeline.PipelineDocument; import com.babeldoc.core.pipeline.PipelineException; import com.babeldoc.core.pipeline.PipelineStage; import com.babeldoc.core.pipeline.PipelineStageInfo; import com.babeldoc.core.pipeline.PipelineStageResult; import com.babeldoc.core.resource.IResource; import com.babeldoc.core.resource.ResourceException; import com.babeldoc.core.resource.ResourceFactory; import com.babeldoc.utils.XMLTableModel; import java.io.BufferedInputStream; import java.io.IOException; import java.io.File; import java.io.FileWriter; import java.io.StringWriter; import java.lang.reflect.Constructor; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.io.FileInputStream; import java.io.InputStream; import java.io.StringReader; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.dom.DOMDocument; import org.jfree.report.JFreeReport; import org.jfree.report.ReportProcessingException; import org.jfree.report.modules.parser.base.ReportGenerator; import org.jfree.report.modules.output.pageable.pdf.PDFReportUtil; import org.jfree.report.modules.output.table.csv.CSVReportUtil; import org.jfree.report.modules.output.table.html.HTMLReportUtil; import org.jfree.report.modules.output.table.rtf.RTFReportUtil; import org.xml.sax.InputSource; /** * Make a JFreeReport from SqlQuery output. * * @author michaelansley * @version 1.0 **/ public class JFRPipelineStage extends PipelineStage { private static LogService log = LogService.getInstance(JFRPipelineStage.class.getName()); /** constant: mime type for XML */ public static final String XML_MIME_TYPE = "text/xml"; /** constant: Report definition attribute/config property */ public final static String REPORT_DEF = "reportDefinition"; /** constant: Report definition URL attribute/config property */ public final static String REPORT_DEF_URL = "reportDefinitionUrl"; /** constant: Report output type property */ public final static String REPORT_OUTPUT_FORMAT = "reportOutputFormat"; /** constant: Report output type HTML */ public final static int OUTPUT_FORMAT_HTML = 0; /** constant: Report output type PDF */ public final static int OUTPUT_FORMAT_PDF = 1; /** constant: Report output type CSV */ public final static int OUTPUT_FORMAT_CSV = 2; /** constant: Report output type Excel */ public final static int OUTPUT_FORMAT_XLS = 3; /** constant: Report output type Rich Text Format */ public final static int OUTPUT_FORMAT_RTF = 4; /** instance: Counter for unique file numbers */ private static int tempFileCount = 0; /** * Construct the stage information for this stage * */ public JFRPipelineStage() { super(new PipelineStageInfo() { public String getName() { return "JFreeReport"; } public String getDescription() { return I18n.get("jfr.001"); } public Collection getTypeSpecificOptions() { ArrayList options = new ArrayList(); options.add(new ConfigOption(REPORT_DEF, IConfigOptionType.URL, null, true, I18n.get("jfr.002"))); options.add(new ConfigOption(REPORT_DEF_URL, IConfigOptionType.URL, null, true, I18n.get("jfr.003"))); options.add(new ConfigOption(REPORT_OUTPUT_FORMAT, IConfigOptionType.URL, null, true, I18n.get("jfr.004"))); return options; } }); } /* (non-Javadoc) * @see com.babeldoc.core.pipeline.PipelineStage#process() */ public PipelineStageResult[] process() throws PipelineException { if (getDocument().getMimeType() != XML_MIME_TYPE) { throw new PipelineException(I18n.get("jfr.101")); } // Find the report definition String xmlReportDefinition = getReportDefinition(); JFreeReport report = null; String outputFile = null; try { log.logDebug("Get the table model"); TableModel tm = getTableModel(); log.logDebug("Create a report with the correct definition"); log.logDebug(xmlReportDefinition); StringReader reader = new StringReader (xmlReportDefinition); URL contentBaseURL = new URL ("file:///"); report = ReportGenerator.getInstance().parseReport(new InputSource(reader), contentBaseURL); log.logDebug("Set the data for the report"); report.setData(tm); log.logDebug("Get the required output type"); int outputFormat = getOutputFormat(); outputFile = getTempFilename(); log.logDebug("Generate the report output"); switch (outputFormat) { case OUTPUT_FORMAT_PDF: { PDFReportUtil.createPDF(report, outputFile); break; } case OUTPUT_FORMAT_HTML: { try { HTMLReportUtil.createStreamHTML(report, outputFile); } catch (org.jfree.report.function.FunctionInitializeException e1) { log.logError(e1); } catch (java.io.IOException e2) { log.logError(e2); } catch (org.jfree.report.ReportProcessingException e3) { log.logError(e3); } break; } case OUTPUT_FORMAT_CSV: { try { CSVReportUtil.createCSV(report, outputFile); } catch (org.jfree.report.function.FunctionInitializeException e1) { log.logError(e1); } catch (java.io.IOException e2) { log.logError(e2); } catch (org.jfree.report.ReportProcessingException e3) { log.logError(e3); } break; } case OUTPUT_FORMAT_RTF: { try { RTFReportUtil.createRTF(report, outputFile); } catch (org.jfree.report.function.FunctionInitializeException e1) { log.logError(e1); } catch (java.io.IOException e2) { log.logError(e2); } catch (org.jfree.report.ReportProcessingException e3) { log.logError(e3); } break; } default: { try { HTMLReportUtil.createStreamHTML(report, outputFile); } catch (org.jfree.report.function.FunctionInitializeException e1) { log.logError(e1); } catch (java.io.IOException e2) { log.logError(e2); } catch (org.jfree.report.ReportProcessingException e3) { log.logError(e3); } break; } } } catch (java.net.MalformedURLException e1) { // From new URL() log.logError(e1); throw new PipelineException(I18n.get("jfr.105"), e1); } catch (org.jfree.xml.ElementDefinitionException e2) { // From parseReport() log.logError(e2); throw new PipelineException(I18n.get("jfr.106"), e2); } catch (DocumentException e3) { // From getTableModel() log.logError(e3); throw new PipelineException(I18n.get("jfr.107"), e3); } log.logDebug("Create the pipeline document from the temp file"); PipelineDocument newDocument = null; if (outputFile != null && !(outputFile.equals(""))) { try { byte[] data = getBytesFromFile(new File(outputFile)); newDocument = new PipelineDocument(this.getDocument(), data); newDocument.setBinary(true); newDocument.setMimeType("application/pdf"); new File(outputFile).delete(); } catch (java.io.IOException e) { log.logError(e); throw new PipelineException(I18n.get("jfr.108"), e); } } return super.processHelper(newDocument); } /** * Find the report definition. First search the document attributes for a * report definition, then a report definition URL. Then check the pipeline * stage. * * @return String The report definition * @throws ResourceException Any exceptions encountered are converted to * ResourceException * */ private String getReportDefinition() throws PipelineException { String xmlReportDefinition; String strReportDefinitionUrl; // Try to get the report definition directly from the document attributes xmlReportDefinition = (String) getDocument().get(REPORT_DEF); // If unsuccessful, then try for a report definition URL from the document attributes if (xmlReportDefinition == null) { log.logDebug("No report definition document attribute, trying for URL document attribute"); strReportDefinitionUrl = (String) getDocument().get(REPORT_DEF_URL); if (strReportDefinitionUrl != null) { log.logDebug("Report definition URL found in document attribute"); URL urlReportDefinitionUrl; try { xmlReportDefinition = getStringFromFile(new File(strReportDefinitionUrl)); } catch (IOException e) { throw new PipelineException(I18n.get("jfr.103"), e); } } else { log.logDebug("Report definition URL not found in document attribute"); } } else { log.logDebug("Report definition found in document attribute"); } // If still unsuccessful, then try the stage definition for a report definition if (xmlReportDefinition == null) { log.logDebug("Trying for report definition in stage option"); xmlReportDefinition = (String) this.getInfo().getOption(REPORT_DEF).getValue(); } // Otherwise a report definition URL from the stage definition if (xmlReportDefinition == null) { log.logDebug("Try for report definition URL in stage options"); strReportDefinitionUrl = (String) this.getInfo().getOption(REPORT_DEF_URL).getValue(); if (strReportDefinitionUrl != null) { URL urlReportDefinitionUrl; try { log.logDebug("Report definition URL found in stage options"); xmlReportDefinition = getStringFromFile(new File(strReportDefinitionUrl)); } catch (java.net.MalformedURLException e) { throw new PipelineException(I18n.get("jfr.102"), e); } catch (IOException e) { throw new PipelineException(I18n.get("jfr.103"), e); } } } else { log.logDebug("Report definition found in stage options"); } return (xmlReportDefinition); } private TableModel getTableModel() throws DocumentException { Document doc; XMLTableModel tm = new XMLTableModel(); try { doc = DocumentHelper.parseText(new String(getDocument().getBytes())); } catch (DocumentException e) { LogService.getInstance().logError(e); return (tm); } tm.setDocument(doc, "/queryresults/query"); return (tm); } // Returns the contents of the file in a byte array. public byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); log.logInfo("File size: " + length); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } // Close the input stream and return bytes is.close(); return bytes; } private String getTempFilename() { return (System.getProperty("java.io.tmpdir") + "/tempfile" + getFileCounter()); } private String getStringFromFile(File file) throws java.io.IOException { String s = null; try { BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[in.available()]; in.read(b, 0, b.length); s = new String(b, 0, b.length); in.close(); } catch (java.io.FileNotFoundException e1) { s = ""; } return (s); } private synchronized int getFileCounter() { return (++tempFileCount); } private int getOutputFormat() { int result = OUTPUT_FORMAT_PDF; String strReportOutputFormat = (String) getDocument().get(REPORT_OUTPUT_FORMAT); if ((strReportOutputFormat == null) || (strReportOutputFormat.equals(""))) { strReportOutputFormat = (String) this.getInfo().getOption(REPORT_OUTPUT_FORMAT).getValue(); } if (strReportOutputFormat == null) {strReportOutputFormat = ""; } if (strReportOutputFormat.equals("pdf")) { result = OUTPUT_FORMAT_PDF; } else if (strReportOutputFormat.equals("html")) { result = OUTPUT_FORMAT_HTML; } else if (strReportOutputFormat.equals("csv")) { result = OUTPUT_FORMAT_CSV; } else if (strReportOutputFormat.equals("xls")) { result = OUTPUT_FORMAT_XLS; } else if (strReportOutputFormat.equals("rtf")) { result = OUTPUT_FORMAT_RTF; } else { result = OUTPUT_FORMAT_HTML; } return (result); } } |