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:
029: /*
030: * Contributors:
031: * Gaganis Giorgos - gaganis@users.sourceforge.net
032: */
033: package net.sf.jasperreports.compilers;
034:
035: import java.text.SimpleDateFormat;
036: import java.util.ArrayList;
037: import java.util.Collection;
038: import java.util.HashMap;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042:
043: import net.sf.jasperreports.engine.JRExpression;
044: import net.sf.jasperreports.engine.JRExpressionChunk;
045: import net.sf.jasperreports.engine.JRField;
046: import net.sf.jasperreports.engine.JRParameter;
047: import net.sf.jasperreports.engine.JRVariable;
048: import net.sf.jasperreports.engine.design.JRSourceCompileTask;
049: import net.sf.jasperreports.engine.util.JRStringUtil;
050:
051: /**
052: * @author Teodor Danciu (teodord@users.sourceforge.net)
053: * @version $Id: JRBshGenerator.java 1777 2007-07-11 11:39:40Z lucianc $
054: */
055: public class JRBshGenerator {
056:
057: private static Map fieldPrefixMap = null;
058: private static Map variablePrefixMap = null;
059: private static Map methodSuffixMap = null;
060:
061: /**
062: *
063: */
064: protected final JRSourceCompileTask sourceTask;
065:
066: protected Map parametersMap;
067: protected Map fieldsMap;
068: protected Map variablesMap;
069: protected JRVariable[] variables;
070:
071: static {
072: fieldPrefixMap = new HashMap();
073: fieldPrefixMap
074: .put(new Byte(JRExpression.EVALUATION_OLD), "Old");
075: fieldPrefixMap.put(new Byte(JRExpression.EVALUATION_ESTIMATED),
076: "");
077: fieldPrefixMap.put(new Byte(JRExpression.EVALUATION_DEFAULT),
078: "");
079:
080: variablePrefixMap = new HashMap();
081: variablePrefixMap.put(new Byte(JRExpression.EVALUATION_OLD),
082: "Old");
083: variablePrefixMap.put(new Byte(
084: JRExpression.EVALUATION_ESTIMATED), "Estimated");
085: variablePrefixMap.put(
086: new Byte(JRExpression.EVALUATION_DEFAULT), "");
087:
088: methodSuffixMap = new HashMap();
089: methodSuffixMap.put(new Byte(JRExpression.EVALUATION_OLD),
090: "Old");
091: methodSuffixMap.put(
092: new Byte(JRExpression.EVALUATION_ESTIMATED),
093: "Estimated");
094: methodSuffixMap.put(new Byte(JRExpression.EVALUATION_DEFAULT),
095: "");
096: }
097:
098: protected JRBshGenerator(JRSourceCompileTask sourceTask) {
099: this .sourceTask = sourceTask;
100:
101: this .parametersMap = sourceTask.getParametersMap();
102: this .fieldsMap = sourceTask.getFieldsMap();
103: this .variablesMap = sourceTask.getVariablesMap();
104: this .variables = sourceTask.getVariables();
105: }
106:
107: /**
108: *
109: */
110: public static String generateScript(JRSourceCompileTask sourceTask) {
111: JRBshGenerator generator = new JRBshGenerator(sourceTask);
112: return generator.generateScript();
113: }
114:
115: protected String generateScript() {
116: StringBuffer sb = new StringBuffer();
117:
118: generateScriptStart(sb);
119:
120: generateDeclarations(sb);
121: generateInitMethod(sb);
122:
123: sb.append("\n");
124: sb.append("\n");
125:
126: List expressions = sourceTask.getExpressions();
127: sb.append(generateMethod(JRExpression.EVALUATION_DEFAULT,
128: expressions));
129: if (sourceTask.isOnlyDefaultEvaluation()) {
130: List empty = new ArrayList();
131: sb
132: .append(generateMethod(JRExpression.EVALUATION_OLD,
133: empty));
134: sb.append(generateMethod(JRExpression.EVALUATION_ESTIMATED,
135: empty));
136: } else {
137: sb.append(generateMethod(JRExpression.EVALUATION_OLD,
138: expressions));
139: sb.append(generateMethod(JRExpression.EVALUATION_ESTIMATED,
140: expressions));
141: }
142:
143: generateScriptEnd(sb);
144:
145: return sb.toString();
146: }
147:
148: protected final void generateScriptStart(StringBuffer sb) {
149: /* */
150: sb.append("//\n");
151: sb.append("// Generated by JasperReports - ");
152: sb
153: .append((new SimpleDateFormat())
154: .format(new java.util.Date()));
155: sb.append("\n");
156: sb.append("//\n");
157: sb.append("import net.sf.jasperreports.engine.*;\n");
158: sb.append("import net.sf.jasperreports.engine.fill.*;\n");
159: sb.append("\n");
160: sb.append("import java.util.*;\n");
161: sb.append("import java.math.*;\n");
162: sb.append("import java.text.*;\n");
163: sb.append("import java.io.*;\n");
164: sb.append("import java.net.*;\n");
165: sb.append("\n");
166:
167: /* */
168: String[] imports = sourceTask.getImports();
169: if (imports != null && imports.length > 0) {
170: for (int i = 0; i < imports.length; i++) {
171: sb.append("import ");
172: sb.append(imports[i]);
173: sb.append(";\n");
174: }
175: }
176:
177: /* */
178: sb.append("\n");
179: sb.append("\n");
180: sb.append("createBshEvaluator()\n");
181: sb.append("{\n");
182: sb.append("\n");
183: sb.append("\n");
184: sb.append(" JREvaluator evaluator = null;\n");
185: sb.append("\n");
186: }
187:
188: protected final void generateDeclarations(StringBuffer sb) {
189: /* */
190: if (parametersMap != null && parametersMap.size() > 0) {
191: Collection parameterNames = parametersMap.keySet();
192: for (Iterator it = parameterNames.iterator(); it.hasNext();) {
193: sb.append(" JRFillParameter parameter_");
194: sb.append(JRStringUtil.getLiteral((String) it.next()));
195: sb.append(" = null;\n");
196: }
197: }
198:
199: /* */
200: sb.append("\n");
201:
202: /* */
203: if (fieldsMap != null && fieldsMap.size() > 0) {
204: Collection fieldNames = fieldsMap.keySet();
205: for (Iterator it = fieldNames.iterator(); it.hasNext();) {
206: sb.append(" JRFillField field_");
207: sb.append(JRStringUtil.getLiteral((String) it.next()));
208: sb.append(" = null;\n");
209: }
210: }
211:
212: /* */
213: sb.append("\n");
214:
215: /* */
216: if (variables != null && variables.length > 0) {
217: for (int i = 0; i < variables.length; i++) {
218: sb.append(" JRFillVariable variable_");
219: sb.append(JRStringUtil.getLiteral(variables[i]
220: .getName()));
221: sb.append(" = null;\n");
222: }
223: }
224: }
225:
226: protected final void generateInitMethod(StringBuffer sb) {
227: /* */
228: sb.append("\n");
229: sb.append("\n");
230: sb.append(" init(\n");
231: sb.append(" JREvaluator evaluator,\n");
232: sb.append(" Map parsm,\n");
233: sb.append(" Map fldsm,\n");
234: sb.append(" Map varsm\n");
235: sb.append(" )\n");
236: sb.append(" {\n");
237: sb.append(" super.evaluator = evaluator;\n");
238: sb.append("\n");
239:
240: /* */
241: if (parametersMap != null && parametersMap.size() > 0) {
242: Collection parameterNames = parametersMap.keySet();
243: String parameterName = null;
244: for (Iterator it = parameterNames.iterator(); it.hasNext();) {
245: parameterName = (String) it.next();
246: sb.append(" super.parameter_");
247: sb.append(JRStringUtil.getLiteral(parameterName));
248: sb.append(" = (JRFillParameter)parsm.get(\"");
249: sb.append(parameterName);
250: sb.append("\");\n");
251: }
252: }
253:
254: /* */
255: sb.append("\n");
256:
257: /* */
258: if (fieldsMap != null && fieldsMap.size() > 0) {
259: Collection fieldNames = fieldsMap.keySet();
260: String fieldName = null;
261: for (Iterator it = fieldNames.iterator(); it.hasNext();) {
262: fieldName = (String) it.next();
263: sb.append(" super.field_");
264: sb.append(JRStringUtil.getLiteral(fieldName));
265: sb.append(" = (JRFillField)fldsm.get(\"");
266: sb.append(fieldName);
267: sb.append("\");\n");
268: }
269: }
270:
271: /* */
272: sb.append("\n");
273:
274: /* */
275: if (variables != null && variables.length > 0) {
276: String variableName = null;
277: for (int i = 0; i < variables.length; i++) {
278: variableName = variables[i].getName();
279: sb.append(" super.variable_");
280: sb.append(JRStringUtil.getLiteral(variableName));
281: sb.append(" = (JRFillVariable)varsm.get(\"");
282: sb.append(variableName);
283: sb.append("\");\n");
284: }
285: }
286:
287: /* */
288: sb.append(" }\n");
289: }
290:
291: protected void generateScriptEnd(StringBuffer sb) {
292: sb.append("\n");
293: sb.append(" str(String key)\n");
294: sb.append(" {\n");
295: sb.append(" return super.evaluator.str(key);\n");
296: sb.append(" }\n");
297: sb.append("\n");
298: sb.append(" msg(String pattern, Object arg0)\n");
299: sb.append(" {\n");
300: sb
301: .append(" return super.evaluator.msg(pattern, arg0);\n");
302: sb.append(" }\n");
303: sb.append("\n");
304: sb
305: .append(" msg(String pattern, Object arg0, Object arg1)\n");
306: sb.append(" {\n");
307: sb
308: .append(" return super.evaluator.msg(pattern, arg0, arg1);\n");
309: sb.append(" }\n");
310: sb.append("\n");
311: sb
312: .append(" msg(String pattern, Object arg0, Object arg1, Object arg2)\n");
313: sb.append(" {\n");
314: sb
315: .append(" return super.evaluator.msg(pattern, arg0, arg1, arg2);\n");
316: sb.append(" }\n");
317: sb.append("\n");
318: sb.append(" msg(String pattern, Object[] args)\n");
319: sb.append(" {\n");
320: sb
321: .append(" return super.evaluator.msg(pattern, args);\n");
322: sb.append(" }\n");
323: sb.append("\n");
324: sb.append(" return this;\n");
325: sb.append("}\n");
326: }
327:
328: /**
329: *
330: */
331: protected final String generateMethod(byte evaluationType,
332: List expressionsList) {
333: StringBuffer sb = new StringBuffer();
334:
335: /* */
336: sb.append(" Object evaluate");
337: sb.append((String) methodSuffixMap
338: .get(new Byte(evaluationType)));
339: sb.append("(int id)\n");
340: sb.append(" {\n");
341: sb.append(" Object value = null;\n");
342: sb.append("\n");
343: sb.append(" switch (id)\n");
344: sb.append(" {\n");
345:
346: if (expressionsList != null && !expressionsList.isEmpty()) {
347: JRExpression expression = null;
348: for (Iterator it = expressionsList.iterator(); it.hasNext();) {
349: expression = (JRExpression) it.next();
350:
351: sb.append(" case ");
352: sb.append(sourceTask.getExpressionId(expression));
353: sb.append(" :\n");
354: sb.append(" {\n");
355: sb.append(" value = (");
356: sb.append(expression.getValueClassName());
357: sb.append(")(");
358: sb.append(this .generateExpression(expression,
359: evaluationType));
360: sb.append(");\n");
361: sb.append(" break;\n");
362: sb.append(" }\n");
363: }
364: }
365:
366: /* */
367: sb.append(" default :\n");
368: sb.append(" {\n");
369: sb.append(" }\n");
370: sb.append(" }\n");
371: sb.append(" \n");
372: sb.append(" return value;\n");
373: sb.append(" }\n");
374: sb.append("\n");
375: sb.append("\n");
376:
377: return sb.toString();
378: }
379:
380: /**
381: *
382: */
383: private String generateExpression(JRExpression expression,
384: byte evaluationType) {
385: JRParameter jrParameter = null;
386: JRField jrField = null;
387: JRVariable jrVariable = null;
388:
389: StringBuffer sbuffer = new StringBuffer();
390:
391: JRExpressionChunk[] chunks = expression.getChunks();
392: JRExpressionChunk chunk = null;
393: String chunkText = null;
394: if (chunks != null && chunks.length > 0) {
395: for (int i = 0; i < chunks.length; i++) {
396: chunk = chunks[i];
397:
398: chunkText = chunk.getText();
399: if (chunkText == null) {
400: chunkText = "";
401: }
402:
403: switch (chunk.getType()) {
404: case JRExpressionChunk.TYPE_TEXT: {
405: sbuffer.append(chunkText);
406: break;
407: }
408: case JRExpressionChunk.TYPE_PARAMETER: {
409: jrParameter = (JRParameter) parametersMap
410: .get(chunkText);
411:
412: sbuffer.append("((");
413: sbuffer.append(jrParameter.getValueClassName());
414: sbuffer.append(")super.parameter_");
415: sbuffer.append(JRStringUtil.getLiteral(chunkText));
416: sbuffer.append(".getValue())");
417:
418: break;
419: }
420: case JRExpressionChunk.TYPE_FIELD: {
421: jrField = (JRField) fieldsMap.get(chunkText);
422:
423: sbuffer.append("((");
424: sbuffer.append(jrField.getValueClassName());
425: sbuffer.append(")super.field_");
426: sbuffer.append(JRStringUtil.getLiteral(chunkText));
427: sbuffer.append(".get");
428: sbuffer.append((String) fieldPrefixMap
429: .get(new Byte(evaluationType)));
430: sbuffer.append("Value())");
431:
432: break;
433: }
434: case JRExpressionChunk.TYPE_VARIABLE: {
435: jrVariable = (JRVariable) variablesMap
436: .get(chunkText);
437:
438: sbuffer.append("((");
439: sbuffer.append(jrVariable.getValueClassName());
440: sbuffer.append(")super.variable_");
441: sbuffer.append(JRStringUtil.getLiteral(chunkText));
442: sbuffer.append(".get");
443: sbuffer.append((String) variablePrefixMap
444: .get(new Byte(evaluationType)));
445: sbuffer.append("Value())");
446:
447: break;
448: }
449: case JRExpressionChunk.TYPE_RESOURCE: {
450: jrParameter = (JRParameter) parametersMap
451: .get(chunkText);
452:
453: sbuffer.append("super.evaluator.str(\"");
454: sbuffer.append(chunkText);
455: sbuffer.append("\")");
456:
457: break;
458: }
459: }
460: }
461: }
462:
463: if (sbuffer.length() == 0) {
464: sbuffer.append("null");
465: }
466:
467: return sbuffer.toString();
468: }
469: }
|