use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class WhileStatement method execute.
@Override
public void execute(Context ctx) {
if (this.hasGoto) {
while (true) {
Object result = exp.evaluate(ctx);
if (result instanceof Boolean) {
if ((Boolean) result) {
whileBody.execute(ctx);
switch(ctx.gotoFlag) {
case IGoto.NORMAL:
break;
case IGoto.CONTINUE:
ctx.gotoFlag = IGoto.NORMAL;
continue;
case IGoto.RETURN:
return;
case IGoto.BREAK:
ctx.gotoFlag = IGoto.NORMAL;
return;
}
} else {
break;
}
} else {
BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
be.pushToken(exp.token);
throw be;
}
}
} else {
while (true) {
Object result = exp.evaluate(ctx);
if (result instanceof Boolean) {
if ((Boolean) result) {
whileBody.execute(ctx);
} else {
break;
}
} else {
BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
be.pushToken(exp.token);
throw be;
}
}
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class SwitchStatement method execute.
@Override
public void execute(Context ctx) {
Object o = value.evaluate(ctx);
if (o == null) {
BeetlException ex = new BeetlException(BeetlException.NULL);
ex.pushToken(value.token);
throw ex;
}
boolean isMatch = false;
for (Expression exp : condtionsList) {
if (isMatch || ALU.equals(o, exp.evaluate(ctx))) {
isMatch = true;
BlockStatement block = map.get(exp);
if (block != null) {
block.execute(ctx);
switch(ctx.gotoFlag) {
case IGoto.NORMAL:
break;
case IGoto.RETURN:
return;
case IGoto.BREAK:
ctx.gotoFlag = IGoto.NORMAL;
return;
}
} else {
// 匹配下一个Block
continue;
}
} else {
continue;
}
}
if (!isMatch && defaultBlock != null) {
defaultBlock.execute(ctx);
switch(ctx.gotoFlag) {
case IGoto.NORMAL:
break;
case IGoto.RETURN:
return;
case IGoto.BREAK:
ctx.gotoFlag = IGoto.NORMAL;
return;
}
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class TagVarBindingStatement method runTag.
@Override
protected void runTag(Tag tag, Context ctx) {
try {
if (tag instanceof HTMLTagVarBindingWrapper) {
HTMLTagVarBindingWrapper htmlTag = (HTMLTagVarBindingWrapper) tag;
// 初始化
Object[] vars = htmlTag.bindVars();
if (vars != null) {
for (int i = 0; i < vars.length; i++) {
ctx.vars[varIndexs[i].getVarIndex()] = vars[i];
}
}
LinkedHashMap<String, Integer> indexMap = new LinkedHashMap<String, Integer>(this.varIndexs.length);
for (VarDefineNode node : this.varIndexs) {
indexMap.put(node.token.text, node.varIndex);
}
htmlTag.mapName2Index(indexMap);
} else {
BeetlException be = new BeetlException(BeetlException.ERROR, "tag必须是HTMLTagVarBindingWrapper");
be.pushToken(this.token);
throw be;
}
tag.render();
} catch (BeetlException ex) {
// BeetlException异常时不要设置token,因为抛出的地方已经设置了
throw ex;
} catch (RuntimeException ex) {
BeetlException be = new BeetlException(BeetlException.ERROR, "tag执行抛错", ex);
be.pushToken(token);
throw be;
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class TernaryExpression method evaluate.
public Object evaluate(Context ctx) {
Object value = condtion.evaluate(ctx);
if (value == null) {
BeetlException be = new BeetlException(BeetlException.NULL);
be.pushToken(condtion.token);
throw be;
} else if (!(value instanceof Boolean)) {
BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
be.pushToken(condtion.token);
throw be;
}
boolean cond = (Boolean) value;
if (cond) {
return a.evaluate(ctx);
} else {
if (b != null) {
return b.evaluate(ctx);
} else {
return null;
}
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class VarRef method evaluateUntilLast.
/**
* 计算所有表达式,知道最后一值,用于a.b[xx].c = 1 赋值,只计算到a.b[xx]
* @param ctx
* @return
*/
public Object evaluateUntilLast(Context ctx) {
if (attributes.length == 0) {
// 不可能发生,除非beetl写错了,先放在着
throw new RuntimeException();
}
Object value = ctx.vars[varIndex];
if (value == Context.NOT_EXIST_OBJECT) {
BeetlException ex = new BeetlException(BeetlException.VAR_NOT_DEFINED);
ex.pushToken(this.firstToken);
throw ex;
}
if (value == null) {
BeetlException ex = new BeetlException(BeetlException.NULL);
ex.pushToken(this.firstToken);
throw ex;
}
for (int i = 0; i < attributes.length - 1; i++) {
VarAttribute attr = attributes[i];
if (value == null) {
BeetlException be = new BeetlException(BeetlException.NULL, "空指针");
if (i == 0) {
be.pushToken(this.firstToken);
} else {
be.pushToken(attributes[i - 1].token);
}
throw be;
}
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;
}
}
return value;
}
Aggregations