/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { Object result = null; if (args.size() == 1) { Object arg0 = args.get(0); if (arg0 instanceof TemplateDateModel) { result = ISO8601DateFormat.format(((TemplateDateModel)arg0).getAsDate()); } else if (arg0 instanceof TemplateScalarModel) { result = ISO8601DateFormat.parse(((TemplateScalarModel)arg0).getAsString()); } } return result != null ? result : ""; }
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { String result = ""; if (args.size() != 0) { String s = ""; Object arg0 = args.get(0); if (arg0 instanceof TemplateScalarModel) { s = ((TemplateScalarModel)arg0).getAsString(); } if (s != null) { result = URLDecoder.decode(s); } } return result; }
@SuppressWarnings("unchecked") private static String getArg(Map args, String key) { String result = ""; Object o = args.get(key); if (o != null) { if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module); if (o instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) o; try { // SCIPIO: This should bypass auto-escaping result = LangFtlUtil.getAsStringNonEscaping(s); } catch (TemplateModelException e) { Debug.logError(e, "Template Exception", module); } } else { result = o.toString(); } } return result; }
@SuppressWarnings("unchecked") private static String getArg(Map args, String key) { String result = ""; Object o = args.get(key); if (o != null) { if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module); if (o instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) o; try { // SCIPIO: bypass auto-escaping on this //result = s.getAsString(); result = LangFtlUtil.getAsStringNonEscaping(s); } catch (TemplateModelException e) { Debug.logError(e, "Template Exception", module); } } else { result = o.toString(); } } return result; }
/** * Gets boolean arg. * <p> * Will automatically interpret string true/false as boolean. */ public static Boolean getBooleanArg(TemplateModel obj, Boolean defaultValue) throws TemplateModelException { if (obj instanceof TemplateBooleanModel) { return ((TemplateBooleanModel) obj).getAsBoolean(); } else if (obj instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) obj; String val = s.getAsString(); // SCIPIO: empty check is desirable and makes it so caller can request default by specifying "" if (!val.isEmpty()) { return "true".equalsIgnoreCase(s.getAsString()); } } else if (obj != null) { throw new TemplateModelException("Expected boolean model or string model representation of boolean, but got a " + obj.getClass() + " instead"); } return defaultValue; }
/** * Gets string arg. * <p> * If number or date passed, will be coerced to string. Other types such as maps or lists * will throw TemplateModelException. */ public static String getStringArg(TemplateModel obj, String defaultValue, boolean useDefaultWhenEmpty, boolean nonEscaping) throws TemplateModelException { String result = null; if (obj instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) obj; result = LangFtlUtil.getAsString(s, nonEscaping); } else if (obj == null) { return defaultValue; } else if (obj instanceof TemplateNumberModel || obj instanceof TemplateDateModel) { // TODO: optimize this call result = LangFtlUtil.execStringBuiltIn(obj, FreeMarkerWorker.getCurrentEnvironment()).getAsString(); } else { throw new TemplateModelException("Expected string model or something coercible to string, but got a " + obj.getClass() + " instead"); } if (useDefaultWhenEmpty && result.isEmpty()) { return defaultValue; } return result; }
public static Object getBooleanOrStringArg(TemplateModel obj, Object defaultValue, boolean useDefaultWhenEmpty, boolean nonEscaping) throws TemplateModelException { Object result = null; if (obj instanceof TemplateBooleanModel) { return ((TemplateBooleanModel) obj).getAsBoolean(); } else if (obj instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) obj; result = LangFtlUtil.getAsString(s, nonEscaping); } else if (obj != null) { result = obj.toString(); } else { return defaultValue; } if (useDefaultWhenEmpty && (result instanceof String) && ((String) result).isEmpty()) { return defaultValue; } return result; }
/** * Gets integer arg. * <p> * If string passed, will be parsed as integer. Other types such as maps or lists * will throw TemplateModelException. */ public static Integer getIntegerArg(TemplateModel obj, Integer defaultValue) throws TemplateModelException, NumberFormatException { if (obj instanceof TemplateNumberModel) { return ((TemplateNumberModel) obj).getAsNumber().intValue(); } else if (obj instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) obj; String strResult = LangFtlUtil.getAsString(s, true); if (strResult.isEmpty()) { return defaultValue; } else { return Integer.parseInt(strResult); } } else if (obj == null) { return defaultValue; } else { throw new TemplateModelException("Expected integer model or string representing of integer, but got a " + obj.getClass() + " instead"); } }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() != 1) { throw new TemplateModelException("Invalid number of arguments (expected: 1)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); Object res = ContextFtlUtil.getRequestVar(LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)), env); ObjectWrapper objectWrapper = getResultObjectWrapper(env); return LangFtlUtil.wrap(res, objectWrapper); }
@SuppressWarnings("unchecked") protected Object execPush(List args, boolean setLast) throws TemplateModelException { if (args == null || args.size() != 2) { throw new TemplateModelException("Invalid number of arguments (expected: 2)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } TemplateModel valueModel = (TemplateModel) args.get(1); Environment env = CommonFtlUtil.getCurrentEnvironment(); ContextFtlUtil.pushRequestStack(LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)), valueModel, setLast, env); return new SimpleScalar(""); }
@SuppressWarnings("unchecked") protected Object execRead(List args, boolean pop) throws TemplateModelException { if (args == null || args.size() != 1) { throw new TemplateModelException("Invalid number of arguments (expected: 1)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); Object res = ContextFtlUtil.readRequestStack(LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)), pop, env); ObjectWrapper objectWrapper = GetRequestVarMethod.getResultObjectWrapper(env); return LangFtlUtil.wrap(res, objectWrapper); }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() != 2) throw new TemplateModelException("Invalid number of arguements"); if (!(args.get(0) instanceof TemplateScalarModel)) throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); if (!(args.get(1) instanceof TemplateScalarModel)) throw new TemplateModelException("Second argument not an instance of TemplateScalarModel (string)"); String resource = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0))); String name = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(1))); String res = UtilProperties.getPropertyValue(resource, name); // here we do opposite of UtilProperties and return null if empty, so ! operator can work if (res != null && res.isEmpty()) { res = null; } // Prevent escaping by EscapingObjectWrapper - values from properties are always coded // by trusted people return LangFtlUtil.wrapNonEscaping(res); }
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { String name = null; TemplateModel paramValue = (TemplateModel) params.get("name"); if (paramValue != null) { name = ((TemplateScalarModel) paramValue).getAsString(); } if (name == null || "".equals(name)) { return; } try { String url = fisRes.require(name); } catch (FisException e) { throw new TemplateException(e.getMessage(), e, env); } }
public static Long getLong(String name, Map<String, TemplateModel> params) throws TemplateException { TemplateModel model = params.get(name); if (model == null) { return null; } if (model instanceof TemplateScalarModel) { String s = ((TemplateScalarModel) model).getAsString(); if (StringUtils.isBlank(s)) { return null; } try { return Long.parseLong(s); } catch (NumberFormatException e) { throw new MustNumberException(name); } } else if (model instanceof TemplateNumberModel) { return ((TemplateNumberModel) model).getAsNumber().longValue(); } else { throw new MustNumberException(name); } }
public static Integer getInt(String name, Map<String, TemplateModel> params) throws TemplateException { TemplateModel model = params.get(name); if (model == null) { return null; } if (model instanceof TemplateScalarModel) { String s = ((TemplateScalarModel) model).getAsString(); if (StringUtils.isBlank(s)) { return null; } try { return Integer.parseInt(s); } catch (NumberFormatException e) { throw new MustNumberException(name); } } else if (model instanceof TemplateNumberModel) { return ((TemplateNumberModel) model).getAsNumber().intValue(); } else { throw new MustNumberException(name); } }
public static Boolean getBool(String name, Map<String, TemplateModel> params) throws TemplateException { TemplateModel model = params.get(name); if (model == null) { return null; } if (model instanceof TemplateBooleanModel) { return ((TemplateBooleanModel) model).getAsBoolean(); } else if (model instanceof TemplateNumberModel) { return !(((TemplateNumberModel) model).getAsNumber().intValue() == 0); } else if (model instanceof TemplateScalarModel) { String s = ((TemplateScalarModel) model).getAsString(); // 空串应该返回null还是true呢? if (!StringUtils.isBlank(s)) { return !(s.equals("0") || s.equalsIgnoreCase("false") || s .equalsIgnoreCase("f")); } else { return null; } } else { throw new MustBooleanException(name); } }
public static Date getDate(String name, Map<String, TemplateModel> params) throws TemplateException { TemplateModel model = params.get(name); if (model == null) { return null; } if (model instanceof TemplateDateModel) { return ((TemplateDateModel) model).getAsDate(); } else if (model instanceof TemplateScalarModel) { DateTypeEditor editor = new DateTypeEditor(); editor.setAsText(((TemplateScalarModel) model).getAsString()); return (Date) editor.getValue(); } else { throw new MustDateException(name); } }
/** * Compacts an input string. * <p> * {@inheritDoc} */ @Override public TemplateModel exec(List arguments) throws TemplateModelException { try { String toCompact = ((TemplateScalarModel) arguments.get(0)).getAsString(); if (toCompact == null || toCompact.length() == 0) { return new SimpleScalar(""); } StringBuilder result = new StringBuilder(); result.append(Character.toUpperCase(toCompact.charAt(0))); for (int i = 1; i < toCompact.length(); i++) { char prev = toCompact.charAt(i - 1); char curr = toCompact.charAt(i); if ((Character.isUpperCase(curr) && Character.isLowerCase(prev)) || prev == UNDERSCORE) { result.append(Character.toUpperCase(curr)); } } return new SimpleScalar(result.toString()); } catch (Exception ex) { throw new TemplateModelException("Could execute compactString method.", ex); } }
@SuppressWarnings("unchecked") private static String getArg(Map args, String key) { String result = ""; Object o = args.get(key); if (o != null) { if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module); if (o instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) o; try { result = s.getAsString(); } catch (TemplateModelException e) { Debug.logError(e, "Template Exception", module); } } else { result = o.toString(); } } return result; }
@SuppressWarnings("unchecked") public Object exec(List args) throws TemplateModelException { if (args == null || args.size() != 2) throw new TemplateModelException("Invalid number of arguements"); if (!(args.get(0) instanceof TemplateScalarModel)) throw new TemplateModelException("First argument not an instance of TemplateScalarModel"); if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel)) throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel"); Environment env = Environment.getCurrentEnvironment(); BeanModel req = (BeanModel)env.getVariable("request"); HttpServletRequest request = (HttpServletRequest) req.getWrappedObject(); String name = ((TemplateScalarModel) args.get(0)).getAsString(); Object value = null; if (args.get(1) instanceof TemplateScalarModel) value = ((TemplateScalarModel) args.get(1)).getAsString(); if (args.get(1) instanceof TemplateNumberModel) value = ((TemplateNumberModel) args.get(1)).getAsNumber(); if (args.get(1) instanceof BeanModel) value = ((BeanModel) args.get(1)).getWrappedObject(); request.setAttribute(name, value); return new SimpleScalar(""); }
@SuppressWarnings("unchecked") private static String getArg(Map args, String key) { String result = ""; Object obj = args.get(key); if (obj != null) { if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + obj.getClass().getName(), module); if (obj instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) obj; try { result = s.getAsString(); } catch (TemplateModelException e) { Debug.logError(e, "Template Exception", module); } } else { result = obj.toString(); } } return result; }
private static String getArg(Map args, String key) { String result = ""; Object o = args.get(key); if (o != null) { if (Debug.verboseOn()) Debug.logVerbose("Arg Object : " + o.getClass().getName(), module); if (o instanceof TemplateScalarModel) { TemplateScalarModel s = (TemplateScalarModel) o; try { result = s.getAsString(); } catch (TemplateModelException e) { Debug.logError(e, "Template Exception", module); } } else { result = o.toString(); } } return result; }
/** * 获取Integer类型的参数值 * * @return 参数值 */ public static Integer getIntegerParameter(String name, Map<String, TemplateModel> params) throws TemplateModelException { TemplateModel templateModel = params.get(name); if (templateModel == null) { return null; } if (templateModel instanceof TemplateScalarModel) { String value = ((TemplateScalarModel) templateModel).getAsString(); if (StringUtils.isEmpty(value)) { return null; } else { return Integer.parseInt(value); } } else if ((templateModel instanceof TemplateNumberModel)) { return ((TemplateNumberModel) templateModel).getAsNumber().intValue(); } else { throw new TemplateModelException("The \"" + name + "\" parameter " + "must be a integer."); } }
/** * 获取Boolean类型的参数值 * * @return 参数值 */ public static Boolean getBooleanParameter(String name, Map<String, TemplateModel> params) throws TemplateModelException { TemplateModel templateModel = params.get(name); if (templateModel == null) { return null; } if (templateModel instanceof TemplateScalarModel) { String value = ((TemplateScalarModel) templateModel).getAsString(); if (StringUtils.isEmpty(value)) { return null; } else { return Boolean.valueOf(value); } } else if ((templateModel instanceof TemplateBooleanModel)) { return ((TemplateBooleanModel) templateModel).getAsBoolean(); } else { throw new TemplateModelException("The \"" + name + "\" parameter " + "must be a boolean."); } }
/** * 获取Date类型的参数值 * * @return 参数值 */ public static Date getDateParameter(String name, Map<String, TemplateModel> params) throws TemplateModelException { TemplateModel templateModel = params.get(name); if (templateModel == null) { return null; } if (templateModel instanceof TemplateScalarModel) { String value = ((TemplateScalarModel) templateModel).getAsString(); if (StringUtils.isEmpty(value)) { return null; } try { String[] pattern = new String[]{"yyyy-MM","yyyyMM","yyyy/MM", "yyyyMMdd","yyyy-MM-dd","yyyy/MM/dd", "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss"}; return DateUtils.parseDate(value, pattern); } catch (ParseException e) { e.printStackTrace(); return null; } } else if ((templateModel instanceof TemplateDateModel)) { return ((TemplateDateModel) templateModel).getAsDate(); } else { throw new TemplateModelException("The \"" + name + "\" parameter " + "must be a date."); } }