KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > compilers > JRBshCompiler


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.compilers;
29
30 import java.io.File JavaDoc;
31 import java.io.Serializable JavaDoc;
32
33 import net.sf.jasperreports.engine.JRException;
34 import net.sf.jasperreports.engine.JRReport;
35 import net.sf.jasperreports.engine.design.JRAbstractCompiler;
36 import net.sf.jasperreports.engine.design.JRCompilationUnit;
37 import net.sf.jasperreports.engine.design.JRSourceCompileTask;
38 import net.sf.jasperreports.engine.fill.JRCalculator;
39 import net.sf.jasperreports.engine.fill.JREvaluator;
40
41
42 /**
43  * @author Teodor Danciu (teodord@users.sourceforge.net)
44  * @version $Id: JRBshCompiler.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
45  */

46 public class JRBshCompiler extends JRAbstractCompiler
47 {
48
49
50     public JRBshCompiler()
51     {
52         super(false);
53     }
54
55
56     protected JREvaluator loadEvaluator(Serializable JavaDoc compileData, String JavaDoc unitName) throws JRException
57     {
58         return new JRBshEvaluator((String JavaDoc) compileData);
59     }
60
61
62     protected void checkLanguage(String JavaDoc language) throws JRException
63     {
64         if (!JRReport.LANGUAGE_JAVA.equals(language))
65         {
66             throw
67                 new JRException(
68                     "Language \"" + language
69                     + "\" not supported by this report compiler.\n"
70                     + "Expecting \"java\" instead."
71                     );
72         }
73     }
74
75
76     protected String JavaDoc generateSourceCode(JRSourceCompileTask sourceTask) throws JRException
77     {
78         return JRBshGenerator.generateScript(sourceTask);
79     }
80
81
82     protected String JavaDoc compileUnits(JRCompilationUnit[] units, String JavaDoc classpath, File JavaDoc tempDirFile) throws JRException
83     {
84         verifyScripts(units);
85
86         for (int i = 0; i < units.length; i++)
87         {
88             String JavaDoc script = units[i].getSourceCode();
89             units[i].setCompileData(script);
90         }
91         
92         return null;
93     }
94
95
96     private void verifyScripts(JRCompilationUnit[] units) throws JRException
97     {
98         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
99         
100         // trick for detecting the Ant class loader
101
try
102         {
103             classLoader.loadClass(JRCalculator.class.getName());
104         }
105         catch(ClassNotFoundException JavaDoc e)
106         {
107             classLoader = getClass().getClassLoader();
108         }
109
110         ClassLoader JavaDoc oldContextClassLoader = Thread.currentThread().getContextClassLoader();
111         Thread.currentThread().setContextClassLoader(classLoader);
112
113         for (int i = 0; i < units.length; i++)
114         {
115             String JavaDoc script = units[i].getSourceCode();
116             
117             JRBshEvaluator bshEvaluator = new JRBshEvaluator(script);
118             bshEvaluator.verify(units[i].getExpressions());
119         }
120
121         Thread.currentThread().setContextClassLoader(oldContextClassLoader);
122     }
123
124
125     protected String JavaDoc getSourceFileName(String JavaDoc unitName)
126     {
127         return unitName + ".bsh";
128     }
129
130
131 }
132
Popular Tags