KickJava   Java API By Example, From Geeks To Geeks.

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


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

34 package net.sf.jasperreports.engine.design;
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.util.JRStringUtil;
53
54
55 /**
56  * @author Teodor Danciu (teodord@users.sourceforge.net)
57  * @version $Id: JRClassGenerator.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
58  */

59 public class JRClassGenerator
60 {
61     
62     
63     /**
64      *
65      */

66     private static final int EXPR_MAX_COUNT_PER_METHOD = 100;
67
68
69     private static Map JavaDoc fieldPrefixMap = null;
70     private static Map JavaDoc variablePrefixMap = null;
71     private static Map JavaDoc methodSuffixMap = null;
72     
73     static
74     {
75         fieldPrefixMap = new HashMap JavaDoc();
76         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
77         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "");
78         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
79         
80         variablePrefixMap = new HashMap JavaDoc();
81         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
82         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "Estimated");
83         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
84         
85         methodSuffixMap = new HashMap JavaDoc();
86         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
87         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "Estimated");
88         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
89     }
90
91     protected final JRSourceCompileTask sourceTask;
92     
93     protected Map JavaDoc parametersMap;
94     protected Map JavaDoc fieldsMap;
95     protected Map JavaDoc variablesMap;
96     protected JRVariable[] variables;
97     
98     protected JRClassGenerator(JRSourceCompileTask sourceTask)
99     {
100         this.sourceTask = sourceTask;
101         
102         parametersMap = sourceTask.getParametersMap();
103         fieldsMap = sourceTask.getFieldsMap();
104         variablesMap = sourceTask.getVariablesMap();
105         variables = sourceTask.getVariables();
106     }
107
108     
109     /**
110      * Generates Java source code for evaluating the expressions of a report/dataset/crosstab.
111      *
112      * @param sourceTask the source task containing data required to generate the source file
113      * @return the source code
114      * @throws JRException
115      */

116     public static String JavaDoc generateClass(JRSourceCompileTask sourceTask) throws JRException
117     {
118         JRClassGenerator generator = new JRClassGenerator(sourceTask);
119         return generator.generateClass();
120     }
121     
122
123     protected String JavaDoc generateClass() throws JRException
124     {
125         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126
127         generateClassStart(sb);
128
129         generateDeclarations(sb);
130
131         generateInitMethod(sb);
132         generateInitParamsMethod(sb);
133         if (fieldsMap != null)
134         {
135             generateInitFieldsMethod(sb);
136         }
137         generateInitVarsMethod(sb);
138
139         List JavaDoc expressions = sourceTask.getExpressions();
140         sb.append(generateMethod(JRExpression.EVALUATION_DEFAULT, expressions));
141         if (sourceTask.isOnlyDefaultEvaluation())
142         {
143             List JavaDoc empty = new ArrayList JavaDoc();
144             sb.append(generateMethod(JRExpression.EVALUATION_OLD, empty));
145             sb.append(generateMethod(JRExpression.EVALUATION_ESTIMATED, empty));
146         }
147         else
148         {
149             sb.append(generateMethod(JRExpression.EVALUATION_OLD, expressions));
150             sb.append(generateMethod(JRExpression.EVALUATION_ESTIMATED, expressions));
151         }
152         
153         sb.append("}\n");
154
155         return sb.toString();
156     }
157
158
159     private void generateInitMethod(StringBuffer JavaDoc sb)
160     {
161         sb.append("\n");
162         sb.append("\n");
163         sb.append(" /**\n");
164         sb.append(" *\n");
165         sb.append(" */\n");
166         sb.append(" public void customizedInit(\n");
167         sb.append(" Map pm,\n");
168         sb.append(" Map fm,\n");
169         sb.append(" Map vm\n");
170         sb.append(" )\n");
171         sb.append(" {\n");
172         sb.append(" initParams(pm);\n");
173         if (fieldsMap != null)
174         {
175             sb.append(" initFields(fm);\n");
176         }
177         sb.append(" initVars(vm);\n");
178         sb.append(" }\n");
179         sb.append("\n");
180         sb.append("\n");
181     }
182
183
184     protected final void generateClassStart(StringBuffer JavaDoc sb)
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("public 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 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 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 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(" private 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(" private 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(" private 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     protected final String JavaDoc generateMethod(byte evaluationType, List JavaDoc expressionsList) throws JRException
436     {
437         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
438
439         if (expressionsList.size() > 0)
440         {
441             sb.append(generateMethod(expressionsList.listIterator(), 0, evaluationType));
442         }
443         else
444         {
445             /* */
446             sb.append(" /**\n");
447             sb.append(" *\n");
448             sb.append(" */\n");
449             sb.append(" public Object evaluate");
450             sb.append((String JavaDoc)methodSuffixMap.get(new Byte JavaDoc(evaluationType)));
451             sb.append("(int id) throws Throwable\n");
452             sb.append(" {\n");
453             sb.append(" return null;\n");
454             sb.append(" }\n");
455             sb.append("\n");
456             sb.append("\n");
457         }
458         
459         return sb.toString();
460     }
461
462
463     /**
464      *
465      */

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

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