freemarker.template.TemplateScalarModel Java Examples
The following examples show how to use
freemarker.template.TemplateScalarModel.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DirectiveUtils.java From Lottery with GNU General Public License v2.0 | 6 votes |
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); } }
Example #2
Source File: TransformUtil.java From scipio-erp with Apache License 2.0 | 6 votes |
/** * 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; }
Example #3
Source File: TransformUtil.java From scipio-erp with Apache License 2.0 | 6 votes |
/** * 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; }
Example #4
Source File: GetPropertyValueMethod.java From scipio-erp with Apache License 2.0 | 6 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") 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); }
Example #5
Source File: GetEntityPropertyValueMethod.java From scipio-erp with Apache License 2.0 | 6 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") 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))); Delegator delegator = ContextFtlUtil.getDelegator(FreeMarkerWorker.getCurrentEnvironment()); String res = EntityUtilProperties.getPropertyValue(resource, name, delegator); // 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); }
Example #6
Source File: TransformUtil.java From scipio-erp with Apache License 2.0 | 6 votes |
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; }
Example #7
Source File: RequestStackMethod.java From scipio-erp with Apache License 2.0 | 6 votes |
protected Object execGetAsList(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { if (args == null || args.size() < 1 || args.size() > 2) { throw new TemplateModelException("Invalid number of arguments (expected: 1-2)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } String stackName = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)); boolean origList = false; if (args.size() >= 2) { TemplateModel listTypeModel = (TemplateModel) args.get(1); String listType = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) listTypeModel)); if ("orig".equals(listType)) { origList = true; } } Environment env = CommonFtlUtil.getCurrentEnvironment(); ObjectWrapper objectWrapper = GetRequestVarMethod.getResultObjectWrapper(env); Object res = ContextFtlUtil.getRequestStackAsList(stackName, (origList ? null : LangFtlUtil.TemplateValueTargetType.SIMPLEMODEL), env, objectWrapper); return LangFtlUtil.wrap(res, objectWrapper); }
Example #8
Source File: DirectiveUtils.java From Lottery with GNU General Public License v2.0 | 6 votes |
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); } }
Example #9
Source File: URLDecodeMethod.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * @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; }
Example #10
Source File: DirectiveUtils.java From Lottery with GNU General Public License v2.0 | 6 votes |
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); } }
Example #11
Source File: ISO8601DateFormatMethod.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * @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 : ""; }
Example #12
Source File: DirectiveUtils.java From Lottery with GNU General Public License v2.0 | 6 votes |
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); } }
Example #13
Source File: GetEffortDescriptionForPoints.java From windup with Eclipse Public License 1.0 | 6 votes |
@Override public Object exec(List arguments) throws TemplateModelException { ExecutionStatistics.get().begin(NAME); if (arguments.size() < 1) { throw new TemplateModelException("Error, method expects one or two arguments (Integer, [verbosity:String])"); } SimpleNumber simpleNumber = (SimpleNumber) arguments.get(0); int effort = simpleNumber.getAsNumber().intValue(); Verbosity verbosity = Verbosity.SHORT; if (arguments.size() > 1) { final TemplateScalarModel verbosityModel = (TemplateScalarModel) arguments.get(1); String verbosityString = verbosityModel.getAsString(); verbosity = Verbosity.valueOf(verbosityString.toUpperCase()); } String result = EffortReportService.getEffortLevelDescription(verbosity, effort); ExecutionStatistics.get().end(NAME); return result; }
Example #14
Source File: GetStyleNamesByPrefixMethod.java From scipio-erp with Apache License 2.0 | 6 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { if (args == null || args.size() != 2) { throw new TemplateModelException("Invalid number of arguments (expected: 2)"); } String styleString = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0))); styleString = TemplateFtlUtil.getPlainClassArgNames(styleString); String prefix = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(1))); String[] names = StringUtils.split(styleString, ' '); // NOTE: For emergency/safety reasons, use the current wrapper, which MAY be escaping. // style strings contain only simple characters anyway. ObjectWrapper objectWrapper = LangFtlUtil.getCurrentObjectWrapper(); SimpleSequence res = new SimpleSequence(names.length, objectWrapper); for(String name : names) { if (name.startsWith(prefix)) { res.add(name); } } // redundant //return LangFtlUtil.wrap(res, objectWrapper); return res; }
Example #15
Source File: GetRequestVarMethod.java From scipio-erp with Apache License 2.0 | 6 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") 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); }
Example #16
Source File: PermistionDirective.java From Lottery with GNU General Public License v2.0 | 5 votes |
@SuppressWarnings("unchecked") public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { // 此处的权限判断有可能和拦截器的不一致,有没有关系?大部分应该没有关系,因为不需要判断权限的可以不加这个标签。 // 光一个perms可能还不够,至少还有一个是否只浏览的问题。这个是否可以不管?可以! // 是否控制权限这个总是要的吧?perms为null代表无需控制权限。 String url = DirectiveUtils.getString(PARAM_URL, params); boolean pass = false; if (StringUtils.isBlank(url)) { // url为空,则认为有权限。 pass = true; } else { TemplateSequenceModel perms = getPerms(env); if (perms == null) { // perms为null,则代表不需要判断权限。 pass = true; } else { String perm; for (int i = 0, len = perms.size(); i < len; i++) { perm = ((TemplateScalarModel) perms.get(i)).getAsString(); if (url.startsWith(perm)) { pass = true; break; } } } } if (pass) { body.render(env.getOut()); } }
Example #17
Source File: DirectiveUtils.java From Lottery with GNU General Public License v2.0 | 5 votes |
public static String getString(String name, Map<String, TemplateModel> params) throws TemplateException { TemplateModel model = params.get(name); if (model == null) { return null; } if (model instanceof TemplateScalarModel) { return ((TemplateScalarModel) model).getAsString(); } else if ((model instanceof TemplateNumberModel)) { return ((TemplateNumberModel) model).getAsNumber().toString(); } else { throw new MustStringException(name); } }
Example #18
Source File: UtilCodecMethod.java From scipio-erp with Apache License 2.0 | 5 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { if (args == null || args.size() != 2) { throw new TemplateModelException("Invalid number of arguments (expected: 2)"); } Environment env = FreeMarkerWorker.getCurrentEnvironment(); String value = LangFtlUtil.toRawJavaString((TemplateModel) args.get(0), env); String lang = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(1))); return new SimpleScalar(langExec(value, lang)); }
Example #19
Source File: DictDataTag.java From jeewx with Apache License 2.0 | 5 votes |
/** * 取得标签参数 * * @param params * @param name * @return */ @SuppressWarnings("unchecked") private String getAttribute(Map params, String name) { String value = null; if (params.containsKey(name)) { TemplateModel paramValue = (TemplateModel) params.get(name); try { value = ((TemplateScalarModel) paramValue).getAsString(); } catch (TemplateModelException e) { LOG.error("get attribute '{}' error", name, e); } } return value; }
Example #20
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * Adds the still-wrapped TemplateModels in hash to a java Map. * <p> * <em>WARN</em>: This is not BeanModel-aware (complex map). */ public static void addModelsToMap(Map<String, ? super TemplateModel> dest, TemplateHashModelEx source) throws TemplateModelException { TemplateCollectionModel keysModel = source.keys(); TemplateModelIterator modelIt = keysModel.iterator(); while(modelIt.hasNext()) { String key = getAsStringNonEscaping((TemplateScalarModel) modelIt.next()); dest.put(key, source.get(key)); } }
Example #21
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * Performs the logical raw string operation on multiple values, concatenating the result. */ public static TemplateScalarModel toRawString(Collection<TemplateModel> values, Environment env) throws TemplateModelException { StringBuilder sb = new StringBuilder(); for(TemplateModel value: values) { if (value instanceof TemplateScalarModel) { sb.append(getAsStringNonEscaping((TemplateScalarModel) value)); } else { sb.append(execStringBuiltIn(value, env).getAsString()); } } return new SimpleScalar(sb.toString()); }
Example #22
Source File: DictDataTag.java From jeecg with Apache License 2.0 | 5 votes |
/** * 取得标签参数 * * @param params * @param name * @return */ @SuppressWarnings("unchecked") private String getAttribute(Map params, String name) { String value = null; if (params.containsKey(name)) { TemplateModel paramValue = (TemplateModel) params.get(name); try { value = ((TemplateScalarModel) paramValue).getAsString(); } catch (TemplateModelException e) { LOG.error("get attribute '{}' error", name, e); } } return value; }
Example #23
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * To string set. * <p> * WARN: bypasses auto-escaping, caller handles. * (e.g. the object wrapper used to rewrap the result). */ public static Set<String> toStringSet(TemplateSequenceModel seqModel) throws TemplateModelException { Set<String> set = new HashSet<String>(); for(int i=0; i < seqModel.size(); i++) { set.add(getAsStringNonEscaping((TemplateScalarModel) seqModel.get(i))); } return set; }
Example #24
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * Add to string set. * <p> * WARN: bypasses auto-escaping, caller handles. * (e.g. the object wrapper used to rewrap the result). */ public static void addToStringSet(Set<String> dest, TemplateCollectionModel collModel) throws TemplateModelException { TemplateModelIterator modelIt = collModel.iterator(); while(modelIt.hasNext()) { dest.add(getAsStringNonEscaping((TemplateScalarModel) modelIt.next())); } }
Example #25
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * To string set. * <p> * WARN: Bypasses auto-escaping, caller handles. * (e.g. the object wrapper used to rewrap the result). */ public static Set<String> toStringSet(TemplateCollectionModel collModel) throws TemplateModelException { Set<String> set = new HashSet<String>(); TemplateModelIterator modelIt = collModel.iterator(); while(modelIt.hasNext()) { set.add(getAsStringNonEscaping((TemplateScalarModel) modelIt.next())); } return set; }
Example #26
Source File: TemplateFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
private static Object getAsStringEscapingIfString(TemplateModel model) throws TemplateModelException { if (model == null) { return null; } else if (OfbizFtlObjectType.STRING.isObjectType(model)) { return ((TemplateScalarModel) model).getAsString(); } else { return LangFtlUtil.unwrapAlways(model); } }
Example #27
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * Adds to simple hash from source map. * <p> * <em>WARN</em>: This is not BeanModel-aware (complex map). */ public static void addToSimpleMap(SimpleHash dest, TemplateHashModelEx source) throws TemplateModelException { TemplateCollectionModel keysModel = source.keys(); TemplateModelIterator modelIt = keysModel.iterator(); while(modelIt.hasNext()) { String key = getAsStringNonEscaping((TemplateScalarModel) modelIt.next()); dest.put(key, source.get(key)); } }
Example #28
Source File: RewrapObjectMethod.java From scipio-erp with Apache License 2.0 | 5 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { if (args == null || args.size() < 1 || args.size() > 3 ) { throw new TemplateModelException("Invalid number of arguments (expected: 1-3)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); TemplateModel object = (TemplateModel) args.get(0); String wrapperStr = null; String modeStr = null; if (args.size() >= 2) { TemplateScalarModel wrapperModel = (TemplateScalarModel) args.get(1); if (wrapperModel != null) { wrapperStr = wrapperModel.getAsString(); } if (args.size() >= 3) { TemplateScalarModel modeModel = (TemplateScalarModel) args.get(2); if (modeModel != null) { modeStr = modeModel.getAsString(); } } } Object res = LangFtlUtil.rewrapObject(object, WrappingOptions.makeOptions(wrapperStr, modeStr, env), env); return res; }
Example #29
Source File: IsObjectTypeMethod.java From scipio-erp with Apache License 2.0 | 5 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { if (args == null || args.size() != 2) { throw new TemplateModelException("Invalid number of arguments (expected: 2)"); } String type = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0))); TemplateModel object = (TemplateModel) args.get(1); return OfbizFtlObjectType.isObjectTypeSafe(type, object); }
Example #30
Source File: TemplateFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
public static TemplateScalarModel execEscapeValFunction(TemplateModel arg1, TemplateModel arg2, TemplateModel arg3, Environment env) throws TemplateModelException { if (escapeVal3ArgFunctionCall == null) { // NOTE: no real need for synchronize here escapeVal3ArgFunctionCall = LangFtlUtil.getFunctionCall("escapeVal", 3, env); } return (TemplateScalarModel) LangFtlUtil.execFunction(escapeVal3ArgFunctionCall, new TemplateModel[] { arg1, arg2, arg3}, env); }