Search in sources :

Example 1 with Resource

use of org.beetl.core.Resource in project beetl2.0 by javamonkey.

the class FileResourceLoader method getResource.

@Override
public Resource getResource(String key) {
    File file = new File(root, key);
    Resource resource = new FileResource(file, key, this);
    resource.setResourceLoader(this);
    return resource;
}
Also used : Resource(org.beetl.core.Resource) File(java.io.File)

Example 2 with Resource

use of org.beetl.core.Resource in project beetl2.0 by javamonkey.

the class MapResourceLoader method getResource.

public Resource getResource(final String key) {
    return new Resource(key, this) {

        public Reader openReader() {
            String val = get(key);
            if (val == null) {
                BeetlException ex = new BeetlException(BeetlException.TEMPLATE_LOAD_ERROR);
                ex.pushResource(this);
                throw ex;
            }
            return new StringReader(val);
        }

        public boolean isModified() {
            return !autoCheck;
        }
    };
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Resource(org.beetl.core.Resource) StringReader(java.io.StringReader)

Example 3 with Resource

use of org.beetl.core.Resource in project beetl2.0 by javamonkey.

the class FunctionExpression method infer.

public void infer(InferContext inferCtx) {
    Function fn = inferCtx.gt.getFunction(name);
    if (fn == null) {
        Resource resource = getResource(inferCtx.gt, name);
        if (resource == null) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_NOT_FOUND);
            ex.pushToken(token);
            throw ex;
        } else {
            fn = new FileFunctionWrapper(resource.getId());
        }
    }
    for (Expression exp : exps) {
        exp.infer(inferCtx);
    }
    // return type;
    Class c = null;
    if (fn instanceof SingleFunctionWrapper) {
        SingleFunctionWrapper singleWrapper = (SingleFunctionWrapper) fn;
        c = singleWrapper.getReturnType();
    } else if (fn instanceof MutipleFunctionWrapper) {
        try {
            Class[] parasType = new Class[exps.length];
            int i = 0;
            for (Expression exp : exps) {
                exp.infer(inferCtx);
                parasType[i++] = exp.getType().cls;
            }
            c = ((MutipleFunctionWrapper) fn).getReturnType(parasType);
        } catch (BeetlException ex) {
            ex.pushToken(token);
            throw ex;
        }
    } else {
        Method call = null;
        try {
            call = fn.getClass().getMethod("call", Object[].class, Context.class);
            c = call.getReturnType();
        } catch (NoSuchMethodException e) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_INVALID);
            ex.pushToken(token);
            throw ex;
        } catch (SecurityException e) {
            BeetlException ex = new BeetlException(BeetlException.FUNCTION_INVALID);
            ex.pushToken(token);
            throw ex;
        }
    }
    Type lastType = new Type(c);
    if (vas == null) {
        this.type = lastType;
        return;
    } else {
        Type t = null;
        for (VarAttribute attr : vas) {
            inferCtx.temp = lastType;
            attr.infer(inferCtx);
            t = lastType;
            lastType = attr.type;
            attr.type = t;
        }
        this.type = lastType;
    }
    if (safeExp != null) {
        safeExp.infer(inferCtx);
        if (!safeExp.type.equals(this.type)) {
            this.type = Type.ObjectType;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) FileFunctionWrapper(org.beetl.core.fun.FileFunctionWrapper) SingleFunctionWrapper(org.beetl.core.fun.SingleFunctionWrapper) Resource(org.beetl.core.Resource) Method(java.lang.reflect.Method) Function(org.beetl.core.Function) MutipleFunctionWrapper(org.beetl.core.fun.MutipleFunctionWrapper)

Example 4 with Resource

use of org.beetl.core.Resource in project beetl2.0 by javamonkey.

the class WebErrorHandler method processExcption.

@Override
public void processExcption(BeetlException e, Writer writer) {
    // 判断是不是开发者模式,如果不是调用父类方法(默认输出控制台)
    if (!Boolean.valueOf(e.gt.getConf().getProperty("RESOURCE.autoCheck"))) {
        super.processExcption(e, writer);
    }
    ErrorInfo error = new ErrorInfo(e);
    StringBuilder title = new StringBuilder();
    StringBuilder msg = new StringBuilder();
    if (error.getErrorCode().equals(BeetlException.CLIENT_IO_ERROR_ERROR)) {
        // 不输出详细提示信息
        title = new StringBuilder(">>").append("客户端IO异常:").append(e.resource.getId());
        if (e.getCause() != null) {
            msg.append(e.getCause());
        }
        render(writer, title.toString(), msg.toString());
        return;
    }
    int line = error.getErrorTokenLine();
    title = new StringBuilder(">>").append(error.getType()).append(":").append(error.getErrorTokenText()).append(" 位于").append(line).append("行").append(" 资源:").append(e.resource.getId());
    if (error.getErrorCode().equals(BeetlException.TEMPLATE_LOAD_ERROR)) {
        if (error.getMsg() != null)
            msg.append(error.getMsg());
        render(writer, title.toString(), msg.toString());
        return;
    }
    if (e.getMessage() != null) {
        msg.append(e.getMessage()).append("\n");
    }
    ResourceLoader resLoader = e.gt.getResourceLoader();
    // 潜在问题,此时可能得到是一个新的模板,不过可能性很小,忽略!
    String content = null;
    Resource res = resLoader.getResource(e.resource.getId());
    // 显示前后三行的内容
    int[] range = this.getRange(line);
    try {
        content = res.getContent(range[0], range[1]);
    } catch (IOException e1) {
    }
    if (content != null) {
        String[] strs = content.split(e.cr);
        int lineNumber = range[0];
        for (int i = 0; i < strs.length; i++) {
            msg.append("" + lineNumber).append("|").append(strs[i].trim()).append("\n");
            lineNumber++;
        }
    }
    if (error.hasCallStack()) {
        msg.append("  ========================").append("\n");
        msg.append("  调用栈:").append("\n");
        for (int i = 0; i < error.getResourceCallStack().size(); i++) {
            msg.append("  " + error.getResourceCallStack().get(i) + " 行:").append(error.getTokenCallStack().get(i).line).append("\n");
        }
        Throwable t = error.getCause();
        if (t != null) {
            msg.append(t.toString()).append("\n");
        }
    }
    render(writer, title.toString(), msg.toString());
    try {
        writer.flush();
    } catch (IOException e1) {
    }
}
Also used : ResourceLoader(org.beetl.core.ResourceLoader) ErrorInfo(org.beetl.core.exception.ErrorInfo) Resource(org.beetl.core.Resource) IOException(java.io.IOException)

Example 5 with Resource

use of org.beetl.core.Resource in project beetl2.0 by javamonkey.

the class FunctionExpression method getResource.

private Resource getResource(GroupTemplate gt, String name) {
    Map<String, String> resourceMap = gt.getConf().getResourceMap();
    String functionSuffix = resourceMap.get("functionSuffix");
    String functionRoot = resourceMap.get("functionRoot");
    String path = name.replace(".", "/");
    Resource resource = gt.getResourceLoader().getResource(functionRoot + "/" + path + "." + functionSuffix);
    return resource;
}
Also used : Resource(org.beetl.core.Resource)

Aggregations

Resource (org.beetl.core.Resource)6 BeetlException (org.beetl.core.exception.BeetlException)3 Function (org.beetl.core.Function)2 FileFunctionWrapper (org.beetl.core.fun.FileFunctionWrapper)2 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Method (java.lang.reflect.Method)1 ResourceLoader (org.beetl.core.ResourceLoader)1 ErrorInfo (org.beetl.core.exception.ErrorInfo)1 MutipleFunctionWrapper (org.beetl.core.fun.MutipleFunctionWrapper)1 SingleFunctionWrapper (org.beetl.core.fun.SingleFunctionWrapper)1