use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class ALU method isTrue.
/**
* @param o
* @param node
* @return
*/
public static Boolean isTrue(final Object o, ASTNode node) {
if (o == null) {
BeetlException be = new BeetlException(BeetlException.NULL);
be.pushToken(node.token);
throw be;
}
if (Boolean.TRUE == o) {
return Boolean.TRUE;
} else if (Boolean.FALSE == o) {
return Boolean.FALSE;
} else if (o instanceof Boolean) {
return ((Boolean) o);
} else {
BeetlException ex = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR, o.getClass().toString());
ex.pushToken(node.token);
throw ex;
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class WebRender method render.
/**
* @param key 模板资源id
* @param request
* @param response
* @param args 其他参数,将会传给modifyTemplate方法
*/
public void render(String key, HttpServletRequest request, HttpServletResponse response, Object... args) {
Writer writer = null;
OutputStream os = null;
String ajaxId = null;
Template template = null;
boolean isError = false;
try {
// response.setContentType(contentType);
int ajaxIdIndex = key.lastIndexOf("#");
if (ajaxIdIndex != -1) {
ajaxId = key.substring(ajaxIdIndex + 1);
key = key.substring(0, ajaxIdIndex);
template = gt.getAjaxTemplate(key, ajaxId);
} else {
template = gt.getTemplate(key);
}
Enumeration<String> attrs = request.getAttributeNames();
while (attrs.hasMoreElements()) {
String attrName = attrs.nextElement();
template.binding(attrName, request.getAttribute(attrName));
}
WebVariable webVariable = new WebVariable();
webVariable.setRequest(request);
webVariable.setResponse(response);
template.binding(WebVariable.SESSION, new SessionWrapper(request, request.getSession(false)));
template.binding(WebVariable.SERVLET, webVariable);
template.binding(WebVariable.REQUEST, request);
template.binding("ctxPath", request.getContextPath());
template.binding("$page", new HashMap());
template.binding("parameter", new ParameterWrapper(request));
template.binding("cookie", new CookieFunction());
modifyTemplate(template, key, request, response, args);
String strWebAppExt = gt.getConf().getWebAppExt();
if (strWebAppExt != null) {
WebRenderExt renderExt = this.getWebRenderExt(strWebAppExt);
renderExt.modify(template, gt, request, response);
}
if (gt.getConf().isDirectByteOutput()) {
os = response.getOutputStream();
template.renderTo(os);
} else {
writer = response.getWriter();
template.renderTo(writer);
}
} catch (IOException e) {
isError = true;
handleClientError(e);
} catch (BeetlException e) {
isError = true;
// response.setStatus(500);
//
handleBeetlException(e);
} finally {
try {
if (!isError && writer != null)
writer.flush();
if (!isError && os != null) {
os.flush();
}
} catch (IOException e) {
handleClientError(e);
}
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class Printf method call.
public String call(Object[] paras, Context ctx) {
String template = (String) paras[0];
Object[] args = new Object[paras.length - 1];
for (int i = 0; i < args.length; i++) {
args[i] = paras[i + 1];
}
StringBuilder sb = new StringBuilder();
Formatter f = new Formatter(sb);
f.format(template, args);
try {
ctx.byteWriter.writeString(sb.toString());
} catch (IOException e) {
BeetlException be = new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR);
throw be;
}
return "";
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class Println method call.
public String call(Object[] paras, Context ctx) {
try {
ByteWriter w = ctx.byteWriter;
if (paras.length == 0) {
w.writeString(ctx.template.program.metaData.lineSeparator);
return "";
}
Object o = paras[0];
if (o != null) {
w.writeString(o.toString());
w.writeString(ctx.template.program.metaData.lineSeparator);
}
} catch (IOException e) {
BeetlException be = new BeetlException(BeetlException.CLIENT_IO_ERROR_ERROR);
throw be;
}
return "";
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class ObjectAA method value.
@Override
public Object value(Object o, Object name) {
if (o instanceof Map) {
return ((Map) o).get(name);
} else if (o instanceof List) {
try {
return ((List) o).get(((Number) name).intValue());
} catch (ClassCastException ex) {
throw new ClassCastException("目标类型为java.util.List,无此属性:" + name);
}
} else if (o.getClass().isArray()) {
try {
if (o.getClass().getComponentType().isPrimitive()) {
return PrimitiveArrayUtil.getObject(o, (((Number) name).intValue()));
} else {
return ((Object[]) o)[(((Number) name).intValue())];
}
} catch (ClassCastException ex) {
throw new ClassCastException("目标类型为数组,无此属性:" + name);
}
} else {
Class c = o.getClass();
MethodInvoker invoker = ObjectUtil.getInvokder(c, (String) name);
if (invoker != null) {
return invoker.get(o);
} else {
BeetlException ex = new BeetlException(BeetlException.ATTRIBUTE_NOT_FOUND, (String) name);
throw ex;
}
}
}
Aggregations