KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 /*
30  * Contributors:
31  * Peter Severin - peter_p_s@users.sourceforge.net
32  */

33 package net.sf.jasperreports.compilers;
34
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.Serializable JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import net.sf.jasperreports.engine.JRException;
42 import net.sf.jasperreports.engine.JRReport;
43 import net.sf.jasperreports.engine.design.JRAbstractJavaCompiler;
44 import net.sf.jasperreports.engine.design.JRCompilationUnit;
45 import net.sf.jasperreports.engine.design.JRSourceCompileTask;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49 import org.codehaus.groovy.ast.ClassNode;
50 import org.codehaus.groovy.control.CompilationFailedException;
51 import org.codehaus.groovy.control.CompilationUnit;
52 import org.codehaus.groovy.control.CompilerConfiguration;
53 import org.codehaus.groovy.control.Phases;
54 import org.objectweb.asm.ClassVisitor;
55 import org.objectweb.asm.ClassWriter;
56
57 /**
58  * Calculator compiler that uses groovy to compile expressions.
59  *
60  * @author Teodor Danciu (teodord@users.sourceforge.net), Peter Severin (peter_p_s@users.sourceforge.net)
61  * @version $Id: JRGroovyCompiler.java 1542 2006-12-22 18:31:48 +0200 (Fri, 22 Dec 2006) teodord $
62  */

63 public class JRGroovyCompiler extends JRAbstractJavaCompiler
64 {
65
66     /**
67      *
68      */

69     private static final Log log = LogFactory.getLog(JRGroovyCompiler.class);
70
71     
72     public JRGroovyCompiler()
73     {
74         super(false);
75     }
76     
77
78     protected String JavaDoc compileUnits(JRCompilationUnit[] units, String JavaDoc classpath, File JavaDoc tempDirFile) throws JRException
79     {
80         CompilerConfiguration config = new CompilerConfiguration();
81         config.setClasspath(classpath);
82         CompilationUnit unit = new CompilationUnit(config);
83         
84         for (int i = 0; i < units.length; i++)
85         {
86             unit.addSource("calculator_" + units[i].getName(), new ByteArrayInputStream JavaDoc(units[i].getSourceCode().getBytes()));
87         }
88         
89         ClassCollector collector = new ClassCollector();
90         unit.setClassgenCallback(collector);
91         try
92         {
93             unit.compile(Phases.CLASS_GENERATION);
94         }
95         catch (CompilationFailedException e)
96         {
97             throw new JRException(
98                 "Errors were encountered when compiling report expressions class file:\n"
99                 + e.toString()
100                 );
101         }
102
103         if (collector.classes.size() < units.length)
104         {
105             throw new JRException("Too few groovy class were generated.");
106         }
107         else if (collector.classCount > units.length)
108         {
109             throw new JRException(
110                 "Too many groovy classes were generated.\n"
111                 + "Please make sure that you don't use Groovy features such as closures that are not supported by this report compiler.\n"
112                 );
113         }
114         
115         for (int i = 0; i < units.length; i++)
116         {
117             units[i].setCompileData((Serializable JavaDoc) collector.classes.get(units[i].getName()));
118         }
119         
120         return null;
121     }
122
123     
124     /**
125      *
126      */

127     private static class ClassCollector extends CompilationUnit.ClassgenCallback
128     {
129         public Map JavaDoc classes = new HashMap JavaDoc();
130         public int classCount;
131     
132         /**
133          * @see org.codehaus.groovy.control.CompilationUnit.ClassgenCallback#call(
134          * org.objectweb.asm.ClassVisitor,
135          * org.codehaus.groovy.ast.ClassNode)
136          */

137         public void call(ClassVisitor writer, ClassNode node) throws CompilationFailedException
138         {
139             classCount++;
140             String JavaDoc name = node.getName();
141             if (!classes.containsKey(name))
142             {
143                 byte[] bytes = ((ClassWriter) writer).toByteArray();
144                 classes.put(name, bytes);
145             }
146         }
147     }
148
149
150     protected void checkLanguage(String JavaDoc language) throws JRException
151     {
152         if (!JRReport.LANGUAGE_GROOVY.equals(language))
153         {
154             throw
155                 new JRException(
156                     "Language \"" + language
157                     + "\" not supported by this report compiler.\n"
158                     + "Expecting \"groovy\" instead."
159                     );
160         }
161     }
162
163
164     protected String JavaDoc generateSourceCode(JRSourceCompileTask sourceTask) throws JRException
165     {
166         return JRGroovyGenerator.generateClass(sourceTask);
167     }
168
169
170     protected String JavaDoc getSourceFileName(String JavaDoc unitName)
171     {
172         return unitName + ".groovy";
173     }
174
175
176 }
Popular Tags