|
From: <mic...@us...> - 2003-11-12 22:18:08
|
Update of /cvsroot/babeldoc/babeldoc/modules/jfreereports/src/com/babeldoc/jfreereports/pipeline/stage
In directory sc8-pr-cvs1:/tmp/cvs-serv21577/modules/jfreereports/src/com/babeldoc/jfreereports/pipeline/stage
Added Files:
Tag: TEMP_MIKEA
JFRPipelineStage.java
Log Message:
This branch has an augmented jmx scanner, and a new pipeline stage for jfreereports.
--- 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/babeldoc/modules/jfreereports/src/com/babeldoc/jfreereports/pipeline/stage/Attic/JFRPipelineStage.java,v 1.1.2.1 2003/11/12 22:18:05 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 java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
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.jfree.report.JFreeReport;
import org.jfree.report.modules.parser.base.ReportGenerator;
import org.jfree.report.modules.output.pageable.pdf.PDFReportUtil;
/**
* @author mikea
*
*/
public class JFRPipelineStage extends PipelineStage {
/**
* constants
*/
public static final String XML_MIME_TYPE = "text/xml";
public final static String REPORT_DEF = "reportDefinition";
public final static String REPORT_DEF_URL = "reportDefinitionUrl";
/**
* Construct with this stages info
*/
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")));
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();
File f = new File(System.getProperties().get("java.io.tmpdir") + "/tmprpt.xml");
try {
FileWriter writer = new FileWriter(f);
writer.write(xmlReportDefinition);
writer.flush();
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
// Set up the table model
TableModel tm = getTableModel();
ReportGenerator gen = ReportGenerator.getInstance();
JFreeReport report = null;
try {
report = gen.parseReport(f);
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (org.jfree.xml.ElementDefinitionException e2) {
e2.printStackTrace();
}
report.setData(tm);
PDFReportUtil.createPDF(report, System.getProperties().get("java.io.tmpdir") + "/output.pdf");
// TODO Auto-generated method stub
return null;
}
/*
* 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) {
strReportDefinitionUrl = (String) getDocument().get(REPORT_DEF_URL);
if (strReportDefinitionUrl != null) {
URL urlReportDefinitionUrl;
try {
urlReportDefinitionUrl = new URL(strReportDefinitionUrl);
xmlReportDefinition = (String) urlReportDefinitionUrl.getContent();
} catch (java.net.MalformedURLException e) {
throw new PipelineException(I18n.get("jfr.102"), e);
} catch (IOException e) {
throw new PipelineException(I18n.get("jfr.103"), e);
}
}
}
// If still unsuccessful, then try the stage definition for a report definition
if (xmlReportDefinition == null) {
xmlReportDefinition = (String) this.getInfo().getOption(REPORT_DEF).getValue();
}
// Otherwise a report definition URL from the stage definition
if (xmlReportDefinition == null) {
strReportDefinitionUrl = (String) this.getInfo().getOption(REPORT_DEF_URL).getValue();
if (strReportDefinitionUrl != null) {
URL urlReportDefinitionUrl;
try {
urlReportDefinitionUrl = new URL(strReportDefinitionUrl);
xmlReportDefinition = (String) urlReportDefinitionUrl.getContent();
} catch (java.net.MalformedURLException e) {
throw new PipelineException(I18n.get("jfr.102"), e);
} catch (IOException e) {
throw new PipelineException(I18n.get("jfr.103"), e);
}
}
}
return (xmlReportDefinition);
}
private TableModel getTableModel() {
DefaultTableModel tm = new DefaultTableModel();
Document doc;
try {
doc = DocumentHelper.parseText(new String(getDocument().getBytes()));
} catch (DocumentException e) {
LogService.getInstance().logError(e);
return (tm);
}
Element queryResults = (Element) doc.selectSingleNode( "/queryresults/query" );
// iterate through child elements of root with element name "foo"
ArrayList rowData = new ArrayList();
boolean needHeaders = true;
for ( Iterator i = queryResults.elementIterator( "row" ); i.hasNext(); ) {
Element row = (Element) i.next();
int pos = 0;
for ( Iterator j = queryResults.elementIterator( "column" ); j.hasNext(); ) {
Element cell = (Element) j.next();
// Add the column to the table model, if this is the first row being constructed
if (needHeaders) {
String columnName = cell.attribute("column-name").toString();
if (tm.findColumn(columnName) == -1) {
tm.addColumn(columnName);
}
}
// Add the cell value to the rowData array
Object v;
try {
Class c = Class.forName(cell.attribute("column-class").toString()).getClass();
Constructor cons = c.getConstructor(new Class[] {String.class});
v = cons.newInstance(new Object[] {cell.getText()});
} catch (Exception e) {
LogService.getInstance().logWarn(e.getMessage());
v = new String("[" + I18n.get("jfr.104", cell.attribute("column-class").toString()) + "]");
}
rowData.add(pos, v);
pos++;
}
needHeaders = false;
tm.addRow(rowData.toArray());
}
return (tm);
}
}
|