@Override public Object apply(Object input, Map<String, Object> args) { if (input == null) { return null; } Date date = null; DateFormat existingFormat = null; DateFormat intendedFormat = null; EvaluationContext context = (EvaluationContext) args.get("_context"); Locale locale = context.getLocale(); intendedFormat = new SimpleDateFormat((String) args.get("format"), locale); if (args.get("existingFormat") != null) { existingFormat = new SimpleDateFormat((String) args.get("existingFormat"), locale); try { date = existingFormat.parse((String) input); } catch (ParseException e) { throw new RuntimeException("Could not parse date", e); } } else { date = (Date) input; } return new SafeString(intendedFormat.format(date)); }
@Override public Object evaluate(PebbleTemplateImpl self, EvaluationContext context) throws PebbleException { FilterInvocationExpression filterInvocation = (FilterInvocationExpression) getRightExpression(); ArgumentsNode args = filterInvocation.getArgs(); String filterName = filterInvocation.getFilterName(); if (this.filter == null) { Map<String, Filter> filters = context.getFilters(); this.filter = filters.get(filterInvocation.getFilterName()); } if (filter == null) { throw new PebbleException(null, String.format("Filter [%s] does not exist.", filterName)); } if (filter instanceof LocaleAware) { ((LocaleAware) filter).setLocale(context.getLocale()); } Map<String, Object> namedArguments = args.getArgumentMap(self, context, filter); return filter.apply(getLeftExpression().evaluate(self, context), namedArguments); }
@Override public Object execute(Map<String, Object> args) { String key = (String) args.get("0"); int i = 1; List<Object> arguments = new ArrayList<>(); while (args.containsKey(String.valueOf(i))) { Object param = args.get(String.valueOf(i)); arguments.add(param); i++; } EvaluationContext context = (EvaluationContext) args.get("_context"); Locale locale = context.getLocale(); return this.messageSource.getMessage(key, arguments.toArray(), "???" + key + "???", locale); }