KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Gaganis Giorgos - gaganis@users.sourceforge.net
33  */

34 package net.sf.jasperreports.compilers;
35
36 import java.text.SimpleDateFormat JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Arrays JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Collections JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import net.sf.jasperreports.engine.JRException;
47 import net.sf.jasperreports.engine.JRExpression;
48 import net.sf.jasperreports.engine.JRExpressionChunk;
49 import net.sf.jasperreports.engine.JRField;
50 import net.sf.jasperreports.engine.JRParameter;
51 import net.sf.jasperreports.engine.JRVariable;
52 import net.sf.jasperreports.engine.design.JRSourceCompileTask;
53 import net.sf.jasperreports.engine.util.JRStringUtil;
54
55
56 /**
57  * @author Teodor Danciu (teodord@users.sourceforge.net), Peter Severin (peter_p_s@users.sourceforge.net)
58  * @version $Id: JRGroovyGenerator.java 1232 2006-04-20 16:15:10 +0300 (Thu, 20 Apr 2006) teodord $
59  */

60 public class JRGroovyGenerator
61 {
62     
63     /**
64      *
65      */

66     private static final int EXPR_MAX_COUNT_PER_METHOD = 100;
67
68     private static Map JavaDoc fieldPrefixMap = null;
69     private static Map JavaDoc variablePrefixMap = null;
70     private static Map JavaDoc methodSuffixMap = null;
71
72     static
73     {
74         fieldPrefixMap = new HashMap JavaDoc();
75         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
76         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "");
77         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
78         
79         variablePrefixMap = new HashMap JavaDoc();
80         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
81         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "Estimated");
82         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
83         
84         methodSuffixMap = new HashMap JavaDoc();
85         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
86         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "Estimated");
87         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
88     }
89     
90     /**
91      *
92      */

93     protected final JRSourceCompileTask sourceTask;
94
95     protected Map JavaDoc parametersMap;
96     protected Map JavaDoc fieldsMap;
97     protected Map JavaDoc variablesMap;
98     protected JRVariable[] variables;
99
100     
101     protected JRGroovyGenerator(JRSourceCompileTask sourceTask)
102     {
103         this.sourceTask = sourceTask;
104         
105         this.parametersMap = sourceTask.getParametersMap();
106         this.fieldsMap = sourceTask.getFieldsMap();
107         this.variablesMap = sourceTask.getVariablesMap();
108         this.variables = sourceTask.getVariables();
109     }
110
111
112     /**
113      *
114      */

115     public static String JavaDoc generateClass(JRSourceCompileTask sourceTask) throws JRException
116     {
117         JRGroovyGenerator generator = new JRGroovyGenerator(sourceTask);
118         return generator.generateClass();
119     }
120     
121     
122     protected String JavaDoc generateClass() throws JRException
123     {
124         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
125
126         generateClassStart(sb);
127
128         generateDeclarations(sb);
129
130         generateInitMethod(sb);
131         generateInitParamsMethod(sb);
132         if (fieldsMap != null)
133         {
134             generateInitFieldsMethod(sb);
135         }
136         generateInitVarsMethod(sb);
137
138         List JavaDoc expressions = sourceTask.getExpressions();
139         sb.append(this.generateMethod(JRExpression.EVALUATION_DEFAULT, expressions));
140         if (sourceTask.isOnlyDefaultEvaluation())
141         {
142             List JavaDoc empty = new ArrayList JavaDoc();
143             sb.append(this.generateMethod(JRExpression.EVALUATION_OLD, empty));
144             sb.append(this.generateMethod(JRExpression.EVALUATION_ESTIMATED, empty));
145         }
146         else
147         {
148             sb.append(this.generateMethod(JRExpression.EVALUATION_OLD, expressions));
149             sb.append(this.generateMethod(JRExpression.EVALUATION_ESTIMATED, expressions));
150         }
151
152         sb.append("}\n");
153
154         return sb.toString();
155     }
156
157
158     private void generateInitMethod(StringBuffer JavaDoc sb)
159     {
160         sb.append("\n");
161         sb.append("\n");
162         sb.append(" /**\n");
163         sb.append(" *\n");
164         sb.append(" */\n");
165         sb.append(" void customizedInit(\n");
166         sb.append(" Map pm,\n");
167         sb.append(" Map fm,\n");
168         sb.append(" Map vm\n");
169         sb.append(" )\n");
170         sb.append(" {\n");
171         sb.append(" initParams(pm);\n");
172         if (fieldsMap != null)
173         {
174             sb.append(" initFields(fm);\n");
175         }
176         sb.append(" initVars(vm);\n");
177         sb.append(" }\n");
178         sb.append("\n");
179         sb.append("\n");
180     }
181
182     
183     protected final void generateClassStart(StringBuffer JavaDoc sb)
184     {
185         /* */
186         sb.append("/*\n");
187         sb.append(" * Generated by JasperReports - ");
188         sb.append((new SimpleDateFormat JavaDoc()).format(new java.util.Date JavaDoc()));
189         sb.append("\n");
190         sb.append(" */\n");
191         sb.append("import net.sf.jasperreports.engine.*;\n");
192         sb.append("import net.sf.jasperreports.engine.fill.*;\n");
193         sb.append("\n");
194         sb.append("import java.util.*;\n");
195         sb.append("import java.math.*;\n");
196         sb.append("import java.text.*;\n");
197         sb.append("import java.io.*;\n");
198         sb.append("import java.net.*;\n");
199         sb.append("\n");
200         
201         /* */
202         String JavaDoc[] imports = sourceTask.getImports();
203         if (imports != null && imports.length > 0)
204         {
205             for (int i = 0; i < imports.length; i++)
206             {
207                 sb.append("import ");
208                 sb.append(imports[i]);
209                 sb.append(";\n");
210             }
211         }
212
213         /* */
214         sb.append("\n");
215         sb.append("\n");
216         sb.append("/**\n");
217         sb.append(" *\n");
218         sb.append(" */\n");
219         sb.append("class ");
220         sb.append(sourceTask.getUnitName());
221         sb.append(" extends JREvaluator\n");
222         sb.append("{\n");
223         sb.append("\n");
224         sb.append("\n");
225         sb.append(" /**\n");
226         sb.append(" *\n");
227         sb.append(" */\n");
228     }
229
230
231     protected final void generateDeclarations(StringBuffer JavaDoc sb)
232     {
233         if (parametersMap != null && parametersMap.size() > 0)
234         {
235             Collection JavaDoc parameterNames = parametersMap.keySet();
236             for (Iterator JavaDoc it = parameterNames.iterator(); it.hasNext();)
237             {
238                 sb.append(" private property JRFillParameter parameter_");
239                 sb.append(JRStringUtil.getLiteral((String JavaDoc)it.next()));
240                 sb.append(" = null;\n");
241             }
242         }
243         
244         if (fieldsMap != null && fieldsMap.size() > 0)
245         {
246             Collection JavaDoc fieldNames = fieldsMap.keySet();
247             for (Iterator JavaDoc it = fieldNames.iterator(); it.hasNext();)
248             {
249                 sb.append(" private property JRFillField field_");
250                 sb.append(JRStringUtil.getLiteral((String JavaDoc)it.next()));
251                 sb.append(" = null;\n");
252             }
253         }
254         
255         if (variables != null && variables.length > 0)
256         {
257             for (int i = 0; i < variables.length; i++)
258             {
259                 sb.append(" private property JRFillVariable variable_");
260                 sb.append(JRStringUtil.getLiteral(variables[i].getName()));
261                 sb.append(" = null;\n");
262             }
263         }
264     }
265
266
267     protected final void generateInitParamsMethod(StringBuffer JavaDoc sb) throws JRException
268     {
269         Iterator JavaDoc parIt = null;
270         if (parametersMap != null && parametersMap.size() > 0)
271         {
272             parIt = parametersMap.keySet().iterator();
273         }
274         else
275         {
276             parIt = Collections.EMPTY_SET.iterator();
277         }
278         generateInitParamsMethod(sb, parIt, 0);
279     }
280
281
282     protected final void generateInitFieldsMethod(StringBuffer JavaDoc sb) throws JRException
283     {
284         Iterator JavaDoc fieldIt = null;
285         if (fieldsMap != null && fieldsMap.size() > 0)
286         {
287             fieldIt = fieldsMap.keySet().iterator();
288         }
289         else
290         {
291             fieldIt = Collections.EMPTY_SET.iterator();
292         }
293         generateInitFieldsMethod(sb, fieldIt, 0);
294     }
295
296
297     protected final void generateInitVarsMethod(StringBuffer JavaDoc sb) throws JRException
298     {
299         Iterator JavaDoc varIt = null;
300         if (variables != null && variables.length > 0)
301         {
302             varIt = Arrays.asList(variables).iterator();
303         }
304         else
305         {
306             varIt = Collections.EMPTY_LIST.iterator();
307         }
308         generateInitVarsMethod(sb, varIt, 0);
309     }
310
311
312     /**
313      *
314      */

315     private void generateInitParamsMethod(StringBuffer JavaDoc sb, Iterator JavaDoc it, int index) throws JRException
316     {
317         sb.append(" /**\n");
318         sb.append(" *\n");
319         sb.append(" */\n");
320         sb.append(" void initParams");
321         if(index > 0)
322         {
323             sb.append(index);
324         }
325         sb.append("(Map pm)\n");
326         sb.append(" {\n");
327         for (int i = 0; i < EXPR_MAX_COUNT_PER_METHOD && it.hasNext(); i++)
328         {
329             String JavaDoc parameterName = (String JavaDoc)it.next();
330             sb.append(" parameter_");
331             sb.append(JRStringUtil.getLiteral(parameterName));
332             sb.append(" = (JRFillParameter)pm.get(\"");
333             sb.append(parameterName);
334             sb.append("\");\n");
335         }
336         if(it.hasNext())
337         {
338             sb.append(" initParams");
339             sb.append(index + 1);
340             sb.append("(pm);\n");
341         }
342         sb.append(" }\n");
343         sb.append("\n");
344         sb.append("\n");
345
346         if(it.hasNext())
347         {
348             generateInitParamsMethod(sb, it, index + 1);
349         }
350     }
351
352
353     /**
354      *
355      */

356     private void generateInitFieldsMethod(StringBuffer JavaDoc sb, Iterator JavaDoc it, int index) throws JRException
357     {
358         sb.append(" /**\n");
359         sb.append(" *\n");
360         sb.append(" */\n");
361         sb.append(" void initFields");
362         if(index > 0)
363         {
364             sb.append(index);
365         }
366         sb.append("(Map fm)\n");
367         sb.append(" {\n");
368         for (int i = 0; i < EXPR_MAX_COUNT_PER_METHOD && it.hasNext(); i++)
369         {
370             String JavaDoc fieldName = (String JavaDoc)it.next();
371             sb.append(" field_");
372             sb.append(JRStringUtil.getLiteral(fieldName));
373             sb.append(" = (JRFillField)fm.get(\"");
374             sb.append(fieldName);
375             sb.append("\");\n");
376         }
377         if(it.hasNext())
378         {
379             sb.append(" initFields");
380             sb.append(index + 1);
381             sb.append("(fm);\n");
382         }
383         sb.append(" }\n");
384         sb.append("\n");
385         sb.append("\n");
386
387         if(it.hasNext())
388         {
389             generateInitFieldsMethod(sb, it, index + 1);
390         }
391     }
392
393
394     /**
395      *
396      */

397     private void generateInitVarsMethod(StringBuffer JavaDoc sb, Iterator JavaDoc it, int index) throws JRException
398     {
399         sb.append(" /**\n");
400         sb.append(" *\n");
401         sb.append(" */\n");
402         sb.append(" void initVars");
403         if(index > 0)
404         {
405             sb.append(index);
406         }
407         sb.append("(Map vm)\n");
408         sb.append(" {\n");
409         for (int i = 0; i < EXPR_MAX_COUNT_PER_METHOD && it.hasNext(); i++)
410         {
411             String JavaDoc variableName = ((JRVariable) it.next()).getName();
412             sb.append(" variable_");
413             sb.append(JRStringUtil.getLiteral(variableName));
414             sb.append(" = (JRFillVariable)vm.get(\"");
415             sb.append(variableName);
416             sb.append("\");\n");
417         }
418         if(it.hasNext())
419         {
420             sb.append(" initVars");
421             sb.append(index + 1);
422             sb.append("(vm);\n");
423         }
424         sb.append(" }\n");
425         sb.append("\n");
426         sb.append("\n");
427
428         if(it.hasNext())
429         {
430             generateInitVarsMethod(sb, it, index + 1);
431         }
432     }
433
434
435     /**
436      *
437      */

438     protected final String JavaDoc generateMethod(byte evaluationType, List JavaDoc expressionsList) throws JRException
439     {
440         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
441         
442         if (expressionsList != null && !expressionsList.isEmpty())
443         {
444             sb.append(generateMethod(expressionsList.iterator(), 0, evaluationType, expressionsList.size()));
445         }
446         else
447         {
448             /* */
449             sb.append(" /**\n");
450             sb.append(" *\n");
451             sb.append(" */\n");
452             sb.append(" Object evaluate");
453             sb.append((String JavaDoc)methodSuffixMap.get(new Byte JavaDoc(evaluationType)));
454             sb.append("(int id)\n");
455             sb.append(" {\n");
456             sb.append(" return null;\n");
457             sb.append(" }\n");
458             sb.append("\n");
459             sb.append("\n");
460         }
461         
462         return sb.toString();
463     }
464
465
466     /**
467      *
468      */

469     private String JavaDoc generateMethod(Iterator JavaDoc it, int index, byte evaluationType, int expressionCount) throws JRException
470     {
471         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
472         
473         /* */
474         sb.append(" /**\n");
475         sb.append(" *\n");
476         sb.append(" */\n");
477         sb.append(" Object evaluate");
478         sb.append((String JavaDoc) methodSuffixMap.get(new Byte JavaDoc(evaluationType)));
479         if (index > 0)
480         {
481             sb.append(index);
482         }
483         sb.append("(int id)\n");
484         sb.append(" {\n");
485         sb.append(" Object value = null;\n");
486         sb.append("\n");
487         
488         //NB: relying on the fact that the expression id is the same as the index of the expression in the list
489
int expressionIdLimit = (index + 1) * EXPR_MAX_COUNT_PER_METHOD;
490         boolean nextMethod = expressionCount > expressionIdLimit;
491         
492         if (nextMethod)
493         {
494             sb.append(" if (id >= ");
495             sb.append(expressionIdLimit);
496             sb.append(")\n");
497             sb.append(" {\n");
498             sb.append(" value = evaluate");
499             sb.append((String JavaDoc) methodSuffixMap.get(new Byte JavaDoc(evaluationType)));
500             sb.append(index + 1);
501             sb.append("(id);\n");
502             sb.append(" }\n");
503         }
504         
505         for (int i = 0; it.hasNext() && i < EXPR_MAX_COUNT_PER_METHOD; i++)
506         {
507             JRExpression expression = (JRExpression) it.next();
508             
509             sb.append(" ");
510             if (i > 0 || nextMethod)
511             {
512                 sb.append("else ");
513             }
514             sb.append("if (id == ");
515             sb.append(sourceTask.getExpressionId(expression));
516             sb.append(")\n");
517             sb.append(" {\n");
518             sb.append(" value = (");
519             sb.append(expression.getValueClassName());
520             sb.append(")(");
521             sb.append(this.generateExpression(expression, evaluationType));
522             sb.append(");\n");
523             sb.append(" }\n");
524         }
525
526         sb.append("\n");
527         sb.append(" return value;\n");
528         sb.append(" }\n");
529         sb.append("\n");
530         sb.append("\n");
531         
532         if (nextMethod)
533         {
534             sb.append(generateMethod(it, index + 1, evaluationType, expressionCount));
535         }
536         
537         return sb.toString();
538     }
539
540
541     /**
542      *
543      */

544     private String JavaDoc generateExpression(
545         JRExpression expression,
546         byte evaluationType
547         )
548     {
549         JRParameter jrParameter = null;
550         JRField jrField = null;
551         JRVariable jrVariable = null;
552
553         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
554
555         JRExpressionChunk[] chunks = expression.getChunks();
556         JRExpressionChunk chunk = null;
557         String JavaDoc chunkText = null;
558         if (chunks != null && chunks.length > 0)
559         {
560             for(int i = 0; i < chunks.length; i++)
561             {
562                 chunk = chunks[i];
563
564                 chunkText = chunk.getText();
565                 if (chunkText == null)
566                 {
567                     chunkText = "";
568                 }
569                 
570                 switch (chunk.getType())
571                 {
572                     case JRExpressionChunk.TYPE_TEXT :
573                     {
574                         sb.append(chunkText);
575                         break;
576                     }
577                     case JRExpressionChunk.TYPE_PARAMETER :
578                     {
579                         jrParameter = (JRParameter)parametersMap.get(chunkText);
580     
581                         sb.append("((");
582                         sb.append(jrParameter.getValueClassName());
583                         sb.append(")parameter_");
584                         sb.append(JRStringUtil.getLiteral(chunkText));
585                         sb.append(".getValue())");
586     
587                         break;
588                     }
589                     case JRExpressionChunk.TYPE_FIELD :
590                     {
591                         jrField = (JRField)fieldsMap.get(chunkText);
592     
593                         sb.append("((");
594                         sb.append(jrField.getValueClassName());
595                         sb.append(")field_");
596                         sb.append(JRStringUtil.getLiteral(chunkText));
597                         sb.append(".get");
598                         sb.append((String JavaDoc)fieldPrefixMap.get(new Byte JavaDoc(evaluationType)));
599                         sb.append("Value())");
600     
601                         break;
602                     }
603                     case JRExpressionChunk.TYPE_VARIABLE :
604                     {
605                         jrVariable = (JRVariable)variablesMap.get(chunkText);
606     
607                         sb.append("((");
608                         sb.append(jrVariable.getValueClassName());
609                         sb.append(")variable_");
610                         sb.append(JRStringUtil.getLiteral(chunkText));
611                         sb.append(".get");
612                         sb.append((String JavaDoc)variablePrefixMap.get(new Byte JavaDoc(evaluationType)));
613                         sb.append("Value())");
614     
615                         break;
616                     }
617                     case JRExpressionChunk.TYPE_RESOURCE :
618                     {
619                         sb.append("str(\"");
620                         sb.append(chunkText);
621                         sb.append("\")");
622     
623                         break;
624                     }
625                 }
626             }
627         }
628         
629         if (sb.length() == 0)
630         {
631             sb.append("null");
632         }
633
634         return sb.toString();
635     }
636 }
637
Popular Tags