org.apache.commons.jexl3.JexlInfo Java Examples

The following examples show how to use org.apache.commons.jexl3.JexlInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JexlSelectorTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testPrettyExceptionMsgNoDetail() {
  // Setup
  String expected = "at line 2 column 4";

  JexlInfo info = new JexlInfo("", 2, 4);
  // Mocked because JexlException modifies msg internally after construction
  JexlException ex = mock(JexlException.class);
  doReturn(info).when(ex).getInfo();
  doReturn("").when(ex).getMessage();

  // Execute
  String returned = JexlEngine.expandExceptionDetail(ex);

  // Verify
  assertNotNull("Returned string was not set.", returned);
  assertEquals(expected, returned);
}
 
Example #2
Source File: JexlEngine.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns detail about the given JEXL exception, expanded to make it more readable.
 */
public static String expandExceptionDetail(final JexlException e) {
  StringBuilder detailBuilder = new StringBuilder(e.getMessage());

  JexlInfo info = e.getInfo();
  if (info != null) {
    // remove condensed header, replaced below with something more readable
    Matcher matcher = JEXL_CONDENSED_INFO_HEADER.matcher(detailBuilder);
    if (matcher.find()) {
      detailBuilder.delete(matcher.start(), matcher.end());
    }

    // add more detail if we have it and it's not already part of the message
    Optional<String> detail = ofNullable(info.getDetail()).map(Object::toString);
    if (detail.isPresent() && detailBuilder.indexOf(detail.get()) < 0) {
      addContext(detailBuilder, format("in '%s'", detail.get()));
    }

    // finally add the location in a more readable form
    addContext(detailBuilder, format("at line %d column %d", info.getLine(), info.getColumn()));
  }

  return detailBuilder.toString();
}
 
Example #3
Source File: TemplateEngine.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a JxltEngine.Exception from a JexlException.
 * @param info   the source info
 * @param action createExpression, prepare, evaluate
 * @param expr   the template expression
 * @param xany   the exception
 * @return an exception containing an explicit error message
 */
static Exception createException(JexlInfo info, String action, TemplateExpression expr, java.lang.Exception xany) {
    StringBuilder strb = new StringBuilder("failed to ");
    strb.append(action);
    if (expr != null) {
        strb.append(" '");
        strb.append(expr.toString());
        strb.append("'");
    }
    Throwable cause = xany.getCause();
    if (cause != null) {
        String causeMsg = cause.getMessage();
        if (causeMsg != null) {
            strb.append(", ");
            strb.append(causeMsg);
        }
    }
    return new Exception(info, strb.toString(), xany);
}
 
Example #4
Source File: TemplateEngine.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
public JxltEngine.Expression createExpression(JexlInfo info, String expression) {
    if (info == null) {
        info = jexl.createInfo();
    }
    Exception xuel = null;
    TemplateExpression stmt = null;
    try {
        stmt = cache.get(expression);
        if (stmt == null) {
            stmt = parseExpression(info, expression, null);
            cache.put(expression, stmt);
        }
    } catch (JexlException xjexl) {
        xuel = new Exception(xjexl.getInfo(), "failed to parse '" + expression + "'", xjexl);
    }
    if (xuel != null) {
        if (jexl.isSilent()) {
            jexl.logger.warn(xuel.getMessage(), xuel.getCause());
            stmt = null;
        } else {
            throw xuel;
        }
    }
    return stmt;
}
 
Example #5
Source File: JexlParser.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
/**
 * Declares a local parameter.
 * <p> This method creates an new entry in the symbol map. </p>
 * @param token the parameter name toekn
 */
protected void declareParameter(Token token) {
    String identifier =  token.image;
    if (!allowVariable(identifier)) {
        throwFeatureException(JexlFeatures.LOCAL_VAR, token);
    }
    if (frame == null) {
        frame = new Scope(null, (String[]) null);
    }
    int symbol = frame.declareParameter(identifier);
    // not sure how declaring a parameter could fail...
    // lexical feature error
    if (!block.declareSymbol(symbol) && getFeatures().isLexical()) {
        JexlInfo xinfo = info.at(token.beginLine, token.beginColumn);
        throw new JexlException(xinfo,  identifier + ": variable is already declared", null);
    }
}
 
Example #6
Source File: JexlParser.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a parsing exception.
 * @param xclazz the class of exception
 * @param tok the token to report
 * @param <T> the parsing exception subclass
 */
protected <T extends JexlException.Parsing> void throwParsingException(Class<T> xclazz, Token tok) {
    JexlInfo xinfo  = null;
    String msg = "unrecoverable state";
    JexlException.Parsing xparse = null;
    if (tok == null) {
        tok = this.getToken(0);
    }
    if (tok != null) {
        xinfo = info.at(tok.beginLine, tok.beginColumn);
        msg = tok.image;
        if (xclazz != null) {
            try {
                Constructor<T> ctor = xclazz.getConstructor(JexlInfo.class, String.class);
                xparse = ctor.newInstance(xinfo, msg);
            } catch (Exception xany) {
                // ignore, very unlikely but then again..
            }
        }
    }
    // unlikely but safe
    throw xparse != null ? xparse : new JexlException.Parsing(xinfo, msg);
}
 
Example #7
Source File: Interpreter.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
@Override
protected Object visit(ASTJxltLiteral node, Object data) {
    TemplateEngine.TemplateExpression tp = (TemplateEngine.TemplateExpression) node.jjtGetValue();
    if (tp == null) {
        TemplateEngine jxlt = jexl.jxlt();
        JexlInfo info = node.jexlInfo();
        if (this.block != null) {
            info = new JexlNode.Info(node, info);
        }
        tp = jxlt.parseExpression(info, node.getLiteral(), frame != null ? frame.getScope() : null);
        node.jjtSetValue(tp);
    }
    if (tp != null) {
        return tp.evaluate(frame, context);
    }
    return null;
}
 
Example #8
Source File: TemplateInterpreter.java    From commons-jexl with Apache License 2.0 6 votes vote down vote up
/**
 * Prints to output.
 * <p>
 * This will dynamically try to find the best suitable method in the writer through uberspection.
 * Subclassing Writer by adding 'print' methods should be the preferred way to specialize output.
 * </p>
 * @param info the source info
 * @param arg  the argument to print out
 */
private void doPrint(JexlInfo info, Object arg) {
    try {
        if (writer != null) {
            if (arg instanceof CharSequence) {
                writer.write(arg.toString());
            } else if (arg != null) {
                Object[] value = {arg};
                JexlUberspect uber = jexl.getUberspect();
                JexlMethod method = uber.getMethod(writer, "print", value);
                if (method != null) {
                    method.invoke(writer, value);
                } else {
                    writer.write(arg.toString());
                }
            }
        }
    } catch (java.io.IOException xio) {
        throw TemplateEngine.createException(info, "call print", null, xio);
    } catch (java.lang.Exception xany) {
        throw TemplateEngine.createException(info, "invoke print", null, xany);
    }
}
 
Example #9
Source File: ScriptStepTest.java    From gp2srv with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testExecScript() {
	ScriptStep scriptStep = new ScriptStep(ScriptStepType.EXEC_SCRIPT, null, "x=2+2", null);
	JexlEngine mockEngine = Mockito.mock(JexlEngine.class);
	JexlScript jexlScript = Mockito.mock(JexlScript.class);
	Mockito.when(mockEngine.createScript(Mockito.any(JexlInfo.class), Mockito.anyString(), Mockito.any(String[].class))).thenReturn(jexlScript);
	Assert.assertFalse(scriptStep.execute(null, 12345, mockEngine, jexlContext, null, null));
	Mockito.verify(mockEngine).createScript(null, "x=2+2", null);
	Mockito.verify(jexlScript).execute(jexlContext);
}
 
Example #10
Source File: JexlParser.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Throws Ambiguous exception.
 * <p>Seeks the end of the ambiguous statement to recover.
 * @param node the first token in ambiguous expression
 */
protected void throwAmbiguousException(JexlNode node) {
    JexlInfo begin = node.jexlInfo();
    Token t = getToken(0);
    JexlInfo end = info.at(t.beginLine, t.endColumn);
    String msg = readSourceLine(source, end.getLine());
    throw new JexlException.Ambiguous(begin, end, msg);
}
 
Example #11
Source File: JexlParser.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Throws a feature exception.
 * @param feature the feature code
 * @param token the token that triggered it
 */
protected void throwFeatureException(int feature, Token token) {
    if (token == null) {
        token = this.getToken(0);
        if (token == null) {
            throw new JexlException.Parsing(null, JexlFeatures.stringify(feature));
        }
    }
    JexlInfo xinfo = info.at(token.beginLine, token.beginColumn);
    throwFeatureException(feature, xinfo);
}
 
Example #12
Source File: Engine.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
/**
 * Parses an expression.
 *
 * @param info      information structure
 * @param parsingf  the set of parsing features
 * @param src      the expression to parse
 * @param scope     the script frame
 * @return the parsed tree
 * @throws JexlException if any error occurred during parsing
 */
protected ASTJexlScript parse(JexlInfo info, JexlFeatures parsingf, String src, Scope scope) {
    final boolean cached = src.length() < cacheThreshold && cache != null;
    final JexlFeatures features = parsingf != null? parsingf : DEFAULT_FEATURES;
    final Source source = cached? new Source(features, src) : null;
    ASTJexlScript script = null;
    if (source != null) {
        script = cache.get(source);
        if (script != null) {
            Scope f = script.getScope();
            if ((f == null && scope == null) || (f != null && f.equals(scope))) {
                return script;
            }
        }
    }
    final JexlInfo ninfo = info == null && debug ? createInfo() : info;
    // if parser not in use...
    if (parsing.compareAndSet(false, true)) {
        try {
            // lets parse
            script = parser.parse(ninfo, features, src, scope);
        } finally {
            // no longer in use
            parsing.set(false);
        }
    } else {
        // ...otherwise parser was in use, create a new temporary one
        Parser lparser = new Parser(new StringReader(";"));
        script = lparser.parse(ninfo, features, src, scope);
    }
    if (source != null) {
        cache.put(source, script);
    }
    return script;
}
 
Example #13
Source File: Engine.java    From commons-jexl with Apache License 2.0 5 votes vote down vote up
@Override
public Script createScript(JexlFeatures features, JexlInfo info, String scriptText, String[] names) {
    if (scriptText == null) {
        throw new NullPointerException("source is null");
    }
    String source = trimSource(scriptText);
    Scope scope = names == null || names.length == 0? null : new Scope(null, names);
    JexlFeatures ftrs = features == null? scriptFeatures : features;
    ASTJexlScript tree = parse(info, ftrs, source, scope);
    return new Script(this, source, tree);
}
 
Example #14
Source File: TemplateEngine.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public TemplateScript createTemplate(JexlInfo info, String prefix, Reader source, String... parms) {
    return new TemplateScript(this, info, prefix, source,  parms);
}
 
Example #15
Source File: TemplateEngine.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
JexlInfo getInfo() {
    return node.jexlInfo();
}
 
Example #16
Source File: TemplateEngine.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
/** @return the info */
JexlInfo getInfo() {
    return null;
}
 
Example #17
Source File: Engine.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public Script createExpression(JexlInfo info, String expression) {
    return createScript(expressionFeatures, info, expression, null);
}
 
Example #18
Source File: Script.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
/**
 * @return the info
 */
public JexlInfo getInfo() {
    return script.jexlInfo();
}
 
Example #19
Source File: JexlNode.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public JexlInfo detach() {
    node = null;
    return this;
}
 
Example #20
Source File: JexlNode.java    From commons-jexl with Apache License 2.0 4 votes vote down vote up
@Override
public JexlInfo at(int l, int c) {
    return new Info(node, getName(), l, c);
}
 
Example #21
Source File: Engine.java    From commons-jexl with Apache License 2.0 2 votes vote down vote up
/**
 * Parses an expression.
 *
 * @param info      information structure
 * @param expr     whether we parse an expression or a feature
 * @param src      the expression to parse
 * @param scope     the script frame
 * @return the parsed tree
 * @throws JexlException if any error occurred during parsing
 */
protected ASTJexlScript parse(JexlInfo info, boolean expr, String src, Scope scope) {
    return parse(info, expr? this.expressionFeatures : this.scriptFeatures, src, scope);
}
 
Example #22
Source File: JexlParser.java    From commons-jexl with Apache License 2.0 2 votes vote down vote up
/**
 * Throws a feature exception.
 * @param feature the feature code
 * @param info the exception surroundings
 */
protected void throwFeatureException(int feature, JexlInfo info) {
    String msg = info != null? readSourceLine(source, info.getLine()) : null;
    throw new JexlException.Feature(info, feature, msg);
}
 
Example #23
Source File: JexlNode.java    From commons-jexl with Apache License 2.0 2 votes vote down vote up
/**
 * Copy ctor.
 * @param jnode the node
 * @param info the 
 */
public Info(JexlNode jnode, JexlInfo info) {
    this(jnode, info.getName(), info.getLine(), info.getColumn());
}
 
Example #24
Source File: FeatureController.java    From commons-jexl with Apache License 2.0 2 votes vote down vote up
/**
 * Throws a feature exception.
 * @param feature the feature code
 * @param node    the node that caused it
 */
public void throwFeatureException(int feature, JexlNode node) {
    JexlInfo dbgInfo = node.jexlInfo();
    throw new JexlException.Feature(dbgInfo, feature, "");
}