KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

33 package net.sf.jasperreports.compilers;
34
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import net.sf.jasperreports.engine.JRExpression;
44 import net.sf.jasperreports.engine.JRExpressionChunk;
45 import net.sf.jasperreports.engine.JRField;
46 import net.sf.jasperreports.engine.JRParameter;
47 import net.sf.jasperreports.engine.JRVariable;
48 import net.sf.jasperreports.engine.design.JRSourceCompileTask;
49 import net.sf.jasperreports.engine.util.JRStringUtil;
50
51
52 /**
53  * @author Teodor Danciu (teodord@users.sourceforge.net)
54  * @version $Id: JRBshGenerator.java 1460 2006-11-06 15:52:15 +0200 (Mon, 06 Nov 2006) shertage $
55  */

56 public class JRBshGenerator
57 {
58
59     private static Map JavaDoc fieldPrefixMap = null;
60     private static Map JavaDoc variablePrefixMap = null;
61     private static Map JavaDoc methodSuffixMap = null;
62     
63     
64     /**
65      *
66      */

67     protected final JRSourceCompileTask sourceTask;
68
69     protected Map JavaDoc parametersMap;
70     protected Map JavaDoc fieldsMap;
71     protected Map JavaDoc variablesMap;
72     protected JRVariable[] variables;
73
74     static
75     {
76         fieldPrefixMap = new HashMap JavaDoc();
77         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
78         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "");
79         fieldPrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
80         
81         variablePrefixMap = new HashMap JavaDoc();
82         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
83         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "Estimated");
84         variablePrefixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
85         
86         methodSuffixMap = new HashMap JavaDoc();
87         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_OLD), "Old");
88         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_ESTIMATED), "Estimated");
89         methodSuffixMap.put(new Byte JavaDoc(JRExpression.EVALUATION_DEFAULT), "");
90     }
91     
92
93     protected JRBshGenerator(JRSourceCompileTask sourceTask)
94     {
95         this.sourceTask = sourceTask;
96         
97         this.parametersMap = sourceTask.getParametersMap();
98         this.fieldsMap = sourceTask.getFieldsMap();
99         this.variablesMap = sourceTask.getVariablesMap();
100         this.variables = sourceTask.getVariables();
101     }
102
103
104     /**
105      *
106      */

107     public static String JavaDoc generateScript(JRSourceCompileTask sourceTask)
108     {
109         JRBshGenerator generator = new JRBshGenerator(sourceTask);
110         return generator.generateScript();
111     }
112     
113     
114     protected String JavaDoc generateScript()
115     {
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117
118         generateScriptStart(sb);
119
120         generateDeclarations(sb);
121         generateInitMethod(sb);
122         
123         sb.append("\n");
124         sb.append("\n");
125
126         List JavaDoc expressions = sourceTask.getExpressions();
127         sb.append(generateMethod(JRExpression.EVALUATION_DEFAULT, expressions));
128         if (sourceTask.isOnlyDefaultEvaluation())
129         {
130             List JavaDoc empty = new ArrayList JavaDoc();
131             sb.append(generateMethod(JRExpression.EVALUATION_OLD, empty));
132             sb.append(generateMethod(JRExpression.EVALUATION_ESTIMATED, empty));
133         }
134         else
135         {
136             sb.append(generateMethod(JRExpression.EVALUATION_OLD, expressions));
137             sb.append(generateMethod(JRExpression.EVALUATION_ESTIMATED, expressions));
138         }
139
140         generateScriptEnd(sb);
141
142         return sb.toString();
143     }
144
145
146     protected final void generateScriptStart(StringBuffer JavaDoc sb)
147     {
148         /* */
149         sb.append("//\n");
150         sb.append("// Generated by JasperReports - ");
151         sb.append((new SimpleDateFormat JavaDoc()).format(new java.util.Date JavaDoc()));
152         sb.append("\n");
153         sb.append("//\n");
154         sb.append("import net.sf.jasperreports.engine.*;\n");
155         sb.append("import net.sf.jasperreports.engine.fill.*;\n");
156         sb.append("\n");
157         sb.append("import java.util.*;\n");
158         sb.append("import java.math.*;\n");
159         sb.append("import java.text.*;\n");
160         sb.append("import java.io.*;\n");
161         sb.append("import java.net.*;\n");
162         sb.append("\n");
163         
164         /* */
165         String JavaDoc[] imports = sourceTask.getImports();
166         if (imports != null && imports.length > 0)
167         {
168             for (int i = 0; i < imports.length; i++)
169             {
170                 sb.append("import ");
171                 sb.append(imports[i]);
172                 sb.append(";\n");
173             }
174         }
175
176         /* */
177         sb.append("\n");
178         sb.append("\n");
179         sb.append("createBshEvaluator()\n");
180         sb.append("{\n");
181         sb.append("\n");
182         sb.append("\n");
183         sb.append(" JREvaluator evaluator = null;\n");
184         sb.append("\n");
185     }
186
187
188     protected final void generateDeclarations(StringBuffer JavaDoc sb)
189     {
190         /* */
191         if (parametersMap != null && parametersMap.size() > 0)
192         {
193             Collection JavaDoc parameterNames = parametersMap.keySet();
194             for (Iterator JavaDoc it = parameterNames.iterator(); it.hasNext();)
195             {
196                 sb.append(" JRFillParameter parameter_");
197                 sb.append(JRStringUtil.getLiteral((String JavaDoc)it.next()));
198                 sb.append(" = null;\n");
199             }
200         }
201         
202         /* */
203         sb.append("\n");
204
205         /* */
206         if (fieldsMap != null && fieldsMap.size() > 0)
207         {
208             Collection JavaDoc fieldNames = fieldsMap.keySet();
209             for (Iterator JavaDoc it = fieldNames.iterator(); it.hasNext();)
210             {
211                 sb.append(" JRFillField field_");
212                 sb.append(JRStringUtil.getLiteral((String JavaDoc)it.next()));
213                 sb.append(" = null;\n");
214             }
215         }
216         
217         /* */
218         sb.append("\n");
219
220         /* */
221         if (variables != null && variables.length > 0)
222         {
223             for (int i = 0; i < variables.length; i++)
224             {
225                 sb.append(" JRFillVariable variable_");
226                 sb.append(JRStringUtil.getLiteral(variables[i].getName()));
227                 sb.append(" = null;\n");
228             }
229         }
230     }
231
232
233     protected final void generateInitMethod(StringBuffer JavaDoc sb)
234     {
235         /* */
236         sb.append("\n");
237         sb.append("\n");
238         sb.append(" init(\n");
239         sb.append(" JREvaluator evaluator,\n");
240         sb.append(" Map parsm,\n");
241         sb.append(" Map fldsm,\n");
242         sb.append(" Map varsm\n");
243         sb.append(" )\n");
244         sb.append(" {\n");
245         sb.append(" super.evaluator = evaluator;\n");
246         sb.append("\n");
247
248         /* */
249         if (parametersMap != null && parametersMap.size() > 0)
250         {
251             Collection JavaDoc parameterNames = parametersMap.keySet();
252             String JavaDoc parameterName = null;
253             for (Iterator JavaDoc it = parameterNames.iterator(); it.hasNext();)
254             {
255                 parameterName = (String JavaDoc)it.next();
256                 sb.append(" super.parameter_");
257                 sb.append(JRStringUtil.getLiteral(parameterName));
258                 sb.append(" = (JRFillParameter)parsm.get(\"");
259                 sb.append(parameterName);
260                 sb.append("\");\n");
261             }
262         }
263         
264         /* */
265         sb.append("\n");
266
267         /* */
268         if (fieldsMap != null && fieldsMap.size() > 0)
269         {
270             Collection JavaDoc fieldNames = fieldsMap.keySet();
271             String JavaDoc fieldName = null;
272             for (Iterator JavaDoc it = fieldNames.iterator(); it.hasNext();)
273             {
274                 fieldName = (String JavaDoc)it.next();
275                 sb.append(" super.field_");
276                 sb.append(JRStringUtil.getLiteral(fieldName));
277                 sb.append(" = (JRFillField)fldsm.get(\"");
278                 sb.append(fieldName);
279                 sb.append("\");\n");
280             }
281         }
282         
283         /* */
284         sb.append("\n");
285
286         /* */
287         if (variables != null && variables.length > 0)
288         {
289             String JavaDoc variableName = null;
290             for (int i = 0; i < variables.length; i++)
291             {
292                 variableName = variables[i].getName();
293                 sb.append(" super.variable_");
294                 sb.append(JRStringUtil.getLiteral(variableName));
295                 sb.append(" = (JRFillVariable)varsm.get(\"");
296                 sb.append(variableName);
297                 sb.append("\");\n");
298             }
299         }
300
301         /* */
302         sb.append(" }\n");
303     }
304
305
306     protected void generateScriptEnd(StringBuffer JavaDoc sb)
307     {
308         sb.append("\n");
309         sb.append(" str(String key)\n");
310         sb.append(" {\n");
311         sb.append(" return super.evaluator.str(key);\n");
312         sb.append(" }\n");
313         sb.append("\n");
314         sb.append(" msg(String pattern, Object arg0)\n");
315         sb.append(" {\n");
316         sb.append(" return super.evaluator.msg(pattern, arg0);\n");
317         sb.append(" }\n");
318         sb.append("\n");
319         sb.append(" msg(String pattern, Object arg0, Object arg1)\n");
320         sb.append(" {\n");
321         sb.append(" return super.evaluator.msg(pattern, arg0, arg1);\n");
322         sb.append(" }\n");
323         sb.append("\n");
324         sb.append(" msg(String pattern, Object arg0, Object arg1, Object arg2)\n");
325         sb.append(" {\n");
326         sb.append(" return super.evaluator.msg(pattern, arg0, arg1, arg2);\n");
327         sb.append(" }\n");
328         sb.append("\n");
329         sb.append(" msg(String pattern, Object[] args)\n");
330         sb.append(" {\n");
331         sb.append(" return super.evaluator.msg(pattern, args);\n");
332         sb.append(" }\n");
333         sb.append("\n");
334         sb.append(" return this;\n");
335         sb.append("}\n");
336     }
337
338
339     /**
340      *
341      */

342     protected final String JavaDoc generateMethod(byte evaluationType, List JavaDoc expressionsList)
343     {
344         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
345
346         /* */
347         sb.append(" Object evaluate");
348         sb.append((String JavaDoc)methodSuffixMap.get(new Byte JavaDoc(evaluationType)));
349         sb.append("(int id)\n");
350         sb.append(" {\n");
351         sb.append(" Object value = null;\n");
352         sb.append("\n");
353         sb.append(" switch (id)\n");
354         sb.append(" {\n");
355
356         if (expressionsList != null && !expressionsList.isEmpty())
357         {
358             JRExpression expression = null;
359             for (Iterator JavaDoc it = expressionsList.iterator(); it.hasNext();)
360             {
361                 expression = (JRExpression)it.next();
362                 
363                 sb.append(" case ");
364                 sb.append(sourceTask.getExpressionId(expression));
365                 sb.append(" :\n");
366                 sb.append(" {\n");
367                 sb.append(" value = (");
368                 sb.append(expression.getValueClassName());
369                 sb.append(")(");
370                 sb.append(this.generateExpression(expression, evaluationType));
371                 sb.append(");\n");
372                 sb.append(" break;\n");
373                 sb.append(" }\n");
374             }
375         }
376
377         /* */
378         sb.append(" default :\n");
379         sb.append(" {\n");
380         sb.append(" }\n");
381         sb.append(" }\n");
382         sb.append(" \n");
383         sb.append(" return value;\n");
384         sb.append(" }\n");
385         sb.append("\n");
386         sb.append("\n");
387         
388         return sb.toString();
389     }
390
391
392     /**
393      *
394      */

395     private String JavaDoc generateExpression(
396         JRExpression expression,
397         byte evaluationType
398         )
399     {
400         JRParameter jrParameter = null;
401         JRField jrField = null;
402         JRVariable jrVariable = null;
403
404         StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
405
406         JRExpressionChunk[] chunks = expression.getChunks();
407         JRExpressionChunk chunk = null;
408         String JavaDoc chunkText = null;
409         if (chunks != null && chunks.length > 0)
410         {
411             for(int i = 0; i < chunks.length; i++)
412             {
413                 chunk = chunks[i];
414
415                 chunkText = chunk.getText();
416                 if (chunkText == null)
417                 {
418                     chunkText = "";
419                 }
420                 
421                 switch (chunk.getType())
422                 {
423                     case JRExpressionChunk.TYPE_TEXT :
424                     {
425                         sbuffer.append(chunkText);
426                         break;
427                     }
428                     case JRExpressionChunk.TYPE_PARAMETER :
429                     {
430                         jrParameter = (JRParameter)parametersMap.get(chunkText);
431     
432                         sbuffer.append("((");
433                         sbuffer.append(jrParameter.getValueClassName());
434                         sbuffer.append(")super.parameter_");
435                         sbuffer.append(JRStringUtil.getLiteral(chunkText));
436                         sbuffer.append(".getValue())");
437     
438                         break;
439                     }
440                     case JRExpressionChunk.TYPE_FIELD :
441                     {
442                         jrField = (JRField)fieldsMap.get(chunkText);
443     
444                         sbuffer.append("((");
445                         sbuffer.append(jrField.getValueClassName());
446                         sbuffer.append(")super.field_");
447                         sbuffer.append(JRStringUtil.getLiteral(chunkText));
448                         sbuffer.append(".get");
449                         sbuffer.append((String JavaDoc)fieldPrefixMap.get(new Byte JavaDoc(evaluationType)));
450                         sbuffer.append("Value())");
451     
452                         break;
453                     }
454                     case JRExpressionChunk.TYPE_VARIABLE :
455                     {
456                         jrVariable = (JRVariable)variablesMap.get(chunkText);
457     
458                         sbuffer.append("((");
459                         sbuffer.append(jrVariable.getValueClassName());
460                         sbuffer.append(")super.variable_");
461                         sbuffer.append(JRStringUtil.getLiteral(chunkText));
462                         sbuffer.append(".get");
463                         sbuffer.append((String JavaDoc)variablePrefixMap.get(new Byte JavaDoc(evaluationType)));
464                         sbuffer.append("Value())");
465     
466                         break;
467                     }
468                     case JRExpressionChunk.TYPE_RESOURCE :
469                     {
470                         jrParameter = (JRParameter)parametersMap.get(chunkText);
471     
472                         sbuffer.append("super.evaluator.str(\"");
473                         sbuffer.append(chunkText);
474                         sbuffer.append("\")");
475     
476                         break;
477                     }
478                 }
479             }
480         }
481         
482         if (sbuffer.length() == 0)
483         {
484             sbuffer.append("null");
485         }
486
487         return sbuffer.toString();
488     }
489 }
490
Popular Tags