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.fill;
029:
030: import java.io.File;
031: import java.io.InputStream;
032: import java.net.URL;
033:
034: import net.sf.jasperreports.engine.JRException;
035: import net.sf.jasperreports.engine.JRExpression;
036: import net.sf.jasperreports.engine.JRReportTemplate;
037: import net.sf.jasperreports.engine.JRRuntimeException;
038: import net.sf.jasperreports.engine.JRTemplate;
039: import net.sf.jasperreports.engine.xml.JRXmlTemplateLoader;
040:
041: /**
042: * Fill-time {@link JRReportTemplate} implementation.
043: * <p/>
044: * Used to evaluate template source expressions.
045: *
046: * @author Lucian Chirita (lucianc@users.sourceforge.net)
047: * @version $Id: JRFillReportTemplate.java 1759 2007-06-20 16:47:34Z lucianc $
048: */
049: public class JRFillReportTemplate implements JRReportTemplate {
050:
051: private final JRReportTemplate parent;
052: private final JRBaseFiller filler;
053:
054: public JRFillReportTemplate(JRReportTemplate template,
055: JRBaseFiller filler, JRFillObjectFactory factory) {
056: factory.put(template, this );
057:
058: parent = template;
059: this .filler = filler;
060: }
061:
062: public JRExpression getSourceExpression() {
063: return parent.getSourceExpression();
064: }
065:
066: public JRTemplate evaluate() throws JRException {
067: JRTemplate template;
068: JRExpression sourceExpression = getSourceExpression();
069: Object source = filler.evaluateExpression(sourceExpression,
070: JRExpression.EVALUATION_DEFAULT);
071: if (source == null) {
072: template = null;
073: } else {
074: Class sourceType = sourceExpression.getValueClass();
075: if (JRTemplate.class.isAssignableFrom(sourceType)) {
076: template = (JRTemplate) source;
077: } else {
078: template = loadTemplate(source, sourceType,
079: filler.fillContext);
080: }
081: }
082: return template;
083: }
084:
085: protected static JRTemplate loadTemplate(Object source,
086: Class sourceType, JRFillContext fillContext)
087: throws JRException {
088: JRTemplate template;
089: if (fillContext.hasLoadedTemplate(source)) {
090: template = fillContext.getLoadedTemplate(source);
091: } else {
092: if (String.class.equals(sourceType)) {
093: template = JRXmlTemplateLoader.load((String) source);
094: } else if (File.class.isAssignableFrom(sourceType)) {
095: template = JRXmlTemplateLoader.load((File) source);
096: } else if (URL.class.isAssignableFrom(sourceType)) {
097: template = JRXmlTemplateLoader.load((URL) source);
098: } else if (InputStream.class.isAssignableFrom(sourceType)) {
099: template = JRXmlTemplateLoader
100: .load((InputStream) source);
101: } else {
102: throw new JRRuntimeException(
103: "Unknown template source class "
104: + sourceType.getName());
105: }
106:
107: fillContext.registerLoadedTemplate(source, template);
108: }
109: return template;
110: }
111:
112: }
|