001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2005 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: * 185, Berry Street, Suite 6200
025: * San Francisco CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.compilers;
029:
030: import java.io.File;
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: import net.sf.jasperreports.engine.JRException;
035: import net.sf.jasperreports.engine.JRReport;
036: import net.sf.jasperreports.engine.JasperReport;
037: import net.sf.jasperreports.engine.design.JRCompiler;
038: import net.sf.jasperreports.engine.design.JRVerifier;
039: import net.sf.jasperreports.engine.design.JasperDesign;
040: import net.sf.jasperreports.engine.fill.JRCalculator;
041: import net.sf.jasperreports.engine.util.JRProperties;
042: import net.sf.jasperreports.engine.util.JRSaver;
043:
044: /**
045: * @author Teodor Danciu (teodord@users.sourceforge.net)
046: * @version $Id: JRBshCompiler.java,v 1.1 2007/08/15 23:26:01 suricate Exp $
047: */
048: public class JRBshCompiler implements JRCompiler {
049:
050: /**
051: *
052: */
053: public JasperReport compileReport(JasperDesign jasperDesign)
054: throws JRException {
055: JasperReport jasperReport = null;
056:
057: if (!JRReport.LANGUAGE_JAVA.equals(jasperDesign.getLanguage())) {
058: throw new JRException("Language \""
059: + jasperDesign.getLanguage()
060: + "\" not supported by this report compiler.\n"
061: + "Expecting \"java\" instead.");
062: }
063:
064: Collection brokenRules = JRVerifier.verifyDesign(jasperDesign);
065: if (brokenRules != null && brokenRules.size() > 0) {
066: StringBuffer sbuffer = new StringBuffer();
067: sbuffer.append("Report design not valid : ");
068: int i = 1;
069: for (Iterator it = brokenRules.iterator(); it.hasNext(); i++) {
070: sbuffer.append("\n\t " + i + ". " + (String) it.next());
071: }
072: throw new JRException(sbuffer.toString());
073: } else {
074: //Report design OK
075:
076: //Generating BeanShell script for report expressions
077: String bshScript = JRBshGenerator
078: .generateScript(jasperDesign);
079:
080: boolean isKeepJavaFile = JRProperties
081: .getBooleanProperty(JRProperties.COMPILER_KEEP_JAVA_FILE);
082:
083: if (isKeepJavaFile) {
084: String tempDirStr = JRProperties
085: .getProperty(JRProperties.COMPILER_TEMP_DIR);
086:
087: File tempDirFile = new File(tempDirStr);
088: if (!tempDirFile.exists() || !tempDirFile.isDirectory()) {
089: throw new JRException(
090: "Temporary directory not found : "
091: + tempDirStr);
092: }
093:
094: File javaFile = new File(tempDirFile, jasperDesign
095: .getName()
096: + ".bsh");
097:
098: JRSaver.saveClassSource(bshScript, javaFile);
099: }
100:
101: jasperReport = new JasperReport(jasperDesign, getClass()
102: .getName(), bshScript);
103:
104: /* */
105: verifyScript(jasperDesign, bshScript);
106: }
107:
108: return jasperReport;
109: }
110:
111: /**
112: *
113: */
114: private void verifyScript(JasperDesign jasperDesign,
115: String bshScript) throws JRException {
116: ClassLoader classLoader = Thread.currentThread()
117: .getContextClassLoader();
118:
119: // trick for detecting the Ant class loader
120: try {
121: classLoader.loadClass(JRCalculator.class.getName());
122: } catch (ClassNotFoundException e) {
123: classLoader = getClass().getClassLoader();
124: }
125:
126: ClassLoader oldContextClassLoader = Thread.currentThread()
127: .getContextClassLoader();
128: Thread.currentThread().setContextClassLoader(classLoader);
129:
130: JRBshCalculator bshCalculator = new JRBshCalculator(bshScript);
131: bshCalculator.verify(jasperDesign.getExpressions());
132:
133: Thread.currentThread().setContextClassLoader(
134: oldContextClassLoader);
135: }
136:
137: /**
138: *
139: */
140: public JRCalculator loadCalculator(JasperReport jasperReport)
141: throws JRException {
142: return new JRBshCalculator((String) jasperReport
143: .getCompileData());
144: }
145:
146: }
|