2 * Copyright 2014 Hanei Management Co.,Ltd.
\r
4 * This file is part of Jaxcel
\r
6 * Jaxcel is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU Lesser General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Jaxcel is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU Lesser General Public License for more details.
\r
16 * You should have received a copy of the GNU Lesser General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
19 package org.hanei.jaxcel.report;
\r
21 import java.text.SimpleDateFormat;
\r
22 import java.util.Calendar;
\r
23 import java.util.Date;
\r
24 import java.util.Map;
\r
25 import java.util.Map.Entry;
\r
27 import org.apache.commons.jexl2.Expression;
\r
28 import org.apache.commons.jexl2.JexlContext;
\r
29 import org.apache.commons.jexl2.JexlEngine;
\r
30 import org.apache.commons.jexl2.MapContext;
\r
31 import org.slf4j.Logger;
\r
32 import org.slf4j.LoggerFactory;
\r
35 * EL式の解析、解析で使用するパラメータオブジェクトの保持を行う。<br>
\r
39 * @author noboru saito
\r
42 public class ELManager {
\r
44 private static final Logger log = LoggerFactory.getLogger(ELManager.class);
\r
46 private final JexlEngine elEngine = new JexlEngine();
\r
47 private final JexlContext elContext = new MapContext();
\r
48 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
\r
53 public ELManager() {}
\r
58 * @param parameter テンプレートに挿入するデータ
\r
60 public ELManager(Map<String, Object> parameter) {
\r
61 setParameter(parameter);
\r
65 * EL式解析で使用するオブジェクトをパラメータとしてセット
\r
67 * @param parameter テンプレートに挿入するデータ
\r
69 public void setParameter(Map<String, Object> parameter) {
\r
70 log.trace("setParameter start");
\r
72 if(parameter != null) {
\r
73 if(parameter.entrySet().size() == 0) {
\r
74 log.debug("parameter is no entry");
\r
77 for(Entry<String, Object> p : parameter.entrySet()) {
\r
78 elContext.set(p.getKey(), p.getValue());
\r
79 log.debug("parameter set: [{}]: {} {}", p.getKey(), p.getValue() == null ? "null" : p.getValue().getClass().getName(), p.getValue() == null ? "" : p.getValue().toString());
\r
84 log.debug("parameter is null");
\r
87 log.trace("setParameter end");
\r
93 * @param expression EL式
\r
96 public Object evaluate(String expression) {
\r
97 log.trace("evaluate start");
\r
99 Object object = null;
\r
101 if(expression == null) {
\r
102 log.debug("expression is null");
\r
105 log.debug("expression: {}", expression);
\r
108 expr = elEngine.createExpression(expression);
\r
109 object = expr.evaluate(elContext);
\r
111 catch(Exception e) {
\r
112 log.error("evaluate error: {}", e.getLocalizedMessage(), e);
\r
116 if(object == null) {
\r
117 log.debug("evaluate result is null");
\r
120 if(log.isDebugEnabled()) {
\r
121 log.debug("evaluate result: {}", object.toString());
\r
122 log.debug("evaluate result type: {}", object.getClass().getName());
\r
124 // Date、Calendarならデフォルトフォーマットに置換
\r
125 if(object instanceof Date) {
\r
126 object = sdf.format(object);
\r
128 else if(object instanceof Calendar) {
\r
129 // デフォルトロケールで文字列に置き換え
\r
130 object = sdf.format(((Calendar)object).getTime());
\r
134 log.trace("evaluate end");
\r