PageRenderTime 34ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jasperreports-3.7.4/src/net/sf/jasperreports/compilers/JavaScriptEvaluator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 347 lines | 262 code | 39 blank | 46 comment | 13 complexity | 65f26bb657fc93a1840f9326fc0bb0fe MD5 | raw file
  1. /*
  2. * JasperReports - Free Java Reporting Library.
  3. * Copyright (C) 2001 - 2009 Jaspersoft Corporation. All rights reserved.
  4. * http://www.jaspersoft.com
  5. *
  6. * Unless you have purchased a commercial license agreement from Jaspersoft,
  7. * the following license terms apply:
  8. *
  9. * This program is part of JasperReports.
  10. *
  11. * JasperReports is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * JasperReports is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package net.sf.jasperreports.compilers;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.Map;
  28. import net.sf.jasperreports.engine.JRException;
  29. import net.sf.jasperreports.engine.JRExpression;
  30. import net.sf.jasperreports.engine.JRExpressionChunk;
  31. import net.sf.jasperreports.engine.JRRuntimeException;
  32. import net.sf.jasperreports.engine.fill.JREvaluator;
  33. import net.sf.jasperreports.engine.fill.JRFillField;
  34. import net.sf.jasperreports.engine.fill.JRFillParameter;
  35. import net.sf.jasperreports.engine.fill.JRFillVariable;
  36. import net.sf.jasperreports.engine.util.JRClassLoader;
  37. import net.sf.jasperreports.engine.util.JRStringUtil;
  38. import org.mozilla.javascript.Context;
  39. import org.mozilla.javascript.ContextFactory;
  40. import org.mozilla.javascript.EvaluatorException;
  41. import org.mozilla.javascript.Script;
  42. import org.mozilla.javascript.ScriptableObject;
  43. /**
  44. * JavaScript expression evaluator.
  45. *
  46. * @author Lucian Chirita (lucianc@users.sourceforge.net)
  47. * @version $Id: JavaScriptEvaluator.java 3717 2010-04-09 10:01:33Z teodord $
  48. */
  49. public class JavaScriptEvaluator extends JREvaluator
  50. {
  51. /**
  52. * Base JavaScript value class.
  53. */
  54. public abstract static class JSValue
  55. {
  56. private final ScriptableObject scope;
  57. protected JSValue(ScriptableObject scope)
  58. {
  59. this.scope = scope;
  60. }
  61. protected final Object toJSValue(Object value)
  62. {
  63. return Context.javaToJS(value, scope);
  64. }
  65. }
  66. /**
  67. * Parameter class used in JavaScript expressions.
  68. */
  69. public static class JSParameter extends JSValue
  70. {
  71. private final JRFillParameter parameter;
  72. public JSParameter(JRFillParameter parameter, ScriptableObject scope)
  73. {
  74. super(scope);
  75. this.parameter = parameter;
  76. }
  77. public Object getValue()
  78. {
  79. return toJSValue(parameter.getValue());
  80. }
  81. }
  82. /**
  83. * Field class used in JavaScript expressions.
  84. */
  85. public static class JSField extends JSValue
  86. {
  87. private final JRFillField field;
  88. public JSField(JRFillField field, ScriptableObject scope)
  89. {
  90. super(scope);
  91. this.field = field;
  92. }
  93. public Object getValue()
  94. {
  95. return toJSValue(field.getValue());
  96. }
  97. public Object getOldValue()
  98. {
  99. return toJSValue(field.getOldValue());
  100. }
  101. }
  102. /**
  103. * Variable class used in JavaScript expressions.
  104. */
  105. public static class JSVariable extends JSValue
  106. {
  107. private final JRFillVariable variable;
  108. public JSVariable(JRFillVariable variable, ScriptableObject scope)
  109. {
  110. super(scope);
  111. this.variable = variable;
  112. }
  113. public Object getValue()
  114. {
  115. return toJSValue(variable.getValue());
  116. }
  117. public Object getOldValue()
  118. {
  119. return toJSValue(variable.getOldValue());
  120. }
  121. public Object getEstimatedValue()
  122. {
  123. return toJSValue(variable.getEstimatedValue());
  124. }
  125. }
  126. protected static JavaScriptCompileData.Expression createJSExpression(JRExpression expression)
  127. {
  128. StringBuffer defaultExpr = new StringBuffer();
  129. StringBuffer oldExpr = new StringBuffer();
  130. StringBuffer estimatedExpr = new StringBuffer();
  131. JRExpressionChunk[] chunks = expression.getChunks();
  132. if (chunks == null)
  133. {
  134. defaultExpr.append("null");
  135. oldExpr.append("null");
  136. estimatedExpr.append("null");
  137. }
  138. else
  139. {
  140. for (int i = 0; i < chunks.length; i++)
  141. {
  142. JRExpressionChunk chunk = chunks[i];
  143. switch (chunk.getType())
  144. {
  145. case JRExpressionChunk.TYPE_TEXT:
  146. defaultExpr.append(chunk.getText());
  147. oldExpr.append(chunk.getText());
  148. estimatedExpr.append(chunk.getText());
  149. break;
  150. case JRExpressionChunk.TYPE_PARAMETER:
  151. String paramName = getParameterVar(chunk.getText());
  152. defaultExpr.append(paramName);
  153. defaultExpr.append(".getValue()");
  154. oldExpr.append(paramName);
  155. oldExpr.append(".getValue()");
  156. estimatedExpr.append(paramName);
  157. estimatedExpr.append(".getValue()");
  158. break;
  159. case JRExpressionChunk.TYPE_VARIABLE:
  160. String varName = getVariableVar(chunk.getText());
  161. defaultExpr.append(varName);
  162. defaultExpr.append(".getValue()");
  163. oldExpr.append(varName);
  164. oldExpr.append(".getOldValue()");
  165. estimatedExpr.append(varName);
  166. estimatedExpr.append(".getEstimatedValue()");
  167. break;
  168. case JRExpressionChunk.TYPE_FIELD:
  169. String fieldName = getFieldVar(chunk.getText());
  170. defaultExpr.append(fieldName);
  171. defaultExpr.append(".getValue()");
  172. oldExpr.append(fieldName);
  173. oldExpr.append(".getOldValue()");
  174. estimatedExpr.append(fieldName);
  175. estimatedExpr.append(".getValue()");
  176. break;
  177. }
  178. }
  179. }
  180. return new JavaScriptCompileData.Expression(expression.getValueClassName(),
  181. defaultExpr.toString(), estimatedExpr.toString(), oldExpr.toString());
  182. }
  183. protected static String getParameterVar(String name)
  184. {
  185. return "param_" + JRStringUtil.getJavaIdentifier(name);
  186. }
  187. protected static String getVariableVar(String name)
  188. {
  189. return "var_" + JRStringUtil.getJavaIdentifier(name);
  190. }
  191. protected static String getFieldVar(String name)
  192. {
  193. return "field_" + JRStringUtil.getJavaIdentifier(name);
  194. }
  195. private final JavaScriptCompileData compileData;
  196. private Context context;
  197. private ScriptableObject scope;
  198. private Map loadedTypes = new HashMap();
  199. private Map compiledExpressions = new HashMap();
  200. /**
  201. * Create a JavaScript expression evaluator.
  202. *
  203. * @param compileData the report compile data
  204. */
  205. public JavaScriptEvaluator(JavaScriptCompileData compileData)
  206. {
  207. this.compileData = compileData;
  208. }
  209. protected void customizedInit(Map parametersMap, Map fieldsMap,
  210. Map variablesMap) throws JRException
  211. {
  212. context = ContextFactory.getGlobal().enterContext();//TODO exit context
  213. context.getWrapFactory().setJavaPrimitiveWrap(false);
  214. scope = context.initStandardObjects();
  215. for (Iterator it = parametersMap.entrySet().iterator(); it.hasNext();)
  216. {
  217. Map.Entry entry = (Map.Entry) it.next();
  218. String name = (String) entry.getKey();
  219. JRFillParameter param = (JRFillParameter) entry.getValue();
  220. JSParameter jsParam = new JSParameter(param, scope);
  221. scope.put(getParameterVar(name), scope, jsParam);
  222. }
  223. for (Iterator it = variablesMap.entrySet().iterator(); it.hasNext();)
  224. {
  225. Map.Entry entry = (Map.Entry) it.next();
  226. String name = (String) entry.getKey();
  227. JRFillVariable var = (JRFillVariable) entry.getValue();
  228. JSVariable jsVar = new JSVariable(var, scope);
  229. scope.put(getVariableVar(name), scope, jsVar);
  230. }
  231. if (fieldsMap != null)
  232. {
  233. for (Iterator it = fieldsMap.entrySet().iterator(); it.hasNext();)
  234. {
  235. Map.Entry entry = (Map.Entry) it.next();
  236. String name = (String) entry.getKey();
  237. JRFillField field = (JRFillField) entry.getValue();
  238. JSField jsField = new JSField(field, scope);
  239. scope.put(getFieldVar(name), scope, jsField);
  240. }
  241. }
  242. }
  243. protected Object evaluate(int id) throws Throwable //NOSONAR
  244. {
  245. JavaScriptCompileData.Expression expression = getExpression(id);
  246. return evaluateExpression(expression.getJavaType(),
  247. expression.getDefaultExpression());
  248. }
  249. protected Object evaluateEstimated(int id) throws Throwable //NOSONAR
  250. {
  251. JavaScriptCompileData.Expression expression = getExpression(id);
  252. return evaluateExpression(expression.getJavaType(),
  253. expression.getEstimatedExpression());
  254. }
  255. protected Object evaluateOld(int id) throws Throwable //NOSONAR
  256. {
  257. JavaScriptCompileData.Expression expression = getExpression(id);
  258. return evaluateExpression(expression.getJavaType(),
  259. expression.getOldExpression());
  260. }
  261. protected JavaScriptCompileData.Expression getExpression(int id)
  262. {
  263. return compileData.getExpression(id);
  264. }
  265. protected Object evaluateExpression(String type, String expression)
  266. {
  267. Script compiledExpression = getCompiledExpression(expression);
  268. Object value = compiledExpression.exec(context, scope);
  269. Class typeClass = getTypeClass(type);
  270. Object javaValue;
  271. try
  272. {
  273. javaValue = Context.jsToJava(value, typeClass);
  274. }
  275. catch (EvaluatorException e)
  276. {
  277. throw new JRRuntimeException(e);
  278. }
  279. return javaValue;
  280. }
  281. protected Script getCompiledExpression(String expression)
  282. {
  283. Script compiledExpression = (Script) compiledExpressions.get(expression);
  284. if (compiledExpression == null)
  285. {
  286. compiledExpression = context.compileString(expression, "expression", 0, null);
  287. compiledExpressions.put(expression, compiledExpression);
  288. }
  289. return compiledExpression;
  290. }
  291. protected Class getTypeClass(String type)
  292. {
  293. Class typeClass = (Class) loadedTypes.get(type);
  294. if (typeClass == null)
  295. {
  296. try
  297. {
  298. typeClass = JRClassLoader.loadClassForName(type);
  299. }
  300. catch (ClassNotFoundException e)
  301. {
  302. throw new JRRuntimeException("Unable to load class " + type, e);
  303. }
  304. loadedTypes.put(type, typeClass);
  305. }
  306. return typeClass;
  307. }
  308. }