public Date getAsDate() throws TemplateModelException { return ((TemplateDateModel) model).getAsDate(); }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateDateModel) { TemplateDateModel dmodel = (TemplateDateModel) model; int dtype = dmodel.getDateType(); // Any date model can be coerced into its own type if (dateType == dtype) { return model; } // unknown and datetime can be coerced into any date type if (dtype == TemplateDateModel.UNKNOWN || dtype == TemplateDateModel.DATETIME) { return new SimpleDate(dmodel.getAsDate(), dateType); } throw new _MiscTemplateException(this, "Cannot convert ", TemplateDateModel.TYPE_NAMES.get(dtype), " to ", TemplateDateModel.TYPE_NAMES.get(dateType)); } // Otherwise, interpret as a string and attempt // to parse it into a date. String s = target.evalAndCoerceToPlainText(env); return new DateParser(s, env); }
public int getDateType() { return ((TemplateDateModel) model).getDateType(); }
DateFormatter(TemplateDateModel dateModel, Environment env) throws TemplateException { this.dateModel = dateModel; this.env = env; final int dateType = dateModel.getDateType(); this.defaultFormat = dateType == TemplateDateModel.UNKNOWN ? null // Lazy unknown type error in getAsString() : env.getTemplateDateFormat( dateType, EvalUtil.modelToDate(dateModel, target).getClass(), target, true); }
/** * @param expr {@code null} is allowed, but may results in less helpful error messages */ static Date modelToDate(TemplateDateModel model, Expression expr) throws TemplateModelException { Date value = model.getAsDate(); if (value == null) throw newModelHasStoredNullException(Date.class, model, expr); return value; }
private Map<String, Object> getTemplateModelDump(TemplateDateModel model) throws TemplateModelException { Map<String, Object> map = new HashMap<String, Object>(); map.put(Key.TYPE.toString(), Type.DATE); int dateType = model.getDateType(); DateType type; switch (dateType) { case TemplateDateModel.DATE: type = DateType.DATE; break; case TemplateDateModel.DATETIME: type = DateType.DATETIME; break; case TemplateDateModel.TIME: type = DateType.TIME; break; default: type = DateType.UNKNOWN; } map.put(Key.DATE_TYPE.toString(), type); map.put(Key.VALUE.toString(), model.getAsDate()); return map; }
TemplateDateFormat getTemplateDateFormat(TemplateDateModel tdm, Expression tdmSourceExpr, boolean useTempModelExc) throws TemplateModelException, TemplateException { Date date = EvalUtil.modelToDate(tdm, tdmSourceExpr); TemplateDateFormat format = getTemplateDateFormat( tdm.getDateType(), date.getClass(), tdmSourceExpr, useTempModelExc); return format; }
public Date getAsDate() throws TemplateModelException { return getAsDateModel().getAsDate(); }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateDateModel) { TemplateDateModel dmodel = (TemplateDateModel) model; int dtype = dmodel.getDateType(); // Any date model can be coerced into its own type if (dateType == dtype) { return model; } // unknown and datetime can be coerced into any date type if (dtype == TemplateDateModel.UNKNOWN || dtype == TemplateDateModel.DATETIME) { return new SimpleDate(dmodel.getAsDate(), dateType); } throw new _MiscTemplateException(this, "Cannot convert ", TemplateDateModel.TYPE_NAMES.get(dtype), " to ", TemplateDateModel.TYPE_NAMES.get(dateType)); } // Otherwise, interpret as a string and attempt // to parse it into a date. String s = target.evalAndCoerceToPlainText(env); return new DateParser(s, env); }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel tm = target.eval(env); target.assertNonNull(tm, env); return (tm instanceof TemplateDateModel) && ((TemplateDateModel) tm).getDateType() == dateType ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } }
/** * Utility method to extract the {@link Date} from an {@link TemplateDateModel}, and throw * {@link TemplateModelException} with a standard error message if that's {@code null}. {@link TemplateDateModel} * that store {@code null} are in principle not allowed, and so are considered to be bugs in the * {@link ObjectWrapper} or {@link TemplateNumberModel} implementation. */ public static Date getNonNullDate(TemplateDateModel dateModel) throws TemplateModelException { Date date = dateModel.getAsDate(); if (date == null) { throw EvalUtil.newModelHasStoredNullException(Date.class, dateModel, null); } return date; }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateDateModel) { TemplateDateModel dmodel = (TemplateDateModel) model; int dtype = dmodel.getDateType(); // Any date model can be coerced into its own type if (dateType == dtype) { return model; } // unknown and datetime can be coerced into any date type if (dtype == TemplateDateModel.UNKNOWN || dtype == TemplateDateModel.DATETIME) { return new SimpleDate(dmodel.getAsDate(), dateType); } throw new _MiscTemplateException(this, "Cannot convert ", TemplateDateModel.TYPE_NAMES.get(dtype), " to ", TemplateDateModel.TYPE_NAMES.get(dateType)); } // Otherwise, interpret as a string and attempt // to parse it into a date. String s = target.evalAndCoerceToPlainText(env); return new DateParser(s, env); }
private TemplateDateModel toTemplateDateModel(Object date) throws _TemplateModelException { if (date instanceof Date) { return new SimpleDate((Date) date, dateType); } else { TemplateDateModel tm = (TemplateDateModel) date; if (tm.getDateType() != dateType) { throw new _TemplateModelException("The result of the parsing was of the wrong date type."); } return tm; } }
return ((TemplateDateModel) tm).getAsDate();
TemplateModel _getAsTemplateModel(Environment env) throws TemplateException { TemplateModel model = target.getAsTemplateModel(env); if (model instanceof TemplateDateModel) { TemplateDateModel dmodel = (TemplateDateModel)model; int dtype = dmodel.getDateType(); // Any date model can be coerced into its own type if(dateType == dtype) { return model; } // unknown and datetime can be coerced into any date type if(dtype == TemplateDateModel.UNKNOWN || dtype == TemplateDateModel.DATETIME) { return new SimpleDate(dmodel.getAsDate(), dateType); } throw new TemplateException( "Cannot convert " + TemplateDateModel.TYPE_NAMES.get(dtype) + " into " + TemplateDateModel.TYPE_NAMES.get(dateType), env); } // Otherwise, interpret as a string and attempt // to parse it into a date. String s = target.getStringValue(env); return new DateParser(s, env); }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateDateModel) { TemplateDateModel tdm = (TemplateDateModel) model; return calculateResult(EvalUtil.modelToDate(tdm, target), tdm.getDateType(), env); } else { throw newNonDateException(env, model, target); } }
TemplateDateModel ltdm = (TemplateDateModel)model1; TemplateDateModel rtdm = (TemplateDateModel)model2; int ltype = ltdm.getDateType(); int rtype = rtdm.getDateType(); if(ltype != rtype) { throw new TemplateModelException( "Right date is of UNKNOWN type, and can not be compared."); Date first = ltdm.getAsDate(); Date second = rtdm.getAsDate(); comp = first.compareTo(second);
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateDateModel) { TemplateDateModel tdm = (TemplateDateModel) model; int tdmDateType = tdm.getDateType(); if (tdmDateType != TemplateDateModel.UNKNOWN) { return tdm; } return new SimpleDate(EvalUtil.modelToDate(tdm, target), dateType); } else { throw BuiltInForDate.newNonDateException(env, model, target); } }
public Object getVariableValue(String namespaceURI, String prefix, String localName) throws UnresolvableException { try { TemplateModel model = Environment.getCurrentEnvironment().getVariable(localName); if (model == null) { throw new UnresolvableException("Variable \"" + localName + "\" not found."); } if (model instanceof TemplateScalarModel) { return ((TemplateScalarModel) model).getAsString(); } if (model instanceof TemplateNumberModel) { return ((TemplateNumberModel) model).getAsNumber(); } if (model instanceof TemplateDateModel) { return ((TemplateDateModel) model).getAsDate(); } if (model instanceof TemplateBooleanModel) { return Boolean.valueOf(((TemplateBooleanModel) model).getAsBoolean()); } } catch (TemplateModelException e) { throw new UndeclaredThrowableException(e); } throw new UnresolvableException( "Variable \"" + localName + "\" exists, but it's not a string, number, date, or boolean"); } };