001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.web.directive; 017 018import com.jfinal.kit.LogKit; 019import com.jfinal.template.Env; 020import com.jfinal.template.expr.ast.ExprList; 021import com.jfinal.template.io.Writer; 022import com.jfinal.template.stat.Location; 023import com.jfinal.template.stat.OutputDirectiveFactory; 024import com.jfinal.template.stat.Scope; 025import com.jfinal.template.stat.ast.Output; 026import io.jboot.Jboot; 027 028/** 029 * 主要作用:在生产环境下,忽略模板引擎的错误输出。 030 */ 031public class JbootOutputDirectiveFactory extends OutputDirectiveFactory { 032 033 public static final JbootOutputDirectiveFactory me = new JbootOutputDirectiveFactory(); 034 035 private boolean ignoreTemplateException = !Jboot.isDevMode(); 036 037 public boolean isIgnoreTemplateException() { 038 return ignoreTemplateException; 039 } 040 041 public void setIgnoreTemplateException(boolean ignoreTemplateException) { 042 this.ignoreTemplateException = ignoreTemplateException; 043 } 044 045 @Override 046 public Output getOutputDirective(ExprList exprList, Location location) { 047 return new JbootOutput(exprList, location); 048 } 049 050 051 public static class JbootOutput extends Output { 052 053 public JbootOutput(ExprList exprList, Location location) { 054 super(exprList, location); 055 } 056 057 @Override 058 public void exec(Env env, Scope scope, Writer writer) { 059 try { 060 super.exec(env, scope, writer); 061 } catch (Exception e) { 062 if (me.ignoreTemplateException) { 063 LogKit.error(e.toString(), e); 064 } else { 065 throw e; 066 } 067 } 068 } 069 } 070}