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.base; 017 018import com.jfinal.aop.Aop; 019import com.jfinal.template.Directive; 020import com.jfinal.template.Env; 021import com.jfinal.template.TemplateException; 022import com.jfinal.template.expr.ast.ExprList; 023import com.jfinal.template.io.Writer; 024import com.jfinal.template.stat.Scope; 025import io.jboot.utils.StrUtil; 026 027import java.io.IOException; 028import java.math.BigDecimal; 029import java.math.BigInteger; 030import java.util.Map; 031 032/** 033 * Jfinal 指令的基类 034 */ 035public abstract class JbootDirectiveBase extends Directive { 036 037 public JbootDirectiveBase() { 038 Aop.inject(this); 039 } 040 041 042 @Override 043 public void setExprList(ExprList exprList) { 044 super.setExprList(exprList); 045 } 046 047 048 @Override 049 public void exec(Env env, Scope scope, Writer writer) { 050 scope = new Scope(scope); 051 scope.getCtrl().setLocalAssignment(); 052 exprList.eval(scope); 053 onRender(env, scope, writer); 054 } 055 056 057 public abstract void onRender(Env env, Scope scope, Writer writer); 058 059 060 public void renderBody(Env env, Scope scope, Writer writer) { 061 stat.exec(env, scope, writer); 062 } 063 064 public void renderText(Writer writer, String text) { 065 try { 066 writer.write(text); 067 } catch (IOException e) { 068 throw new TemplateException(e.getMessage(), location, e); 069 } 070 } 071 072 public <T> T getPara(String key, Scope scope) { 073 return getPara(key, scope, null); 074 } 075 076 public <T> T getPara(String key, Scope scope, T defaultValue) { 077 Object data = scope.getLocal(key); 078 return (T) (data == null ? defaultValue : data); 079 } 080 081 public <T> T getPara(int index, Scope scope) { 082 return getPara(index, scope, null); 083 } 084 085 public <T> T getPara(int index, Scope scope, T defaultValue) { 086 if (index < 0 || index >= exprList.length()) { 087 return defaultValue; 088 } 089 Object data = exprList.getExpr(index).eval(scope); 090 return (T) (data == null ? defaultValue : data); 091 } 092 093 public String getParaToString(String key, Scope scope) { 094 Object object = getPara(key, scope, null); 095 if (object == null || object instanceof String) { 096 return (String) object; 097 } 098 String objStr = object.toString(); 099 return StrUtil.isBlank(objStr) ? null : objStr; 100 } 101 102 103 public String getParaToString(String key, Scope scope, String defaultValue) { 104 String v = getParaToString(key, scope); 105 return v == null ? defaultValue : v; 106 } 107 108 109 public String getParaToString(int index, Scope scope) { 110 Object object = getPara(index, scope, null); 111 if (object == null || object instanceof String) { 112 return (String) object; 113 } 114 String objStr = object.toString(); 115 return StrUtil.isBlank(objStr) ? null : objStr; 116 } 117 118 119 public String getParaToString(int index, Scope scope, String defaultValue) { 120 String v = getParaToString(index, scope); 121 return v == null ? defaultValue : v; 122 } 123 124 125 public Integer getParaToInt(String key, Scope scope) { 126 Object object = getPara(key, scope, null); 127 if (object == null || object instanceof Integer) { 128 return (Integer) object; 129 } 130 String objStr = object.toString(); 131 return StrUtil.isBlank(objStr) ? null : Integer.valueOf(objStr); 132 } 133 134 public Integer getParaToInt(String key, Scope scope, Integer defaultValue) { 135 Integer v = getParaToInt(key, scope); 136 return v == null ? defaultValue : v; 137 } 138 139 public Integer getParaToInt(int index, Scope scope) { 140 Object object = getPara(index, scope, null); 141 if (object == null || object instanceof Integer) { 142 return (Integer) object; 143 } 144 String objStr = object.toString(); 145 return StrUtil.isBlank(objStr) ? null : Integer.valueOf(objStr); 146 } 147 148 public Integer getParaToInt(int index, Scope scope, Integer defaultValue) { 149 Integer v = getParaToInt(index, scope); 150 return v == null ? defaultValue : v; 151 } 152 153 154 public Long getParaToLong(String key, Scope scope) { 155 Object object = getPara(key, scope, null); 156 if (object == null || object instanceof Long) { 157 return (Long) object; 158 } 159 String objStr = object.toString(); 160 return StrUtil.isBlank(objStr) ? null : Long.valueOf(objStr); 161 } 162 163 public Long getParaToLong(String key, Scope scope, Long defaultValue) { 164 Long v = getParaToLong(key, scope); 165 return v == null ? defaultValue : v; 166 } 167 168 public Long getParaToLong(int index, Scope scope) { 169 Object object = getPara(index, scope, null); 170 if (object == null || object instanceof Long) { 171 return (Long) object; 172 } 173 String objStr = object.toString(); 174 return StrUtil.isBlank(objStr) ? null : Long.valueOf(objStr); 175 } 176 177 public Long getParaToLong(int index, Scope scope, Long defaultValue) { 178 Long v = getParaToLong(index, scope); 179 return v == null ? defaultValue : v; 180 } 181 182 public Boolean getParaToBool(String key, Scope scope) { 183 Object object = getPara(key, scope, null); 184 if (object == null || object instanceof Boolean) { 185 return (Boolean) object; 186 } 187 String objStr = object.toString(); 188 return StrUtil.isBlank(objStr) ? null : Boolean.valueOf(objStr); 189 } 190 191 public Boolean getParaToBool(String key, Scope scope, Boolean defaultValue) { 192 Boolean v = getParaToBool(key, scope); 193 return v == null ? defaultValue : v; 194 } 195 196 public Boolean getParaToBool(int index, Scope scope) { 197 Object object = getPara(index, scope, null); 198 if (object == null || object instanceof Boolean) { 199 return (Boolean) object; 200 } 201 String objStr = object.toString(); 202 return StrUtil.isBlank(objStr) ? null : Boolean.valueOf(objStr); 203 } 204 205 public Boolean getParaToBool(int index, Scope scope, Boolean defaultValue) { 206 Boolean v = getParaToBool(index, scope); 207 return v == null ? defaultValue : v; 208 } 209 210 public BigInteger getParaToBigInteger(String key, Scope scope) { 211 Object object = getPara(key, scope, null); 212 if (object == null || object instanceof BigInteger) { 213 return (BigInteger) object; 214 } 215 String objStr = object.toString(); 216 return StrUtil.isBlank(objStr) ? null : new BigInteger(objStr); 217 } 218 219 public BigInteger getParaToBigInteger(String key, Scope scope, BigInteger defaultValue) { 220 BigInteger v = getParaToBigInteger(key, scope); 221 return v == null ? defaultValue : v; 222 } 223 224 public BigInteger getParaToBigInteger(int index, Scope scope) { 225 Object object = getPara(index, scope, null); 226 if (object == null || object instanceof BigInteger) { 227 return (BigInteger) object; 228 } 229 String objStr = object.toString(); 230 return StrUtil.isBlank(objStr) ? null : new BigInteger(objStr); 231 } 232 233 public BigInteger getParaToBigInteger(int index, Scope scope, BigInteger defaultValue) { 234 BigInteger v = getParaToBigInteger(index, scope); 235 return v == null ? defaultValue : v; 236 } 237 238 239 public BigDecimal getParaToBigDecimal(String key, Scope scope) { 240 Object object = getPara(key, scope, null); 241 if (object == null || object instanceof BigDecimal) { 242 return (BigDecimal) object; 243 } 244 String objStr = object.toString(); 245 return StrUtil.isBlank(objStr) ? null : new BigDecimal(objStr); 246 } 247 248 public BigDecimal getParaToBigDecimal(String key, Scope scope, BigDecimal defaultValue) { 249 BigDecimal v = getParaToBigDecimal(key, scope); 250 return v == null ? defaultValue : v; 251 } 252 253 public BigDecimal getParaToBigDecimal(int index, Scope scope) { 254 Object object = getPara(index, scope, null); 255 if (object == null || object instanceof BigDecimal) { 256 return (BigDecimal) object; 257 } 258 String objStr = object.toString(); 259 return StrUtil.isBlank(objStr) ? null : new BigDecimal(objStr); 260 } 261 262 263 public BigDecimal getParaToBigDecimal(int index, Scope scope, BigDecimal defaultValue) { 264 BigDecimal v = getParaToBigDecimal(index, scope); 265 return v == null ? defaultValue : v; 266 } 267 268 269 public Map getParas(Scope scope) { 270 return scope.getData(); 271 } 272 273 public Map getRootParas(Scope scope) { 274 return scope.getRootData(); 275 } 276 277 278}