freemarker.template.TemplateBooleanModel Java Examples
The following examples show how to use
freemarker.template.TemplateBooleanModel.
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: 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 #2
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 #3
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 #4
Source File: MultiVarMethod.java From scipio-erp with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") static Collection<String> getAsRawSequence(TemplateModel elems) throws TemplateModelException { if (elems == null || elems instanceof TemplateBooleanModel) { return Collections.emptyList(); } else if (elems instanceof TemplateCollectionModel || elems instanceof TemplateSequenceModel) { return (Collection<String>) LangFtlUtil.unwrapAlways(elems); } else if (elems instanceof TemplateHashModelEx) { return (Collection<String>) LangFtlUtil.getMapKeys(elems); } else { throw new TemplateModelException("invalid parameter type, can't interpret as sequence: " + elems.getClass()); } }
Example #5
Source File: TemplateFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
public static String escapeVal(String value, String lang, Boolean strict, Environment env) throws TemplateModelException { if (value == null || value.isEmpty() || lang == null || lang.isEmpty()) { return value; } if (strict != null) { return execEscapeValFunction(new SimpleScalar(value), new SimpleScalar(lang), strict ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE, env).getAsString(); } else { return execEscapeValFunction(new SimpleScalar(value), new SimpleScalar(lang), env).getAsString(); } }
Example #6
Source File: TemplateFtlUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
public static String escapeFullUrl(String value, String lang, Boolean strict, Environment env) throws TemplateModelException { if (value == null || value.isEmpty() || lang == null || lang.isEmpty() || "raw".equals(lang)) { return value; } if (strict != null) { return execEscapeFullUrlFunction(new SimpleScalar(value), new SimpleScalar(lang), strict ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE, env).getAsString(); } else { return execEscapeFullUrlFunction(new SimpleScalar(value), new SimpleScalar(lang), env).getAsString(); } }
Example #7
Source File: FtlUtils.java From onetwo with Apache License 2.0 | 5 votes |
public static boolean getParameterByBoolean(Map<?, ?> params, String name, boolean def){ TemplateModel attr = getParameter(params, name, false); if(attr!=null){ try { if(TemplateBooleanModel.class.isInstance(attr)){ return ((TemplateBooleanModel)attr).getAsBoolean(); }else{ return Boolean.valueOf(attr.toString()); } } catch (Exception e) { return def; } } return def; }
Example #8
Source File: FormatDateMethod.java From scipio-erp with Apache License 2.0 | 4 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { if (args == null || args.size() < 1 || args.size() > 5) throw new TemplateModelException("Invalid number of arguments"); if (!(args.get(0) instanceof TemplateDateModel)) throw new TemplateModelException("First argument is not a Date object/model"); TemplateDateModel dateModel = ((TemplateDateModel) args.get(0)); String dateTimeFormat = (args.size() > 1) ? LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(1))) : null; TemplateModel specLocaleModel = (args.size() > 2) ? ((TemplateModel) args.get(2)) : null; TemplateModel specTimeZoneModel = (args.size() > 3) ? ((TemplateModel) args.get(3)) : null; String dateType = (args.size() > 4) ? LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(4))) : null; Environment env = CommonFtlUtil.getCurrentEnvironment(); Date date = dateModel.getAsDate(); if (dateTimeFormat != null && dateTimeFormat.isEmpty()) { dateTimeFormat = null; } // NOTE: 2016-10-12: CANNOT pass null locale or timeZone because it causes crash. // warn when missing. Locale locale; if (specLocaleModel == null || specLocaleModel instanceof TemplateBooleanModel) { if (specLocaleModel == null || ((TemplateBooleanModel) specLocaleModel).getAsBoolean()) { locale = LangFtlUtil.getLocale(TransformUtil.getFtlContextGlobalVar("locale", env)); if (locale == null) { locale = Locale.getDefault(); Debug.logWarning("Scipio: formatDate(Time): locale empty (from context); using system default", module); } } else { locale = Locale.getDefault(); Debug.logWarning("Scipio: formatDate(Time): locale false (from caller); using system default", module); } } else { locale = LangFtlUtil.getLocale(specLocaleModel); if (locale == null) { locale = Locale.getDefault(); Debug.logWarning("Scipio: formatDate(Time): locale empty (from caller); using system default", module); } } TimeZone timeZone; if (specTimeZoneModel == null || specTimeZoneModel instanceof TemplateBooleanModel) { if (specTimeZoneModel == null || ((TemplateBooleanModel) specTimeZoneModel).getAsBoolean()) { timeZone = LangFtlUtil.getTimeZone(TransformUtil.getFtlContextGlobalVar("timeZone", env)); if (timeZone == null) { timeZone = TimeZone.getDefault(); Debug.logWarning("Scipio: formatDate(Time): timeZone empty (from context); using system default", module); } } else { timeZone = TimeZone.getDefault(); Debug.logWarning("Scipio: formatDate(Time): timeZone false (from caller); using system default", module); } } else { timeZone = LangFtlUtil.getTimeZone(specTimeZoneModel); if (timeZone == null) { timeZone = TimeZone.getDefault(); Debug.logWarning("Scipio: formatDate(Time): timeZone empty (from caller); using" + " system default", module); } } Object res; if (dateType == null || dateType.isEmpty() || "date".equals(dateType)) { // "date" is default res = UtilFormatOut.formatDate(date, dateTimeFormat, locale, timeZone); } else if ("time".equals(dateType)) { res = UtilFormatOut.formatTime(date, dateTimeFormat, locale, timeZone); } else { res = UtilFormatOut.formatDateTime(date, dateTimeFormat, locale, timeZone); } return res; }
Example #9
Source File: LangFtlUtil.java From scipio-erp with Apache License 2.0 | 4 votes |
public static TemplateBooleanModel toBooleanModel(boolean value, Environment env) throws TemplateModelException { return value ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; }
Example #10
Source File: LoopDirective.java From pcgen with GNU Lesser General Public License v2.1 | 4 votes |
@SuppressWarnings("rawtypes") @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { // Check if no parameters were given: int fromVal = 0; Integer toVal = null; int step = 1; for (Object entryObj : params.entrySet()) { Map.Entry entry = (Entry) entryObj; String paramName = (String) entry.getKey(); TemplateModel paramValue = (TemplateModel) entry.getValue(); switch (paramName) { case "from": if (!(paramValue instanceof TemplateNumberModel)) { throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number."); } fromVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); break; case "to": if (!(paramValue instanceof TemplateNumberModel)) { throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number."); } toVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); break; case "step": if (!(paramValue instanceof TemplateNumberModel)) { throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number."); } step = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); if (step == 0) { throw new TemplateModelException("The \"" + paramName + "\" parameter must not be 0."); } break; default: //Case not caught, should this cause an error? break; } } if (toVal == null) { throw new TemplateModelException("The \"to\" parameter must be provided."); } if (body == null) { throw new TemplateModelException("This directive must have content."); } if (step > 0) { for (int i = fromVal; i <= toVal; i += step) { // Set the loop variable, if there is one: if (loopVars.length > 0) { loopVars[0] = new SimpleNumber(i); } if (loopVars.length > 1) { loopVars[1] = i + step <= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } // Executes the nested body (same as <#nested> in FTL). In this // case we don't provide a special writer as the parameter: body.render(env.getOut()); } } else { for (int i = fromVal; i >= toVal; i += step) { // Set the loop variable, if there is one: if (loopVars.length > 0) { loopVars[0] = new SimpleNumber(i); } if (loopVars.length > 1) { loopVars[1] = i + step >= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } // Executes the nested body (same as <#nested> in FTL). In this // case we don't provide a special writer as the parameter: body.render(env.getOut()); } } }
Example #11
Source File: LoopDirective.java From pcgen with GNU Lesser General Public License v2.1 | 4 votes |
@SuppressWarnings("rawtypes") @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { // Check if no parameters were given: int fromVal = 0; Integer toVal = null; int step = 1; for (Object entryObj : params.entrySet()) { Map.Entry entry = (Entry) entryObj; String paramName = (String) entry.getKey(); TemplateModel paramValue = (TemplateModel) entry.getValue(); switch (paramName) { case "from": if (!(paramValue instanceof TemplateNumberModel)) { throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number."); } fromVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); break; case "to": if (!(paramValue instanceof TemplateNumberModel)) { throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number."); } toVal = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); break; case "step": if (!(paramValue instanceof TemplateNumberModel)) { throw new TemplateModelException("The \"" + paramName + "\" parameter " + "must be a number."); } step = ((TemplateNumberModel) paramValue).getAsNumber().intValue(); if (step == 0) { throw new TemplateModelException("The \"" + paramName + "\" parameter must not be 0."); } break; default: //Case not caught, should this cause an error? break; } } if (toVal == null) { throw new TemplateModelException("The \"to\" parameter must be provided."); } if (body == null) { throw new TemplateModelException("This directive must have content."); } if (step > 0) { for (int i = fromVal; i <= toVal; i += step) { // Set the loop variable, if there is one: if (loopVars.length > 0) { loopVars[0] = new SimpleNumber(i); } if (loopVars.length > 1) { loopVars[1] = i + step <= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } // Executes the nested body (same as <#nested> in FTL). In this // case we don't provide a special writer as the parameter: body.render(env.getOut()); } } else { for (int i = fromVal; i >= toVal; i += step) { // Set the loop variable, if there is one: if (loopVars.length > 0) { loopVars[0] = new SimpleNumber(i); } if (loopVars.length > 1) { loopVars[1] = i + step >= toVal ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } // Executes the nested body (same as <#nested> in FTL). In this // case we don't provide a special writer as the parameter: body.render(env.getOut()); } } }
Example #12
Source File: FormTag.java From javalite with Apache License 2.0 | 4 votes |
@Override protected void render(Map params, String body, Writer writer) throws Exception { SimpleHash activeweb = (SimpleHash) get("activeweb"); if(activeweb == null || !(params.containsKey("controller") || activeweb.toMap().containsKey("controller"))) throw new ViewException("could not render this form, controller is not found"); String httpMethod = Convert.toString(params.get("method")); if (httpMethod != null) { httpMethod = httpMethod.toLowerCase(); } boolean putOrDelete = "put".equals(httpMethod) || "delete".equals(httpMethod); String bodyPrefix = ""; if (CSRF.verificationEnabled() && (putOrDelete || "post".equals(httpMethod))) { bodyPrefix = "\n\t<input type='hidden' name='" + CSRF.name() + "' value='" + CSRF.token() + "' />"; } if(putOrDelete){ bodyPrefix += "\n\t<input type='hidden' name='_method' value='" + httpMethod + "' />"; httpMethod = "post"; } if(blank(body)){ body = " "; } TagFactory tf = new TagFactory("form", bodyPrefix + body); String action = Convert.toString(params.get("action")); Boolean restful = null; String controllerPath = Convert.toString(params.get("controller")); if (controllerPath == null) { controllerPath = activeweb.get("controller").toString(); restful = ((TemplateBooleanModel)activeweb.get("restful")).getAsBoolean(); } if(restful == null){// using current controller AppController controllerInstance = (AppController) Class.forName( ControllerFactory.getControllerClassName(controllerPath) ).getDeclaredConstructor().newInstance(); restful = controllerInstance.restful(); } String id = Convert.toString(params.get("id")); String formAction = Router.generate(controllerPath, action, id, restful, new HashMap()); tf.attribute("action", getContextPath() + formAction); tf.attribute("method", httpMethod); tf.attribute("id", Convert.toString(params.get("html_id"))); tf.addAttributesExcept(params, "controller", "action", "method", "id", "html_id", "data"); tf.textAttributes(Convert.toString(params.get("data"))); tf.write(writer); }
Example #13
Source File: GetEffortDetailsForProjectTraversalMethod.java From windup with Eclipse Public License 1.0 | 4 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { // Process arguments ExecutionStatistics.get().begin(NAME); if (arguments.size() < 2) { throw new TemplateModelException( "Error, method expects at least three arguments" + " (projectModel: ProjectModel, recursive: Boolean, [includeTags: Set<String>], [excludeTags: Set<String>])"); } StringModel projectModelTraversalArg = (StringModel) arguments.get(0); ProjectModelTraversal projectModelTraversal = (ProjectModelTraversal) projectModelTraversalArg.getWrappedObject(); TemplateBooleanModel recursiveBooleanModel = (TemplateBooleanModel) arguments.get(1); boolean recursive = recursiveBooleanModel.getAsBoolean(); Set<String> includeTags = Collections.emptySet(); if (arguments.size() >= 3) { includeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(2)); } Set<String> excludeTags = Collections.emptySet(); if (arguments.size() >= 4) { excludeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(3)); } Set<String> issueCategories = Collections.emptySet(); if (arguments.size() >= 5) { issueCategories = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(4)); } // Get values for classification and hints. Map<Integer, Integer> classificationEffortDetails = classificationService.getMigrationEffortByPoints(projectModelTraversal, includeTags, excludeTags, issueCategories, recursive, false); Map<Integer, Integer> hintEffortDetails = inlineHintService.getMigrationEffortByPoints(projectModelTraversal, includeTags, excludeTags, issueCategories, recursive, false); Map<Integer, Integer> results = sumMaps(classificationEffortDetails, hintEffortDetails); ExecutionStatistics.get().end(NAME); int points = sumPoints(results); LOG.fine(String.format("%s() FM function called:\n\t\t\tEFFORT: %3d = %s = C%s + H%s; %s, %srecur, tags: %s, excl: %s", NAME, points, results, classificationEffortDetails, hintEffortDetails, projectModelTraversal, recursive ? "" : "!", includeTags, excludeTags)); return results; }
Example #14
Source File: GetEffortCountForProjectByIssueCategoryMethod.java From windup with Eclipse Public License 1.0 | 4 votes |
@Override public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { ExecutionStatistics.get().begin(NAME); if (arguments.size() < 3) { throw new TemplateModelException( "Error, method expects at least three arguments (event:GraphRewrite, projectModelTraversal:ProjectModelTraversal, recursive:Boolean, [includeTags:Set<String>]. [excludeTags:Set<String>])"); } GraphRewrite event = (GraphRewrite)((StringModel)arguments.get(0)).getWrappedObject(); StringModel projectModelTraversalArg = (StringModel) arguments.get(1); ProjectModelTraversal traversal = (ProjectModelTraversal) projectModelTraversalArg.getWrappedObject(); TemplateBooleanModel recursiveBooleanModel = (TemplateBooleanModel) arguments.get(2); boolean recursive = recursiveBooleanModel.getAsBoolean(); Set<String> includeTags = Collections.emptySet(); if (arguments.size() >= 4) { includeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(3)); } Set<String> excludeTags = Collections.emptySet(); if (arguments.size() >= 5) { excludeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) arguments.get(4)); } Map<IssueCategoryModel, Integer> classificationEffortDetails = classificationService.getMigrationEffortBySeverity(event, traversal, includeTags, excludeTags, Collections.emptySet(), recursive); Map<IssueCategoryModel, Integer> hintEffortDetails = inlineHintService.getMigrationEffortBySeverity(event, traversal, includeTags, excludeTags, Collections.emptySet(), recursive); Map<IssueCategoryModel, Integer> results = new TreeMap<>(new IssueCategoryModel.IssueSummaryPriorityComparator()); addAllIncidents(results, classificationEffortDetails); addAllIncidents(results, hintEffortDetails); ExecutionStatistics.get().end(NAME); return results; }
Example #15
Source File: RenderApplicationPieChartDirective.java From windup with Eclipse Public License 1.0 | 4 votes |
@Override public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { StringModel projectStringModel = (StringModel) params.get("project"); ProjectModel projectModel = null; if (projectStringModel != null) projectModel = (ProjectModel) projectStringModel.getWrappedObject(); StringModel projectTraversalStringModel = (StringModel) params.get("projectTraversal"); ProjectModelTraversal projectTraversal = null; if (projectTraversalStringModel != null) projectTraversal = (ProjectModelTraversal) projectTraversalStringModel.getWrappedObject(); if (projectModel != null && projectTraversal != null) throw new WindupException(NAME + " both 'project' and 'projectTraversal' were specified. Only one of these values should be specified."); else if (projectModel == null && projectTraversal == null) throw new WindupException(NAME + " neither 'project' nor 'projectTraversal' were specified. At least one of these must be specified."); TemplateBooleanModel recursiveBooleanModel = (TemplateBooleanModel) params.get("recursive"); boolean recursive = recursiveBooleanModel.getAsBoolean(); SimpleScalar elementIDStringModel = (SimpleScalar) params.get("elementID"); String elementID = elementIDStringModel.getAsString(); Set<String> includeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) params.get("includeTags")); Set<String> excludeTags = FreeMarkerUtil.simpleSequenceToSet((SimpleSequence) params.get("excludeTags")); TypeReferenceService typeReferenceService = new TypeReferenceService(context); Map<String, Integer> data; if (projectModel != null) data = typeReferenceService.getPackageUseFrequencies(projectModel, includeTags, excludeTags, 2, recursive); else data = typeReferenceService.getPackageUseFrequencies(projectTraversal, includeTags, excludeTags, 2, recursive); if (data.keySet().size() > 0) { drawPie(env.getOut(), data, elementID); } else { // if we aren't drawing a pie, remove the element that would have held it Writer writer = env.getOut(); writer.append("<script type='text/javascript'>"); writer.append("$('#" + elementID + "').parent().remove()"); writer.append("</script>"); } }