001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * 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,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.query;
029:
030: import java.util.Locale;
031: import java.util.Map;
032: import java.util.TimeZone;
033:
034: import net.sf.jasperreports.engine.JRDataSource;
035: import net.sf.jasperreports.engine.JRDataset;
036: import net.sf.jasperreports.engine.JRException;
037: import net.sf.jasperreports.engine.data.JRXmlDataSource;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.w3c.dom.Document;
042:
043: /**
044: * XPath query executer implementation.
045: * <p/>
046: * The XPath query of the report is executed against the document specified by the
047: * {@link net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory#PARAMETER_XML_DATA_DOCUMENT PARAMETER_XML_DATA_DOCUMENT}
048: * parameter.
049: * <p/>
050: * All the paramters in the XPath query are replaced by calling <code>String.valueOf(Object)</code>
051: * on the parameter value.
052: *
053: * @author Lucian Chirita (lucianc@users.sourceforge.net)
054: * @version $Id: JRXPathQueryExecuter.java 1737 2007-06-04 15:18:39Z teodord $
055: */
056: public class JRXPathQueryExecuter extends JRAbstractQueryExecuter {
057: private static final Log log = LogFactory
058: .getLog(JRXPathQueryExecuter.class);
059:
060: private final Document document;
061:
062: public JRXPathQueryExecuter(JRDataset dataset, Map parametersMap) {
063: super (dataset, parametersMap);
064:
065: document = (Document) getParameterValue(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT);
066:
067: if (document == null) {
068: log
069: .warn("The supplied org.w3c.dom.Document object is null.");
070: }
071:
072: parseQuery();
073: }
074:
075: protected String getParameterReplacement(String parameterName) {
076: return String.valueOf(getParameterValue(parameterName));
077: }
078:
079: public JRDataSource createDatasource() throws JRException {
080: JRXmlDataSource datasource = null;
081:
082: String xPath = getQueryString();
083:
084: if (log.isDebugEnabled()) {
085: log.debug("XPath query: " + xPath);
086: }
087:
088: if (document != null && xPath != null) {
089: datasource = new JRXmlDataSource(document, xPath);
090: datasource.setLocale((Locale) getParameterValue(
091: JRXPathQueryExecuterFactory.XML_LOCALE, true));
092: datasource
093: .setDatePattern((String) getParameterValue(
094: JRXPathQueryExecuterFactory.XML_DATE_PATTERN,
095: true));
096: datasource.setNumberPattern((String) getParameterValue(
097: JRXPathQueryExecuterFactory.XML_NUMBER_PATTERN,
098: true));
099: datasource.setTimeZone((TimeZone) getParameterValue(
100: JRXPathQueryExecuterFactory.XML_TIME_ZONE, true));
101: }
102:
103: return datasource;
104: }
105:
106: public void close() {
107: //nothing to do
108: }
109:
110: public boolean cancelQuery() throws JRException {
111: //nothing to cancel
112: return false;
113: }
114:
115: }
|