001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * SQLDataFactoryWriteHandler.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.parser.extwriter.sql;
030:
031: import java.io.IOException;
032:
033: import org.jfree.report.JFreeReport;
034: import org.jfree.report.JFreeReportBoot;
035: import org.jfree.report.modules.misc.datafactory.sql.ConnectionProvider;
036: import org.jfree.report.modules.misc.datafactory.sql.SQLReportDataFactory;
037: import org.jfree.report.modules.parser.extwriter.DataFactoryWriteHandler;
038: import org.jfree.report.modules.parser.extwriter.ReportWriterContext;
039: import org.jfree.report.modules.parser.extwriter.ReportWriterException;
040: import org.jfree.util.Configuration;
041: import org.jfree.util.ObjectUtilities;
042: import org.jfree.xmlns.common.AttributeList;
043: import org.jfree.xmlns.writer.XmlWriter;
044:
045: /**
046: * Creation-Date: Jan 19, 2007, 4:44:05 PM
047: *
048: * @author Thomas Morgner
049: */
050: public class SQLDataFactoryWriteHandler implements
051: DataFactoryWriteHandler {
052: public static final String PREFIX = "org.jfree.report.modules.parser.extwriter.handler.sql-connection-provider.";
053: public static final String NAMESPACE = "http://jfreereport.sourceforge.net/namespaces/datasources/sql";
054:
055: public SQLDataFactoryWriteHandler() {
056: }
057:
058: public void write(final ReportWriterContext reportWriter,
059: final XmlWriter xmlWriter) throws IOException,
060: ReportWriterException {
061: final JFreeReport report = (JFreeReport) reportWriter
062: .getReport();
063: final SQLReportDataFactory dataFactory = (SQLReportDataFactory) report
064: .getDataFactory();
065:
066: final AttributeList rootAttrs = new AttributeList();
067: rootAttrs.addNamespaceDeclaration("data", NAMESPACE);
068: xmlWriter.writeTag(NAMESPACE, "sql-datasource", rootAttrs,
069: XmlWriter.OPEN);
070: xmlWriter
071: .writeTag(NAMESPACE, "config", "label-mapping", String
072: .valueOf(dataFactory.isLabelMapping()),
073: XmlWriter.CLOSE);
074:
075: writeConnectionInfo(reportWriter, xmlWriter, dataFactory
076: .getConnectionProvider());
077:
078: final String[] queryNames = dataFactory.getQueryNames();
079: for (int i = 0; i < queryNames.length; i++) {
080: final String queryName = queryNames[i];
081: final String query = dataFactory.getQuery(queryName);
082: xmlWriter.writeTag(NAMESPACE, "query", "name", queryName,
083: XmlWriter.OPEN);
084: xmlWriter.writeText(XmlWriter.normalize(query, false));
085: xmlWriter.writeCloseTag();
086: }
087: xmlWriter.writeCloseTag();
088: }
089:
090: private void writeConnectionInfo(
091: final ReportWriterContext reportWriter,
092: final XmlWriter xmlWriter,
093: final ConnectionProvider connectionProvider)
094: throws IOException, ReportWriterException {
095: final String configKey = PREFIX
096: + connectionProvider.getClass().getName();
097: final Configuration globalConfig = JFreeReportBoot
098: .getInstance().getGlobalConfig();
099: final String value = globalConfig.getConfigProperty(configKey);
100: if (value != null) {
101: final ConnectionProviderWriteHandler handler = (ConnectionProviderWriteHandler) ObjectUtilities
102: .loadAndInstantiate(value,
103: SQLReportDataFactory.class,
104: ConnectionProviderWriteHandler.class);
105: if (handler != null) {
106: handler.write(reportWriter, xmlWriter,
107: connectionProvider);
108: }
109: }
110:
111: }
112: }
|