use of org.beetl.core.Resource in project beetl2.0 by javamonkey.
the class FunctionExpression method evaluate.
public Object evaluate(Context ctx) {
Function fn = ctx.gt.getFunction(name);
if (fn == null) {
// 检查html实现
Resource resource = getResource(ctx.gt, name);
if (resource.getResourceLoader().exist(resource.getId())) {
fn = new FileFunctionWrapper(resource.getId());
} else {
BeetlException ex = new BeetlException(BeetlException.FUNCTION_NOT_FOUND);
ex.pushToken(token);
throw ex;
}
}
Object[] paras = new Object[exps.length];
for (int i = 0; i < paras.length; i++) {
paras[i] = exps[i].evaluate(ctx);
}
Object value = null;
try {
value = fn.call(paras, ctx);
} catch (BeetlException ex) {
ex.pushToken(token);
throw ex;
} catch (RuntimeException ex) {
BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用方法出错 " + name, ex);
be.pushToken(this.token);
throw be;
}
Object ret = null;
if (vas == null) {
ret = value;
} else {
for (VarAttribute attr : vas) {
try {
value = attr.evaluate(ctx, value);
} catch (BeetlException ex) {
ex.pushToken(attr.token);
throw ex;
} catch (RuntimeException ex) {
BeetlException be = new BeetlException(BeetlException.ATTRIBUTE_INVALID, "属性访问出错", ex);
be.pushToken(attr.token);
throw be;
}
if (value == null) {
if (hasSafe) {
return safeExp == null ? null : safeExp.evaluate(ctx);
} else {
BeetlException be = new BeetlException(BeetlException.ERROR, "空指针 ");
be.pushToken(attr.token);
throw be;
}
}
}
ret = value;
}
if (ret == null && hasSafe) {
return safeExp == null ? null : safeExp.evaluate(ctx);
} else {
return ret;
}
}
Aggregations