1919import com .api .jsonata4java .expressions .ParseException ;
2020import com .api .jsonata4java .expressions .functions .DeclaredFunction ;
2121import com .api .jsonata4java .expressions .generated .MappingExpressionParser .ExprContext ;
22+ import com .api .jsonata4java .expressions .regex .RegexEngine ;
2223import com .fasterxml .jackson .databind .JsonNode ;
2324import com .fasterxml .jackson .databind .ObjectMapper ;
2425import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
@@ -90,6 +91,25 @@ public static Expression jsonata(String expression) throws ParseException, IOExc
9091 return new Expression (expression );
9192 }
9293
94+ /**
95+ * Generate a new Expression based on evaluating the supplied expression,
96+ * using a custom regex engine to compile any regex literals or dynamic
97+ * string patterns encountered (e.g. for $match/$replace/$split) instead of
98+ * the default {@link java.util.regex.Pattern}-backed one.
99+ *
100+ * @param expression
101+ * the logic to be parsed for later execution via the evaluate
102+ * methods
103+ * @param regexEngine
104+ * the regex engine to use
105+ * @return new Expression object
106+ * @throws ParseException
107+ * @throws IOException
108+ */
109+ public static Expression jsonata (String expression , RegexEngine regexEngine ) throws ParseException , IOException {
110+ return new Expression (expression , regexEngine );
111+ }
112+
93113 /**
94114 * Testing the various methods based on
95115 * https://docs.jsonata.org/embedding-extending#expressionregisterfunctionname-implementation-signature
@@ -143,10 +163,11 @@ public static void main(String[] args) {
143163 ExpressionsVisitor _eval = null ;
144164 Expressions _expr = null ;
145165 Map <String , ExprContext > _variableMap = new HashMap <String , ExprContext >();
166+ RegexEngine _regexEngine = RegexEngine .defaultEngine ();
146167
147168 /**
148169 * Constructor for Expression
149- *
170+ *
150171 * @param expression
151172 * the logic to be parsed for later execution via evaluate
152173 * methods
@@ -158,6 +179,26 @@ public Expression(String expression) throws ParseException, IOException {
158179 _eval = _expr .getExpr ();
159180 }
160181
182+ /**
183+ * Constructor for Expression, using a custom regex engine to compile any
184+ * regex literals or dynamic string patterns encountered (e.g. for
185+ * $match/$replace/$split) instead of the default
186+ * {@link java.util.regex.Pattern}-backed one.
187+ *
188+ * @param expression
189+ * the logic to be parsed for later execution via evaluate
190+ * methods
191+ * @param regexEngine
192+ * the regex engine to use
193+ * @throws ParseException
194+ * @throws IOException
195+ */
196+ public Expression (String expression , RegexEngine regexEngine ) throws ParseException , IOException {
197+ _regexEngine = regexEngine != null ? regexEngine : RegexEngine .defaultEngine ();
198+ _expr = Expressions .parse (expression , _regexEngine );
199+ _eval = _expr .getExpr ();
200+ }
201+
161202 /**
162203 * Assign the binding to the environment preparing for evaluation
163204 *
@@ -210,7 +251,7 @@ public void clear() {
210251 * @throws ParseException
211252 */
212253 public JsonNode evaluate (JsonNode rootContext ) throws ParseException {
213- ExpressionsVisitor eval = new ExpressionsVisitor (rootContext , new FrameEnvironment (null ));
254+ ExpressionsVisitor eval = new ExpressionsVisitor (rootContext , new FrameEnvironment (null ), _regexEngine );
214255 // process any stored bindings
215256 for (Iterator <String > it = _variableMap .keySet ().iterator (); it .hasNext ();) {
216257 String key = it .next ();
@@ -359,7 +400,7 @@ public JsonNode evaluate(JsonNode rootContext, List<Binding> bindings, long time
359400 * If the given device event is invalid.
360401 */
361402 public JsonNode evaluate (JsonNode rootContext , long timeoutMS , int maxDepth ) throws EvaluateException {
362- ExpressionsVisitor eval = new ExpressionsVisitor (rootContext , new FrameEnvironment (null ));
403+ ExpressionsVisitor eval = new ExpressionsVisitor (rootContext , new FrameEnvironment (null ), _regexEngine );
363404 // process any stored bindings
364405 for (Iterator <String > it = _variableMap .keySet ().iterator (); it .hasNext ();) {
365406 String key = it .next ();
0 commit comments