Skip to content

Commit

Permalink
foreach env
Browse files Browse the repository at this point in the history
  • Loading branch information
Sayi committed Dec 29, 2020
1 parent 91a4187 commit b187ee3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.deepoove.poi.render.compute;

import java.util.Map;

import com.deepoove.poi.exception.ExpressionEvalException;
import com.deepoove.poi.expression.DefaultEL;

Expand All @@ -28,22 +26,26 @@
public class DefaultELRenderDataCompute implements RenderDataCompute {

private DefaultEL elObject;
private Map<String, Object> env;
private DefaultEL envObject;
private boolean isStrict;

public DefaultELRenderDataCompute(EnvModel model, boolean isStrict) {
this.elObject = DefaultEL.create(model.getRoot());
this.env = model.getEnv();
this.envObject = DefaultEL.create(model.getEnv());
this.isStrict = isStrict;
}

@Override
public Object compute(String el) {
try {
if (null != env) {
Object val = env.get(el);
if (null != val) {
return val;
if (null != envObject) {
try {
Object val = envObject.eval(el);
if (null != val) {
return val;
}
} catch (Exception e) {
// ignore
}
}
return elObject.eval(el);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SpELRenderDataCompute implements RenderDataCompute {

private final ExpressionParser parser;
private final EvaluationContext context;
private Map<String, Object> env;
private EvaluationContext envContext;
private boolean isStrict;

public SpELRenderDataCompute(EnvModel model) {
Expand All @@ -47,20 +47,25 @@ public SpELRenderDataCompute(EnvModel model, boolean isStrict) {

public SpELRenderDataCompute(EnvModel model, boolean isStrict, Map<String, Method> spELFunction) {
this.isStrict = isStrict;
this.env = model.getEnv();
parser = new SpelExpressionParser();
context = new StandardEvaluationContext(model.getRoot());
this.parser = new SpelExpressionParser();
this.envContext = new StandardEvaluationContext(model.getEnv());
this.context = new StandardEvaluationContext(model.getRoot());
((StandardEvaluationContext) context).addPropertyAccessor(new ReadMapAccessor());
((StandardEvaluationContext) envContext).addPropertyAccessor(new ReadMapAccessor());
spELFunction.forEach(((StandardEvaluationContext) context)::registerFunction);
}

@Override
public Object compute(String el) {
try {
if (null != env) {
Object val = env.get(el);
if (null != val) {
return val;
if (null != envContext) {
try {
Object val = parser.parseExpression(el).getValue(envContext);
if (null != val) {
return val;
}
} catch (Exception e) {
// ignore
}
}
return parser.parseExpression(el).getValue(context);
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/deepoove/poi/tl/render/IterableEnvTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.api.Test;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.tl.source.XWPFTestSupport;

public class IterableEnvTest {
Expand All @@ -23,7 +24,7 @@ public void testEnv() throws Exception {
XWPFRun createRun = para.createRun();
createRun.setText("{{?list}}");
createRun = para.createRun();
createRun.setText("index:{{_index}}");
createRun.setText("index:{{_index+1}}");
createRun = para.createRun();
createRun.setText("_is_first:{{_is_first}}");
createRun = para.createRun();
Expand All @@ -37,7 +38,7 @@ public void testEnv() throws Exception {
createRun = para.createRun();
createRun.setText("{{/list}}");

XWPFTemplate template = XWPFTemplate.compile(XWPFTestSupport.readInputStream(doc));
XWPFTemplate template = XWPFTemplate.compile(XWPFTestSupport.readInputStream(doc), Configure.builder().useSpringEL().build());
template.render(new HashMap<String, Object>() {
{
put("list", Arrays.asList("1", "2", "3", "4"));
Expand All @@ -46,7 +47,7 @@ public void testEnv() throws Exception {
XWPFDocument newDocument = XWPFTestSupport.readNewDocument(template);
String text = newDocument.getParagraphArray(0).getText();
assertEquals(
"index:0_is_first:true_is_last:false_has_next:true_is_even_item:false_is_odd_item:trueindex:1_is_first:false_is_last:false_has_next:true_is_even_item:true_is_odd_item:falseindex:2_is_first:false_is_last:false_has_next:true_is_even_item:false_is_odd_item:trueindex:3_is_first:false_is_last:true_has_next:false_is_even_item:true_is_odd_item:false",
"index:1_is_first:true_is_last:false_has_next:true_is_even_item:false_is_odd_item:trueindex:2_is_first:false_is_last:false_has_next:true_is_even_item:true_is_odd_item:falseindex:3_is_first:false_is_last:false_has_next:true_is_even_item:false_is_odd_item:trueindex:4_is_first:false_is_last:true_has_next:false_is_even_item:true_is_odd_item:false",
text);
}

Expand Down

0 comments on commit b187ee3

Please sign in to comment.