1
1
package com .jayway .jsonpath .filter ;
2
2
3
3
import com .jayway .jsonpath .JsonUtil ;
4
+ import com .jayway .jsonpath .eval .Expression ;
4
5
import org .json .simple .JSONArray ;
5
6
6
- import javax .script .ScriptEngine ;
7
- import javax .script .ScriptEngineManager ;
8
- import javax .script .ScriptException ;
7
+ //import javax.script.ScriptEngine;
8
+ //import javax.script.ScriptEngineManager;
9
9
import java .util .LinkedList ;
10
10
import java .util .List ;
11
11
import java .util .Map ;
20
20
*/
21
21
public class ListFilter extends JsonPathFilterBase {
22
22
23
- private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager ().getEngineByName ("js" );
23
+ // private static ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("js");
24
24
25
25
private static final Pattern LIST_INDEX_PATTERN = Pattern .compile ("\\ [(\\ s?\\ d+\\ s?,?)+\\ ]" ); //[1] OR [1,2,3]
26
26
private static final Pattern LIST_PULL_PATTERN = Pattern .compile ("\\ [\\ s?:(\\ d+)\\ s?\\ ]" ); //[ :2 ]
@@ -51,8 +51,7 @@ public List<Object> apply(List<Object> items) {
51
51
return filterByPullIndex (items );
52
52
} else if (LIST_ITEM_HAS_PROPERTY_PATTERN .matcher (pathFragment ).matches ()) {
53
53
return filterByItemProperty (items );
54
- }
55
- else if (LIST_ITEM_MATCHES_EVAL .matcher (pathFragment ).matches ()) {
54
+ } else if (LIST_ITEM_MATCHES_EVAL .matcher (pathFragment ).matches ()) {
56
55
return filterByItemEvalMatch (items );
57
56
}
58
57
@@ -64,7 +63,7 @@ private List<Object> filterByItemEvalMatch(List<Object> items) {
64
63
65
64
for (Object current : items ) {
66
65
for (Object item : JsonUtil .toList (current )) {
67
- if (isEvalMatch (item )){
66
+ if (isEvalMatch (item )) {
68
67
result .add (item );
69
68
}
70
69
}
@@ -81,8 +80,8 @@ private List<Object> filterByItemProperty(List<Object> items) {
81
80
for (Object current : items ) {
82
81
for (Object item : JsonUtil .toList (current )) {
83
82
84
- if (JsonUtil .isMap (item )){
85
- if (JsonUtil .toMap (item ).containsKey (prop )) {
83
+ if (JsonUtil .isMap (item )) {
84
+ if (JsonUtil .toMap (item ).containsKey (prop )) {
86
85
result .add (item );
87
86
}
88
87
}
@@ -142,43 +141,31 @@ private boolean isEvalMatch(Object check) {
142
141
Matcher matcher = LIST_ITEM_MATCHES_EVAL .matcher (pathFragment );
143
142
144
143
if (matcher .matches ()) {
145
- String property = matcher .group (1 );
146
- String operator = matcher .group (2 );
147
- String expected = matcher .group (3 );
144
+ String property = matcher .group (1 );
145
+ String operator = matcher .group (2 );
146
+ String expected = matcher .group (3 );
148
147
149
- if (!JsonUtil .isMap (check )){
148
+ if (!JsonUtil .isMap (check )) {
150
149
return false ;
151
150
}
152
151
Map obj = JsonUtil .toMap (check );
153
152
154
- if (!obj .containsKey (property )){
153
+ if (!obj .containsKey (property )) {
155
154
return false ;
156
155
}
157
156
158
157
Object propertyValue = obj .get (property );
159
158
160
- if (JsonUtil .isContainer (propertyValue )){
159
+ if (JsonUtil .isContainer (propertyValue )) {
161
160
return false ;
162
161
}
163
162
164
-
165
- if (propertyValue instanceof String ){
166
- propertyValue = "'" + propertyValue + "'" ;
167
- }
168
-
169
- if (operator .trim ().equals ("=" )){
170
- operator = "===" ;
171
- }
172
-
173
-
174
163
String expression = propertyValue + " " + operator + " " + expected ;
175
164
System .out .println ("EVAL" + expression );
176
165
177
- try {
178
- return (Boolean ) SCRIPT_ENGINE .eval (expression );
179
- } catch (ScriptException e ) {
180
- return false ;
181
- }
166
+
167
+ return Expression .eval (propertyValue , operator , expected );
168
+
182
169
}
183
170
return false ;
184
171
}
0 commit comments