@Override public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException { testAtLeastOneArgument(arguments); String value = readValue(arguments.get(0)); MessageFormat formatter = new MessageFormat(value, bundle.getLocale()); if (arguments.size() == 1) { value = formatter.format(new Object[] {}); } else { List<Object> parameters = new ArrayList<>(); for (Object obj : arguments.subList(1, arguments.size())) { if (obj instanceof SimpleNumber) { parameters.add(((SimpleNumber) obj).getAsNumber()); } else if (obj instanceof SimpleDate) { parameters.add(((SimpleDate) obj).getAsDate()); } else { parameters.add(obj.toString()); } } value = formatter.format(parameters.toArray(), new StringBuffer(), null).toString(); } return value; }
@Override @SuppressWarnings("rawtypes") public TemplateModel exec(List args) { long sum = 0; for (Object arg : args) { arg = LiteralParser.parse((String) arg); if (arg instanceof Date) sum += ((Date) arg).getTime(); else if (arg instanceof Timestamp) sum += ((Timestamp) arg).getTime(); else if (arg instanceof Number) sum += ((Number) arg).longValue(); else if (arg != null) throw new IllegalArgumentException("Not a supported date type: " + arg.getClass()); } return new SimpleDate(new java.sql.Date(sum)); }
public static Date getDate(Map params, String paramName){ SimpleDate valueModel=(SimpleDate)params.get(paramName); if (valueModel!=null){ Date value=valueModel.getAsDate(); return value; } return null; }
@Test public void testParamTranslation() throws TemplateModelException, ParseException { ResourceBundle bundle = new SimpleBundle("foo", "bar {0} {1} {2}"); FreemarkerMessageMethod method = new FreemarkerMessageMethod(bundle); SimpleNumber simpleNumber = new SimpleNumber(1234567); Date date = new Date(new SimpleDateFormat("yyyy-MM-dd hh:mm").parse("2014-03-14 13:11").getTime()); SimpleDate simpleDate = new SimpleDate(date); assertEquals("bar baz 1,234,567 3/14/14 1:11 PM", method.exec(Arrays.asList("foo", "baz", simpleNumber, simpleDate))); }
private Date getFormattableObject(Object value) { if (value instanceof SimpleDate) { return ((SimpleDate) value).getAsDate(); } else { throw new PippoRuntimeException("Formattable object for FormatTime not found!"); } }
private Date getFormattableObject(Object value) { if (value instanceof SimpleDate) { return ((SimpleDate) value).getAsDate(); } else { throw new PippoRuntimeException("Formattable object for PrettyTime not found!"); } }
@SuppressWarnings("rawtypes") @Override public TemplateModel wrap(Object obj) throws TemplateModelException { if (obj == null) { return super.wrap(null); } if (obj instanceof TemplateModel) { return (TemplateModel) obj; } if (obj instanceof String) { return new SimpleScalar((String) obj); } if (obj instanceof Number) { return new SimpleNumber((Number) obj); } if (obj instanceof java.util.Date) { if(obj instanceof java.sql.Date) { return new SimpleDate((java.sql.Date) obj); } if(obj instanceof java.sql.Time) { return new SimpleDate((java.sql.Time) obj); } if(obj instanceof java.sql.Timestamp) { return new SimpleDate((java.sql.Timestamp) obj); } return new SimpleDate((java.util.Date) obj, getDefaultDateType()); } if (obj.getClass().isArray()) { obj = convertArray(obj); } if (obj instanceof Collection) { return new SimpleSequence((Collection) obj, this); } if (obj instanceof Boolean) { return obj.equals(Boolean.TRUE) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } if (obj instanceof Iterator) { return new SimpleCollection((Iterator) obj, this); } return handleUnknownType(obj); }
@Override @SuppressWarnings("rawtypes") public TemplateModel exec(List args) { return new SimpleDate(new Date(System.currentTimeMillis())); }