001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.datasetplugin;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.jfree.report.DataFactory;
019: import org.jfree.report.DataRow;
020: import org.jfree.report.ReportDataFactoryException;
021: import org.pentaho.reportdesigner.crm.report.datasetplugin.multidataset.XPathTableModel;
022:
023: import javax.swing.table.TableModel;
024: import javax.xml.xpath.XPathExpressionException;
025: import java.io.IOException;
026: import java.net.URL;
027:
028: /**
029: * User: Martin
030: * Date: 06.03.2007
031: * Time: 17:52:39
032: */
033: public class XPathDataFactory implements ExtendedDataFactory {
034: @NotNull
035: private String queryName;
036: @NotNull
037: private URL xmlResource;
038: @NotNull
039: private String xPathExpression;
040: private boolean limitRows;
041: private int maxRows;
042:
043: public XPathDataFactory(@NotNull
044: String queryName, @NotNull
045: URL xmlResource, @NotNull
046: String xPathExpression, boolean limitRows, int maxRows) {
047: this .limitRows = limitRows;
048: this .maxRows = maxRows;
049: //noinspection ConstantConditions
050: if (queryName == null) {
051: throw new IllegalArgumentException(
052: "queryName must not be null");
053: }
054: //noinspection ConstantConditions
055: if (xmlResource == null) {
056: throw new IllegalArgumentException(
057: "xmlResource must not be null");
058: }
059: //noinspection ConstantConditions
060: if (xPathExpression == null) {
061: throw new IllegalArgumentException(
062: "xPathExpression must not be null");
063: }
064: this .queryName = queryName;
065: this .xmlResource = xmlResource;
066: this .xPathExpression = xPathExpression;
067: }
068:
069: public boolean canExecuteQuery(@Nullable
070: String query) {
071: return queryName.equals(query);
072: }
073:
074: public void close() {
075: }
076:
077: @NotNull
078: public DataFactory derive() throws ReportDataFactoryException {
079: return this ;
080: }
081:
082: public void open() {
083: }
084:
085: @NotNull
086: public TableModel queryData(@Nullable
087: final String query, @Nullable
088: final DataRow parameters) throws ReportDataFactoryException {
089: XPathTableModel xPathTableModel;
090: try {
091: xPathTableModel = new XPathTableModel(xmlResource,
092: xPathExpression, parameters, limitRows, maxRows);
093: } catch (IOException e) {
094: throw new ReportDataFactoryException(e.getMessage(), e);
095: } catch (XPathExpressionException e) {
096: throw new ReportDataFactoryException(e.getMessage(), e);
097: }
098: return xPathTableModel;
099: }
100:
101: }
|