1 28 29 33 package net.sf.jasperreports.compilers; 34 35 import java.text.SimpleDateFormat ; 36 import java.util.ArrayList ; 37 import java.util.Collection ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Map ; 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 56 public class JRBshGenerator 57 { 58 59 private static Map fieldPrefixMap = null; 60 private static Map variablePrefixMap = null; 61 private static Map methodSuffixMap = null; 62 63 64 67 protected final JRSourceCompileTask sourceTask; 68 69 protected Map parametersMap; 70 protected Map fieldsMap; 71 protected Map variablesMap; 72 protected JRVariable[] variables; 73 74 static 75 { 76 fieldPrefixMap = new HashMap (); 77 fieldPrefixMap.put(new Byte (JRExpression.EVALUATION_OLD), "Old"); 78 fieldPrefixMap.put(new Byte (JRExpression.EVALUATION_ESTIMATED), ""); 79 fieldPrefixMap.put(new Byte (JRExpression.EVALUATION_DEFAULT), ""); 80 81 variablePrefixMap = new HashMap (); 82 variablePrefixMap.put(new Byte (JRExpression.EVALUATION_OLD), "Old"); 83 variablePrefixMap.put(new Byte (JRExpression.EVALUATION_ESTIMATED), "Estimated"); 84 variablePrefixMap.put(new Byte (JRExpression.EVALUATION_DEFAULT), ""); 85 86 methodSuffixMap = new HashMap (); 87 methodSuffixMap.put(new Byte (JRExpression.EVALUATION_OLD), "Old"); 88 methodSuffixMap.put(new Byte (JRExpression.EVALUATION_ESTIMATED), "Estimated"); 89 methodSuffixMap.put(new Byte (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 107 public static String generateScript(JRSourceCompileTask sourceTask) 108 { 109 JRBshGenerator generator = new JRBshGenerator(sourceTask); 110 return generator.generateScript(); 111 } 112 113 114 protected String generateScript() 115 { 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, expressions)); 128 if (sourceTask.isOnlyDefaultEvaluation()) 129 { 130 List empty = new ArrayList (); 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 sb) 147 { 148 149 sb.append("//\n"); 150 sb.append("// Generated by JasperReports - "); 151 sb.append((new SimpleDateFormat ()).format(new java.util.Date ())); 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 [] 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 sb) 189 { 190 191 if (parametersMap != null && parametersMap.size() > 0) 192 { 193 Collection parameterNames = parametersMap.keySet(); 194 for (Iterator it = parameterNames.iterator(); it.hasNext();) 195 { 196 sb.append(" JRFillParameter parameter_"); 197 sb.append(JRStringUtil.getLiteral((String )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 fieldNames = fieldsMap.keySet(); 209 for (Iterator it = fieldNames.iterator(); it.hasNext();) 210 { 211 sb.append(" JRFillField field_"); 212 sb.append(JRStringUtil.getLiteral((String )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 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 parameterNames = parametersMap.keySet(); 252 String parameterName = null; 253 for (Iterator it = parameterNames.iterator(); it.hasNext();) 254 { 255 parameterName = (String )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 fieldNames = fieldsMap.keySet(); 271 String fieldName = null; 272 for (Iterator it = fieldNames.iterator(); it.hasNext();) 273 { 274 fieldName = (String )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 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 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 342 protected final String generateMethod(byte evaluationType, List expressionsList) 343 { 344 StringBuffer sb = new StringBuffer (); 345 346 347 sb.append(" Object evaluate"); 348 sb.append((String )methodSuffixMap.get(new Byte (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 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 395 private String generateExpression( 396 JRExpression expression, 397 byte evaluationType 398 ) 399 { 400 JRParameter jrParameter = null; 401 JRField jrField = null; 402 JRVariable jrVariable = null; 403 404 StringBuffer sbuffer = new StringBuffer (); 405 406 JRExpressionChunk[] chunks = expression.getChunks(); 407 JRExpressionChunk chunk = null; 408 String 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 )fieldPrefixMap.get(new Byte (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 )variablePrefixMap.get(new Byte (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 |