From 9bfbb8ee480b5ce8f3dbacdfcaf62a4bfa5e84fe Mon Sep 17 00:00:00 2001 From: Sergeev Viktor Date: Tue, 7 Jul 2026 21:07:49 +0300 Subject: [PATCH] add slf4j --- pom/pom.xml | 5 +++ .../java/com/dashjoin/jsonata/Functions.java | 15 ++++----- .../java/com/dashjoin/jsonata/Jsonata.java | 24 ++++++++------ .../java/com/dashjoin/jsonata/Parser.java | 32 ++++++++++--------- src/main/java/module-info.java | 1 + 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/pom/pom.xml b/pom/pom.xml index b007da8..8614213 100644 --- a/pom/pom.xml +++ b/pom/pom.xml @@ -37,6 +37,11 @@ + + org.slf4j + slf4j-api + 2.0.18 + org.junit.jupiter junit-jupiter diff --git a/src/main/java/com/dashjoin/jsonata/Functions.java b/src/main/java/com/dashjoin/jsonata/Functions.java index c564805..1ec3331 100644 --- a/src/main/java/com/dashjoin/jsonata/Functions.java +++ b/src/main/java/com/dashjoin/jsonata/Functions.java @@ -666,7 +666,7 @@ public static List evaluateMatcher(Pattern matcher, String str) { while (m.find()) { RegexpMatch rm = new RegexpMatch(); - //System.out.println("grc="+m.groupCount()+" "+m.group(1)); + //log.info("grc={} {}", m.groupCount(), m.group(1)); rm.index = m.start(); rm.match = m.group(); @@ -700,7 +700,7 @@ public static Boolean contains(String str, Object token) { result = (str.indexOf((String)token) != -1); } else if (token instanceof Pattern) { var matches = evaluateMatcher((Pattern)token, str); - //if (dbg) System.out.println("match = "+matches); + //log.debug("match = {}", matches); //result = (typeof matches !== 'undefined'); //throw new Error("regexp not impl"); //result = false; result = !matches.isEmpty(); @@ -1134,7 +1134,7 @@ public static String formatNumber(Number value, String picture, Map options) { fixedPicture = fixedPicture.replace("e", "E"); littleE = true; } - //System.out.println("picture "+fixedPicture); + //log.info("picture {}", fixedPicture); formatter.applyLocalizedPattern(fixedPicture); String result = formatter.format(value); @@ -2145,8 +2145,8 @@ public static Method getFunction(Class clz, String name) { Method[] methods = clz.getMethods(); for (Method m : methods) { // if (m.getModifiers() == (Modifier.STATIC | Modifier.PUBLIC) ) { - // System.out.println(m.getName()); - // System.out.println(m.getParameterTypes()); + // log.info(m.getName()); + // log.info(m.getParameterTypes()); // } if (m.getName().equals(name)) { return m; @@ -2176,7 +2176,7 @@ public static Object call(Object instance, Method m, List args) throws T if (arg1!=null) { List wrap = new ArrayList<>(); wrap.add(arg1); callArgs.set(0, wrap); - //System.err.println("wrapped "+arg1+" as "+wrap); + //log.error("wrapped {} as {}"+, arg1, wrap); } } @@ -2344,8 +2344,7 @@ public static Number parseInteger(String value, String picture) throws ParseExce throw new ParseException("Formatting or parsing an integer as a sequence starting with \""+ picture +"\" is not supported by this implementation", 0); } catch (Exception ex) { // Ignore the exception, return null - // System.err.println("Exception in parseInteger (returning null): " + ex); - // ex.printStackTrace(); + // log.error("Exception in parseInteger (returning null): ", ex); return null; } } diff --git a/src/main/java/com/dashjoin/jsonata/Jsonata.java b/src/main/java/com/dashjoin/jsonata/Jsonata.java index 575279b..3b8d804 100644 --- a/src/main/java/com/dashjoin/jsonata/Jsonata.java +++ b/src/main/java/com/dashjoin/jsonata/Jsonata.java @@ -45,6 +45,8 @@ import com.dashjoin.jsonata.Parser.Symbol; import com.dashjoin.jsonata.Utils.JList; import com.dashjoin.jsonata.utils.Signature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @module JSONata @@ -52,6 +54,8 @@ */ @SuppressWarnings({"rawtypes", "unchecked"}) public class Jsonata { + + private static final Logger log = LoggerFactory.getLogger(Jsonata.class); // Start of Evaluator code @@ -150,7 +154,7 @@ Object _evaluate(Symbol expr, Object input, Frame environment) { this.input = input; this.environment = environment; - if (parser.dbg) System.out.println("eval expr="+expr+" type="+expr.type);//+" input="+input); + log.debug("eval expr={} type={}", expr, expr.type);//+" input="+input); var entryCallback = environment.lookup("__evaluate_entry"); if(entryCallback!=null) { @@ -170,7 +174,7 @@ Object _evaluate(Symbol expr, Object input, Frame environment) { break; case "name": result = evaluateName(expr, input, environment); - if (parser.dbg) System.out.println("evalName "+result); + log.debug("evalName {}"); break; case "string": case "number": @@ -676,7 +680,7 @@ public Object call() throws Exception { if(expr.consarray) { if (!(result instanceof JList)) result = new JList((List)result); - //System.out.println("const "+result); + //log.info("const {}", result); ((JList)result).cons = true; } break; @@ -837,7 +841,7 @@ Object evaluateNumericExpression(Object _lhs, Object _rhs, String op) { return null; } - //System.out.println("op22 "+op+" "+_lhs+" "+_rhs); + //log.info("op22 {} {} {}", op, _lhs, _rhs); double lhs = ((Number)_lhs).doubleValue(); double rhs = ((Number)_rhs).doubleValue(); @@ -1303,7 +1307,7 @@ Object evaluateVariable(Symbol expr, Object input, Frame environment) { result = input instanceof JList && ((JList)input).outerWrapper ? ((JList)input).get(0) : input; } else { result = environment.lookup((String)expr.value); - if (parser.dbg) System.out.println("variable name="+expr.value+" val="+result); + log.debug("variable name={} val={}", expr.value, result); } return result; } @@ -1757,7 +1761,7 @@ Jsonata getPerThreadInstance() { // result = /* await */ result; // } } else if (proc instanceof JLambda) { - // System.err.println("Lambda "+proc); + // log.error("Lambda {}", proc); List _args = (List)validatedArgs; if (proc instanceof Fn0) { result = ((Fn0)proc).get(); @@ -1769,7 +1773,7 @@ Jsonata getPerThreadInstance() { } else if (proc instanceof Pattern) { List _res = new ArrayList<>(); for (Object s : (List)validatedArgs) { - //System.err.println("PAT "+proc+" input "+s); + //log.error("PAT {} input {}", proc, s); if (s instanceof String) { Matcher matcher = ((Pattern) proc).matcher((String) s); _res.add(regexClosure(matcher)); @@ -1781,7 +1785,7 @@ Jsonata getPerThreadInstance() { result = _res; } } else { - System.out.println("Proc not found "+proc); + log.warn("Proc not found {}", proc); throw new JException( "T1006", 0 //stack: (new Error()).stack @@ -1989,7 +1993,7 @@ Object partialApplyNativeFunction(JFunction _native, List args) { var body = "function(" + String.join(", ", sigArgs) + "){"; body += "$"+_native.functionName+"("+String.join(", ", sigArgs) + ") }"; - if (parser.dbg) System.out.println("partial trampoline = "+body); + log.debug("partial trampoline = {}", body); // var sigArgs = getNativeFunctionArguments(_native); // sigArgs = sigArgs.stream().map(sigArg -> { @@ -2232,7 +2236,7 @@ public JFunction(String functionName, String signature, Class clz, Object instan this.method = Functions.getFunction(clz, implMethodName); this.methodInstance = instance; if (method==null) { - System.err.println("Function not implemented: "+functionName+" impl="+implMethodName); + log.error("Function not implemented: {} impl={}", functionName, implMethodName); } } diff --git a/src/main/java/com/dashjoin/jsonata/Parser.java b/src/main/java/com/dashjoin/jsonata/Parser.java index 5749d5c..0fe2011 100644 --- a/src/main/java/com/dashjoin/jsonata/Parser.java +++ b/src/main/java/com/dashjoin/jsonata/Parser.java @@ -38,12 +38,14 @@ import com.dashjoin.jsonata.Jsonata.Frame; import com.dashjoin.jsonata.Tokenizer.Token; import com.dashjoin.jsonata.utils.Signature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; //var parseSignature = require('./signature'); @SuppressWarnings({"unchecked"}) public class Parser implements Serializable { - boolean dbg = false; + private static final Logger log = LoggerFactory.getLogger(Parser.class); // This parser implements the 'Top down operator precedence' algorithm developed by Vaughan R Pratt; http://dl.acm.org/citation.cfm?id=512931. // and builds on the Javascript framework described by Douglas Crockford at http://javascript.crockford.com/tdop/tdop.html @@ -212,11 +214,11 @@ public Symbol create() { // We want a shallow clone (do not duplicate outer class!) try { Symbol cl = (Symbol) this.clone(); - //System.err.println("cloning "+this+" clone="+cl); + //log.info("cloning {} clone={}", this, cl); return cl; } catch (CloneNotSupportedException e) { // never reached - if (dbg) e.printStackTrace(); + log.debug("Error when clone object", e); return null; } } @@ -232,10 +234,10 @@ void register(Symbol t) { Symbol s = symbolTable.get(t.id); if (s != null) { - if (dbg) System.out.println("Symbol in table "+t.id+" "+s.getClass().getName()+" -> "+ t.getClass().getName()); + log.debug("Symbol in table {} {} -> {}", t.id, s.getClass().getName(), t.getClass().getName()); //symbolTable.put(t.id, t); if (t.bp >= s.lbp) { - if (dbg) System.out.println("Symbol in table "+t.id+" lbp="+s.lbp+" -> "+t.bp); + log.debug("Symbol in table {} lbp={} -> {}", t.id, s.lbp, t.bp); s.lbp = t.bp; } } else { @@ -281,7 +283,7 @@ Symbol advance(String id, boolean infix) { return handleError(err); } Token next_token = lexer.next(infix); - if (dbg) System.out.println("nextToken "+(next_token!=null ? next_token.type : null)); + log.debug("nextToken {}", next_token!=null ? next_token.type : null); if (next_token == null) { node = symbolTable.get("(end)"); node.position = source.length(); @@ -322,7 +324,7 @@ Symbol advance(String id, boolean infix) { node.value = value; node.type = type; node.position = next_token.position; - if (dbg) System.out.println("advance "+node); + log.debug("advance {}", node); return node; } @@ -335,7 +337,7 @@ Symbol expression(int rbp) { while (rbp < node.lbp) { t = node; advance(null, false); - if (dbg) System.out.println("t="+t+", left="+left.type); + log.debug("t={}, left={}", t, left.type); left = t.led(left); } return left; @@ -618,7 +620,7 @@ Symbol led(Symbol left) { //register(new Prefix("(") { @Override Symbol nud() { - if (dbg) System.out.println("Prefix ("); + log.debug("Prefix ("); List expressions = new ArrayList<>(); while (!node.id.equals(")")) { expressions.add(Parser.this.expression(0)); @@ -981,7 +983,7 @@ void resolveAncestry(Symbol path) { Symbol processAST(Symbol expr) { Symbol result = expr; if (expr==null) return null; - if (dbg) System.out.println(" > processAST type="+expr.type+" value='"+expr.value+"'"); + log.debug(" > processAST type={} value='{}'", expr.type, expr.value); switch (expr.type != null ? expr.type : "(null)") { case "binary": { switch (""+expr.value) { @@ -1028,11 +1030,11 @@ Symbol processAST(Symbol expr) { step.value ); } - //System.out.println("step "+step+" type="+step.type); + //log.info("step {} type={}", step, step.type); if (step.type.equals("string")) step.type = "name"; // for (var lit : step.steps) { - // System.out.println("step2 "+lit+" type="+lit.type); + // log.info("step2 {} type={}", lit, lit.type); // lit.type = "name"; // } } @@ -1056,7 +1058,7 @@ Symbol processAST(Symbol expr) { resolveAncestry(result); break; case "[": - if (dbg) System.out.println("binary ["); + log.debug("binary ["); // predicated step // LHS is a step or a predicated step // RHS is the predicate expr @@ -1236,7 +1238,7 @@ Symbol processAST(Symbol expr) { // expr.value might be Character! String exprValue = ""+expr.value; if (exprValue.equals("[")) { - if (dbg) System.out.println("unary [ "+result); + log.debug("unary [ {}", result); // array constructor - process each item final Symbol _result = result; result.expressions = expr.expressions.stream().map(item -> { @@ -1263,7 +1265,7 @@ Symbol processAST(Symbol expr) { if (exprValue.equals("-") && result.expression.type.equals("number")) { result = result.expression; result.value = Utils.convertNumber( -((Number)result.value).doubleValue() ); - if (dbg) System.out.println("unary - value="+result.value); + log.debug("unary - value={}", result.value); } else { pushAncestry(result, result.expression); } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 4bd689a..41c0593 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,6 +1,7 @@ module com.dashjoin.jsonata { requires java.compiler; requires java.management; + requires org.slf4j; exports com.dashjoin.jsonata; exports com.dashjoin.jsonata.json;