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;
029:
030: import java.io.File;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.util.Collection;
034:
035: import net.sf.jasperreports.crosstabs.JRCrosstab;
036: import net.sf.jasperreports.engine.design.JRCompiler;
037: import net.sf.jasperreports.engine.design.JRJavacCompiler;
038: import net.sf.jasperreports.engine.design.JRJdk12Compiler;
039: import net.sf.jasperreports.engine.design.JRJdk13Compiler;
040: import net.sf.jasperreports.engine.design.JRJdtCompiler;
041: import net.sf.jasperreports.engine.design.JRValidationFault;
042: import net.sf.jasperreports.engine.design.JRVerifier;
043: import net.sf.jasperreports.engine.design.JasperDesign;
044: import net.sf.jasperreports.engine.fill.JREvaluator;
045: import net.sf.jasperreports.engine.util.JRClassLoader;
046: import net.sf.jasperreports.engine.util.JRLoader;
047: import net.sf.jasperreports.engine.util.JRProperties;
048: import net.sf.jasperreports.engine.util.JRSaver;
049: import net.sf.jasperreports.engine.xml.JRXmlLoader;
050: import net.sf.jasperreports.engine.xml.JRXmlWriter;
051:
052: /**
053: * Façade class for compiling report designs into the ready-to-fill form
054: * and for getting the XML representation of report design objects for
055: * storage or network transfer.
056: *
057: * Report compilation using this class is delegated to the
058: * {@link net.sf.jasperreports.engine.design.JRDefaultCompiler}.
059: *
060: * @see net.sf.jasperreports.engine.design.JasperDesign
061: * @see net.sf.jasperreports.engine.JasperReport
062: * @see net.sf.jasperreports.engine.design.JRDefaultCompiler
063: * @see net.sf.jasperreports.engine.design.JRVerifier
064: * @see net.sf.jasperreports.engine.xml.JRXmlLoader
065: * @see net.sf.jasperreports.engine.xml.JRXmlWriter
066: * @see net.sf.jasperreports.engine.util.JRLoader
067: * @see net.sf.jasperreports.engine.util.JRSaver
068: * @author Teodor Danciu (teodord@users.sourceforge.net)
069: * @version $Id: JasperCompileManager.java 1828 2007-08-24 13:58:43Z teodord $
070: */
071: public class JasperCompileManager {
072:
073: /**
074: * Compiles the XML report design file specified by the parameter.
075: * The result of this operation is another file that will contain the serialized
076: * {@link net.sf.jasperreports.engine.JasperReport} object representing the compiled report design,
077: * having the same name as the report design as declared in the XML plus the <code>*.jasper</code> extension,
078: * located in the same directory as the XML source file.
079: *
080: * @param sourceFileName XML source file name
081: * @return resulting file name containing a serialized {@link net.sf.jasperreports.engine.JasperReport} object
082: */
083: public static String compileReportToFile(String sourceFileName)
084: throws JRException {
085: File sourceFile = new File(sourceFileName);
086:
087: JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
088:
089: File destFile = new File(sourceFile.getParent(), jasperDesign
090: .getName()
091: + ".jasper");
092: String destFileName = destFile.toString();
093:
094: compileReportToFile(jasperDesign, destFileName);
095:
096: return destFileName;
097: }
098:
099: /**
100: * Compiles the XML report design file received as the first parameter, placing the result
101: * in the file specified by the second parameter.
102: * The resulting file will contain a serialized instance of a
103: * {@link net.sf.jasperreports.engine.JasperReport} object representing
104: * the compiled report design.
105: *
106: * @param sourceFileName XML source file name
107: * @param destFileName file name to place the result into
108: */
109: public static void compileReportToFile(String sourceFileName,
110: String destFileName) throws JRException {
111: JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
112:
113: compileReportToFile(jasperDesign, destFileName);
114: }
115:
116: /**
117: * Compiles the report design object received as the first parameter, placing the result
118: * in the file specified by the second parameter.
119: * The resulting file will contain a serialized instance of a
120: * {@link net.sf.jasperreports.engine.JasperReport} object representing the compiled report design.
121: *
122: * @param jasperDesign source report design object
123: * @param destFileName file name to place the compiled report design into
124: */
125: public static void compileReportToFile(JasperDesign jasperDesign,
126: String destFileName) throws JRException {
127: JasperReport jasperReport = compileReport(jasperDesign);
128:
129: JRSaver.saveObject(jasperReport, destFileName);
130: }
131:
132: /**
133: * Compiles the XML report design file received as parameter, and returns
134: * the compiled report design object.
135: *
136: * @param sourceFileName XML source file name
137: * @return compiled report design object
138: */
139: public static JasperReport compileReport(String sourceFileName)
140: throws JRException {
141: JasperDesign jasperDesign = JRXmlLoader.load(sourceFileName);
142:
143: return compileReport(jasperDesign);
144: }
145:
146: /**
147: * Compiles the XML representation of the report design read from the supplied input stream and
148: * writes the generated compiled report design object to the output stream specified
149: * by the second parameter.
150: *
151: * @param inputStream XML source input stream
152: * @param outputStream output stream to write the compiled report design to
153: */
154: public static void compileReportToStream(InputStream inputStream,
155: OutputStream outputStream) throws JRException {
156: JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
157:
158: compileReportToStream(jasperDesign, outputStream);
159: }
160:
161: /**
162: * Compiles the report design object represented by the first parameter and
163: * writes the generated compiled report design object to the output stream specified
164: * by the second parameter.
165: *
166: * @param jasperDesign source report design object
167: * @param outputStream output stream to write the compiled report design to
168: */
169: public static void compileReportToStream(JasperDesign jasperDesign,
170: OutputStream outputStream) throws JRException {
171: JasperReport jasperReport = compileReport(jasperDesign);
172:
173: JRSaver.saveObject(jasperReport, outputStream);
174: }
175:
176: /**
177: * Compiles the serialized report design object read from the supplied input stream and
178: * returns the generated compiled report design object.
179: *
180: * @param inputStream XML source input stream
181: * @return compiled report design object
182: */
183: public static JasperReport compileReport(InputStream inputStream)
184: throws JRException {
185: JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
186:
187: return compileReport(jasperDesign);
188: }
189:
190: /**
191: * Compiles the report design object received as parameter and
192: * returns the generated compiled report design object.
193: *
194: * @param jasperDesign source report design object
195: * @return compiled report design object
196: * @see net.sf.jasperreports.engine.design.JRDefaultCompiler
197: */
198: public static JasperReport compileReport(JasperDesign jasperDesign)
199: throws JRException {
200: return getCompiler(jasperDesign).compileReport(jasperDesign);
201: }
202:
203: /**
204: * Verifies the validity and consistency of the report design object.
205: * Returns a collection of {@link JRValidationFault errors}, if problems are found in the report design.
206: *
207: * @param jasperDesign report design object to verify
208: * @return collection of {@link JRValidationFault JRValidationFault} if problems are found
209: * @see net.sf.jasperreports.engine.design.JRVerifier
210: */
211: public static Collection verifyDesign(JasperDesign jasperDesign) {
212: return JRVerifier.verifyDesign(jasperDesign);
213: }
214:
215: /**
216: *
217: */
218: public static JREvaluator loadEvaluator(JasperReport jasperReport,
219: JRDataset dataset) throws JRException {
220: JRCompiler compiler = getCompiler(jasperReport);
221:
222: return compiler.loadEvaluator(jasperReport, dataset);
223: }
224:
225: /**
226: *
227: */
228: public static JREvaluator loadEvaluator(JasperReport jasperReport,
229: JRCrosstab crosstab) throws JRException {
230: JRCompiler compiler = getCompiler(jasperReport);
231:
232: return compiler.loadEvaluator(jasperReport, crosstab);
233: }
234:
235: /**
236: *
237: */
238: public static JREvaluator loadEvaluator(JasperReport jasperReport)
239: throws JRException {
240: return loadEvaluator(jasperReport, jasperReport
241: .getMainDataset());
242: }
243:
244: /**
245: * Generates the XML representation of the report design loaded from the specified filename.
246: * The result of this operation is an "UTF-8" encoded XML file having the same name as
247: * the report design, plus the <code>*.jasper.jrxml</code> extension, located in the same directory as
248: * the source file.
249: *
250: * @param sourceFileName source file name containing the report design object
251: * @return XML representation of the report design
252: */
253: public static String writeReportToXmlFile(String sourceFileName)
254: throws JRException {
255: File sourceFile = new File(sourceFileName);
256:
257: /* We need the report name. */
258: JRReport report = (JRReport) JRLoader.loadObject(sourceFile);
259:
260: File destFile = new File(sourceFile.getParent(), report
261: .getName()
262: + ".jasper.jrxml");
263: String destFileName = destFile.toString();
264:
265: writeReportToXmlFile(report, destFileName);
266:
267: return destFileName;
268: }
269:
270: /**
271: * Generates the XML representation of the report design loaded from the first file parameter
272: * and place it in the file specified by the second parameter. The result is "UTF-8" encoded.
273: *
274: * @param sourceFileName source file name containing the report design object
275: * @param destFileName output file name to write the XML report design representation to
276: */
277: public static void writeReportToXmlFile(String sourceFileName,
278: String destFileName) throws JRException {
279: JRReport report = (JRReport) JRLoader
280: .loadObject(sourceFileName);
281:
282: writeReportToXmlFile(report, destFileName);
283: }
284:
285: /**
286: * Generates the XML representation of the report design supplied as the first parameter
287: * and place it in the file specified by the second parameter. The result is "UTF-8" encoded.
288: *
289: * @param report source report design object
290: * @param destFileName output file name to write the XML report design representation to
291: * @see net.sf.jasperreports.engine.xml.JRXmlWriter
292: */
293: public static void writeReportToXmlFile(JRReport report,
294: String destFileName) throws JRException {
295: JRXmlWriter.writeReport(report, destFileName, "UTF-8");
296: }
297:
298: /**
299: * Generates the XML representation of the serialized report design object read from the supplied
300: * input stream abd writes it to the specified output stream, using the "UTF-8" encoding.
301: *
302: * @param inputStream source input stream to read the report design object from
303: * @param outputStream output stream to write the XML report design representation to
304: */
305: public static void writeReportToXmlStream(InputStream inputStream,
306: OutputStream outputStream) throws JRException {
307: JRReport report = (JRReport) JRLoader.loadObject(inputStream);
308:
309: writeReportToXmlStream(report, outputStream);
310: }
311:
312: /**
313: * Generates the XML representation of the report design object supplied as parameter
314: * and writes it to the specified output stream, using the "UTF-8" encoding.
315: *
316: * @param report source report design object
317: * @param outputStream output stream to write the XML report design representation to
318: * @see net.sf.jasperreports.engine.xml.JRXmlWriter
319: */
320: public static void writeReportToXmlStream(JRReport report,
321: OutputStream outputStream) throws JRException {
322: JRXmlWriter.writeReport(report, outputStream, "UTF-8");
323: }
324:
325: /**
326: * Generates the XML representation of the report design object supplied as parameter
327: * using the "UTF-8" enconding.
328: *
329: * @param report source report design object
330: * @return XML representation of the report design
331: * @see net.sf.jasperreports.engine.xml.JRXmlWriter
332: */
333: public static String writeReportToXml(JRReport report) {
334: return JRXmlWriter.writeReport(report, "UTF-8");
335: }
336:
337: /**
338: *
339: */
340: private static JRCompiler getJavaCompiler() {
341: JRCompiler compiler = null;
342:
343: try {
344: JRClassLoader
345: .loadClassForRealName("org.eclipse.jdt.internal.compiler.Compiler");
346: compiler = new JRJdtCompiler();
347: } catch (Exception e) {
348: }
349:
350: if (compiler == null) {
351: try {
352: JRClassLoader
353: .loadClassForRealName("com.sun.tools.javac.Main");
354: compiler = new JRJdk13Compiler();
355: } catch (Exception e) {
356: }
357: }
358:
359: if (compiler == null) {
360: try {
361: JRClassLoader
362: .loadClassForRealName("sun.tools.javac.Main");
363: compiler = new JRJdk12Compiler();
364: } catch (Exception e) {
365: }
366: }
367:
368: if (compiler == null) {
369: compiler = new JRJavacCompiler();
370: }
371:
372: return compiler;
373: }
374:
375: /**
376: *
377: */
378: private static JRCompiler getCompiler(JasperReport jasperReport)
379: throws JRException {
380: JRCompiler compiler = null;
381:
382: String compilerClassName = jasperReport.getCompilerClass();
383:
384: Class compilerClass = null;
385:
386: ClassLoader classLoader = Thread.currentThread()
387: .getContextClassLoader();
388: if (classLoader != null) {
389: try {
390: compilerClass = classLoader
391: .loadClass(compilerClassName);
392: } catch (ClassNotFoundException e) {
393: }
394: }
395:
396: if (compilerClass == null) {
397: classLoader = JasperCompileManager.class.getClassLoader();
398: try {
399: if (classLoader == null) {
400: compilerClass = Class.forName(compilerClassName);
401: } else {
402: compilerClass = classLoader
403: .loadClass(compilerClassName);
404: }
405: } catch (ClassNotFoundException e) {
406: throw new JRException(
407: "Report compiler class not found : "
408: + compilerClassName);
409: }
410: }
411:
412: try {
413: compiler = (JRCompiler) compilerClass.newInstance();
414: } catch (Exception e) {
415: throw new JRException(
416: "Could not instantiate report compiler : "
417: + compilerClassName, e);
418: }
419: return compiler;
420: }
421:
422: /**
423: *
424: */
425: private static JRCompiler getCompiler(JasperDesign jasperDesign)
426: throws JRException {
427: JRCompiler compiler = null;
428:
429: String compilerClassName = JRProperties
430: .getProperty(JRProperties.COMPILER_CLASS);
431: if (compilerClassName == null
432: || compilerClassName.trim().length() == 0) {
433: String language = jasperDesign.getLanguage();
434: compilerClassName = JRProperties
435: .getProperty(JRCompiler.COMPILER_PREFIX + language);
436: if (compilerClassName == null
437: || compilerClassName.trim().length() == 0) {
438: if (JRReport.LANGUAGE_JAVA.equals(language)) {
439: return getJavaCompiler();
440: } else {
441: throw new JRException(
442: "No report compiler set for language : "
443: + language);
444: }
445: }
446: }
447:
448: try {
449: Class clazz = JRClassLoader
450: .loadClassForName(compilerClassName);
451: compiler = (JRCompiler) clazz.newInstance();
452: } catch (Exception e) {
453: throw new JRException(
454: "Could not instantiate report compiler : "
455: + compilerClassName, e);
456: }
457:
458: return compiler;
459: }
460:
461: }
|