Skip to content

Commit

Permalink
支持语法#{xxx}直接取值节点,不进行表达式计算,例如#{Content-Type}计算结果为取上下文Content-Type节点而不是…
Browse files Browse the repository at this point in the history
…计算两个值相减
  • Loading branch information
FanJiaRui committed Feb 2, 2023
1 parent daf696e commit 7d8cc9f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.20</version>
<version>2.0.22</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/org/fanjr/simplify/el/ELExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,13 @@ private static EL doCompile(String el) {

if (elStart == start && chars[end - 1] == '}' && -1 == findElStart(chars, start + 1, end)) {
start += 2;
elInstance = new SimpleEL(resolve(chars, start, end - 1));
if ('#' == chars[elStart]) {
elInstance = new SimpleEL(doCompileNode(chars, start, end - 1));
} else if ('$' == chars[elStart]) {
elInstance = new SimpleEL(resolve(chars, start, end - 1));
} else {
throw new ElException("解析错误!错误的token:" + chars[elStart] + "{");
}
COMPILES_EL.put(el, elInstance);
if (trim) {
COMPILES_EL.put(trimStr, elInstance);
Expand All @@ -413,10 +419,18 @@ private static EL doCompile(String el) {
while (start < end) {
int elEnd = findNextCharToken(chars, '}', elStart + 2, end);
if (elStart != start) {
// 非表达式部分,解析为字符串
targets.add(StringInvoker.newInstance(new String(chars, start, elStart - start)));
}
elStart += 2;
targets.add(resolve(chars, elStart, elEnd));
if ('#' == chars[elStart]) {
elStart += 2;
targets.add(doCompileNode(chars, elStart, elEnd));
} else if ('$' == chars[elStart]) {
elStart += 2;
targets.add(resolve(chars, elStart, elEnd));
} else {
throw new ElException("解析错误!错误的token:" + chars[elStart] + "{");
}
start = elEnd + 1;
elStart = findElStart(chars, start, end);
if (-1 == elStart) {
Expand Down Expand Up @@ -515,15 +529,21 @@ private static int findChar(char[] chars, char c, int start, int end) {
return -1;
}

/**
* 用于寻找 ${ 或者 #{
*
* @return $ 或者 # 的index
*/
private static int findElStart(char[] chars, int start, int end) {
int elStart = findChar(chars, '$', start, end);
if (elStart == -1) {
// 通过下一个花括号位置来判断是否存在 ${ 或者 #{
int nextCurly = findChar(chars, '{', start, end);
if (nextCurly == -1) {
return -1;
} else {
if (elStart + 1 < end && '{' == chars[elStart + 1]) {
return elStart;
if (nextCurly > start && nextCurly < end && ('$' == chars[nextCurly - 1] || '#' == chars[nextCurly - 1])) {
return nextCurly - 1;
} else {
return findChar(chars, '$', elStart + 1, end);
return findElStart(chars, nextCurly + 2, end);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ private RootNodeInvoker() {
super("this");
}

@Override
public Object invoke(Object ctx) {
return ctx;
}

@Override
public NodeHolder getNodeHolder(Object ctx) {
return NodeHolder.newNodeHolder(ctx, null, this);
Expand All @@ -33,19 +28,24 @@ public NodeHolder getParentNodeHolder(Object ctx) {
}

@Override
public void setValueByParent(NodeHolder parentNode, Object value, int index) {
throw new ElException("不可设置ROOT(this)节点值!");
public Object getValueByParent(Object ctx, NodeHolder parentNode) {
//root节点没有父节点
return ctx;
}

@Override
public Object getValueByParent(Object ctx, NodeHolder parentNode) {
//root节点没有父节点
public Object invoke(Object ctx) {
return ctx;
}

@Override
public void setValueByParent(NodeHolder parentNode, Object value, int index) {
throw new ElException("不可设置ROOT(this)节点值!");
}

@Override
void removeValueByParent(NodeHolder parentNode, int index) {
// skip
logger.info("移除【{}】操作无效,无需移除!", this.toString());
logger.info("移除【{}】操作无效,无需移除!", this);
}
}
46 changes: 25 additions & 21 deletions src/test/java/unit/ElTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ public void baseTest() {
Assertions.assertEquals("rue", ELExecutor.eval("((String)a.b.c.arr[3][a.index]).substring(1)", context, String.class));
Assertions.assertEquals("10012345678901", ELExecutor.eval("((String)1100).substring(1) + (a.b.c.d + 1)", context, String.class));
Assertions.assertEquals("10012345678901", ELExecutor.eval("(((String)1100).substring(1) + a.b.c.d) + 1", context, String.class));
System.out.println(ELExecutor.eval("a.index = (a.index + 5)", context, String.class));
System.out.println(ELExecutor.eval("+ a.index += 5", context, String.class));
System.out.println(ELExecutor.eval("a.index2[a.index] = a.index", context, String.class));
System.out.println(ELExecutor.eval("a.index = (a.index - 5)", context, String.class));
System.out.println(ELExecutor.eval("a.index -= 5", context, String.class));
System.out.println(ELExecutor.eval("a.index2[this.a.index] = a.index", context, String.class));
System.out.println(ELExecutor.eval("a.index = (a.index * (-2))", context, String.class));
System.out.println(ELExecutor.eval("a.index *= \"-2\"", context, String.class));
System.out.println(ELExecutor.eval("-a.index", context, String.class));
System.out.println(ELExecutor.eval("a.index2[a.index] = a.index", context, String.class));
System.out.println(ELExecutor.eval("a.index2", context, String.class));
Assertions.assertEquals("6", ELExecutor.eval("a.index = (a.index + 5)", context, String.class));
Assertions.assertEquals("11",ELExecutor.eval("+ a.index += 5", context, String.class));
Assertions.assertEquals("11",ELExecutor.eval("a.index2[a.index] = a.index", context, String.class));
Assertions.assertEquals("6",ELExecutor.eval("a.index = (a.index - 5)", context, String.class));
Assertions.assertEquals("1",ELExecutor.eval("a.index -= 5", context, String.class));
Assertions.assertEquals("1",ELExecutor.eval("a.index2[this.a.index] = a.index", context, String.class));
Assertions.assertEquals("-2",ELExecutor.eval("a.index = (a.index * (-2))", context, String.class));
Assertions.assertEquals("4",ELExecutor.eval("a.index *= \"-2\"", context, String.class));
Assertions.assertEquals("-4",ELExecutor.eval("-a.index", context, String.class));
Assertions.assertEquals("4",ELExecutor.eval("a.index2[a.index] = a.index", context, String.class));
Assertions.assertEquals("[null,1,null,null,4,null,null,null,null,null,null,11]",ELExecutor.eval("a.index2", context, String.class));
Assertions.assertTrue(ELExecutor.eval("true||false", context, boolean.class));
Assertions.assertTrue(ELExecutor.eval("false||true", context, boolean.class));
Assertions.assertFalse(ELExecutor.eval("true&&false", context, boolean.class));
Expand All @@ -144,16 +144,20 @@ public void baseTest() {
Assertions.assertTrue(ELExecutor.eval("num=0;num-- + --num == -2", context, boolean.class));
Assertions.assertTrue(ELExecutor.eval("num=0;num-- + ++num == 0", context, boolean.class));

System.out.println(ELExecutor.eval("this.num=1;num++;num*=3;num-=2;", context, String.class));
System.out.println(ELExecutor.eval("num", context, String.class));
System.out.println(ELExecutor.eval("num=1;", context, String.class));
System.out.println(ELExecutor.eval("${num}", context, String.class));
System.out.println(ELExecutor.eval("${num}${-num}x${num}${num++}", context, String.class));
System.out.println(ELExecutor.eval("${num}xxx", context, String.class));
System.out.println(ELExecutor.eval("xxx${num}", context, String.class));
System.out.println(ELExecutor.eval("xxx${num}xx", context, String.class));
System.out.println(ELExecutor.eval("(4/2).floatValue()==2", context, String.class));
System.out.println(ELExecutor.eval("map=new com.alibaba.fastjson2.JSONObject();map.b=2;map.a=1;map", context, String.class));
Assertions.assertEquals("4", (ELExecutor.eval("this.num=1;num++;num*=3;num-=2;", context, String.class)));
Assertions.assertEquals("4", ELExecutor.eval("num", context, String.class));
Assertions.assertEquals("1", ELExecutor.eval("num=1;", context, String.class));
Assertions.assertEquals("1", ELExecutor.eval("${num}", context, String.class));
Assertions.assertEquals("1", ELExecutor.eval("#{num}", context, String.class));
Assertions.assertEquals("1-1x11", ELExecutor.eval("#{num}${-num}x#{num}${num++}", context, String.class));
Assertions.assertEquals("2xxx", ELExecutor.eval("${num}xxx", context, String.class));
Assertions.assertEquals("2xxx", ELExecutor.eval("#{num}xxx", context, String.class));
Assertions.assertEquals("xxx2", ELExecutor.eval("xxx${num}", context, String.class));
Assertions.assertEquals("xxx2", ELExecutor.eval("xxx#{num}", context, String.class));
Assertions.assertEquals("xxx2xx", ELExecutor.eval("xxx${num}xx", context, String.class));
Assertions.assertEquals("xxx2xx", ELExecutor.eval("xxx#{num}xx", context, String.class));
Assertions.assertEquals("true", ELExecutor.eval("(4/2).floatValue()==2", context, String.class));
Assertions.assertEquals("{\"b\":2,\"a\":1}", ELExecutor.eval("map=new com.alibaba.fastjson2.JSONObject();map.b=2;map.a=1;map", context, String.class));
System.out.println(ELExecutor.eval("this", context, String.class));

ELExecutor.putNode(context, "pojo", new TestPojo());
Expand Down

0 comments on commit 7d8cc9f

Please sign in to comment.