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.compilers;
029:
030: import java.io.File;
031: import java.io.Serializable;
032:
033: import net.sf.jasperreports.engine.JRException;
034: import net.sf.jasperreports.engine.JRReport;
035: import net.sf.jasperreports.engine.design.JRAbstractCompiler;
036: import net.sf.jasperreports.engine.design.JRCompilationSourceCode;
037: import net.sf.jasperreports.engine.design.JRCompilationUnit;
038: import net.sf.jasperreports.engine.design.JRDefaultCompilationSourceCode;
039: import net.sf.jasperreports.engine.design.JRSourceCompileTask;
040: import net.sf.jasperreports.engine.fill.JRCalculator;
041: import net.sf.jasperreports.engine.fill.JREvaluator;
042:
043: /**
044: * @author Teodor Danciu (teodord@users.sourceforge.net)
045: * @version $Id: JRBshCompiler.java 1828 2007-08-24 13:58:43Z teodord $
046: */
047: public class JRBshCompiler extends JRAbstractCompiler {
048:
049: /**
050: * A constant used to specify that the language used by expressions is BeanShell script.
051: */
052: public static final String LANGUAGE_BSH = "bsh";
053:
054: public JRBshCompiler() {
055: super (false);
056: }
057:
058: protected JREvaluator loadEvaluator(Serializable compileData,
059: String unitName) throws JRException {
060: return new JRBshEvaluator((String) compileData);
061: }
062:
063: protected void checkLanguage(String language) throws JRException {
064: if (!LANGUAGE_BSH.equals(language)
065: && !JRReport.LANGUAGE_JAVA.equals(language)) {
066: throw new JRException("Language \"" + language
067: + "\" not supported by this report compiler.\n"
068: + "Expecting \"bsh\" or \"java\" instead.");
069: }
070: }
071:
072: protected JRCompilationSourceCode generateSourceCode(
073: JRSourceCompileTask sourceTask) throws JRException {
074: return new JRDefaultCompilationSourceCode(JRBshGenerator
075: .generateScript(sourceTask), null);
076: }
077:
078: protected String compileUnits(JRCompilationUnit[] units,
079: String classpath, File tempDirFile) throws JRException {
080: verifyScripts(units);
081:
082: for (int i = 0; i < units.length; i++) {
083: String script = units[i].getSourceCode();
084: units[i].setCompileData(script);
085: }
086:
087: return null;
088: }
089:
090: private void verifyScripts(JRCompilationUnit[] units)
091: throws JRException {
092: ClassLoader classLoader = Thread.currentThread()
093: .getContextClassLoader();
094:
095: // trick for detecting the Ant class loader
096: try {
097: classLoader.loadClass(JRCalculator.class.getName());
098: } catch (ClassNotFoundException e) {
099: classLoader = getClass().getClassLoader();
100: }
101:
102: ClassLoader oldContextClassLoader = Thread.currentThread()
103: .getContextClassLoader();
104: Thread.currentThread().setContextClassLoader(classLoader);
105:
106: for (int i = 0; i < units.length; i++) {
107: String script = units[i].getSourceCode();
108:
109: JRBshEvaluator bshEvaluator = new JRBshEvaluator(script);
110: bshEvaluator.verify(units[i].getExpressions());
111: }
112:
113: Thread.currentThread().setContextClassLoader(
114: oldContextClassLoader);
115: }
116:
117: protected String getSourceFileName(String unitName) {
118: return unitName + ".bsh";
119: }
120:
121: }
|