KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > design > JRDefaultCompiler


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.engine.design;
29
30 import net.sf.jasperreports.crosstabs.JRCrosstab;
31 import net.sf.jasperreports.engine.JRDataset;
32 import net.sf.jasperreports.engine.JRException;
33 import net.sf.jasperreports.engine.JRReport;
34 import net.sf.jasperreports.engine.JasperReport;
35 import net.sf.jasperreports.engine.fill.JREvaluator;
36 import net.sf.jasperreports.engine.util.JRClassLoader;
37 import net.sf.jasperreports.engine.util.JRProperties;
38
39
40 /**
41  * @author Teodor Danciu (teodord@users.sourceforge.net)
42  * @version $Id: JRDefaultCompiler.java 1282 2006-06-07 17:44:40 +0300 (Wed, 07 Jun 2006) teodord $
43  */

44 public final class JRDefaultCompiler implements JRCompiler
45 {
46
47
48     /**
49      *
50      */

51     private static final JRDefaultCompiler instance = new JRDefaultCompiler();
52
53         
54     /**
55      *
56      */

57     private JRDefaultCompiler()
58     {
59     }
60
61         
62     /**
63      *
64      */

65     public static JRDefaultCompiler getInstance()
66     {
67         return instance;
68     }
69
70         
71     /**
72      *
73      */

74     public JasperReport compileReport(JasperDesign jasperDesign) throws JRException
75     {
76         JRCompiler jrCompiler = null;
77
78         String JavaDoc compiler = JRProperties.getProperty(JRProperties.COMPILER_CLASS);
79         if (
80             (compiler == null
81             || compiler.trim().length() == 0)
82             && JRReport.LANGUAGE_GROOVY.equals(jasperDesign.getLanguage())
83             )
84         {
85             compiler = "net.sf.jasperreports.compilers.JRGroovyCompiler";
86         }
87
88         if (compiler == null || compiler.trim().length() == 0)
89         {
90             jrCompiler = getJavaCompiler();
91         }
92         else
93         {
94             try
95             {
96                 Class JavaDoc clazz = JRClassLoader.loadClassForName(compiler);
97                 jrCompiler = (JRCompiler)clazz.newInstance();
98             }
99             catch (Exception JavaDoc e)
100             {
101                 throw new JRException("Could not instantiate report compiler : " + compiler, e);
102             }
103         }
104         
105         return jrCompiler.compileReport(jasperDesign);
106     }
107
108
109     /**
110      *
111      */

112     private static JRCompiler getJavaCompiler()
113     {
114         JRCompiler compiler = null;
115
116         try
117         {
118             JRClassLoader.loadClassForName("org.eclipse.jdt.internal.compiler.Compiler");
119             compiler = new JRJdtCompiler();
120         }
121         catch (Exception JavaDoc e)
122         {
123         }
124
125         if (compiler == null)
126         {
127             try
128             {
129                 JRClassLoader.loadClassForName("com.sun.tools.javac.Main");
130                 compiler = new JRJdk13Compiler();
131             }
132             catch (Exception JavaDoc e)
133             {
134             }
135         }
136
137         if (compiler == null)
138         {
139             try
140             {
141                 JRClassLoader.loadClassForName("sun.tools.javac.Main");
142                 compiler = new JRJdk12Compiler();
143             }
144             catch (Exception JavaDoc e)
145             {
146             }
147         }
148
149         if (compiler == null)
150         {
151             compiler = new JRJavacCompiler();
152         }
153         
154         return compiler;
155     }
156
157
158     private static JRCompiler getCompiler(JasperReport jasperReport) throws JRException
159     {
160         JRCompiler compiler = null;
161         
162         String JavaDoc compilerClassName = jasperReport.getCompilerClass();
163
164         Class JavaDoc compilerClass = null;
165         
166         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
167         if (classLoader != null)
168         {
169             try
170             {
171                 compilerClass = classLoader.loadClass(compilerClassName);
172             }
173             catch(ClassNotFoundException JavaDoc e)
174             {
175             }
176         }
177         
178         if (compilerClass == null)
179         {
180             classLoader = JRDefaultCompiler.class.getClassLoader();
181             try
182             {
183                 if (classLoader == null)
184                 {
185                     compilerClass = Class.forName(compilerClassName);
186                 }
187                 else
188                 {
189                     compilerClass = classLoader.loadClass(compilerClassName);
190                 }
191             }
192             catch(ClassNotFoundException JavaDoc e)
193             {
194                 throw new JRException("Report compiler class not found : " + compilerClassName);
195             }
196         }
197
198
199         try
200         {
201             compiler = (JRCompiler)compilerClass.newInstance();
202         }
203         catch (Exception JavaDoc e)
204         {
205             throw new JRException("Could not instantiate report compiler : " + compilerClassName, e);
206         }
207         return compiler;
208     }
209
210     
211     /**
212      *
213      */

214     public JREvaluator loadEvaluator(JasperReport jasperReport, JRDataset dataset) throws JRException
215     {
216         JRCompiler compiler = getCompiler(jasperReport);
217         
218         return compiler.loadEvaluator(jasperReport, dataset);
219     }
220
221
222     public JREvaluator loadEvaluator(JasperReport jasperReport, JRCrosstab crosstab) throws JRException
223     {
224         JRCompiler compiler = getCompiler(jasperReport);
225         
226         return compiler.loadEvaluator(jasperReport, crosstab);
227     }
228
229
230     public JREvaluator loadEvaluator(JasperReport jasperReport) throws JRException
231     {
232         return loadEvaluator(jasperReport, jasperReport.getMainDataset());
233     }
234
235
236 }
237
Popular Tags