freemarker.template.TemplateDateModel Java Examples
The following examples show how to use
freemarker.template.TemplateDateModel.
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: 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 #2
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 #3
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 #4
Source File: I18NMessageMethod.java From alfresco-repository with GNU Lesser General Public License v3.0 | 4 votes |
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { String result = ""; int argSize = args.size(); if (argSize != 0) { String id = ""; Object arg0 = args.get(0); if (arg0 instanceof TemplateScalarModel) { id = ((TemplateScalarModel)arg0).getAsString(); } if (id != null) { if (argSize == 1) { // shortcut for no additional msg params result = I18NUtil.getMessage(id); } else { Object[] params = new Object[argSize - 1]; for (int i = 0; i < argSize-1; i++) { // ignore first passed-in arg which is the msg id Object arg = args.get(i + 1); if (arg instanceof TemplateScalarModel) { params[i] = ((TemplateScalarModel)arg).getAsString(); } else if (arg instanceof TemplateNumberModel) { params[i] = ((TemplateNumberModel)arg).getAsNumber(); } else if (arg instanceof TemplateDateModel) { params[i] = ((TemplateDateModel)arg).getAsDate(); } else { params[i] = ""; } } result = I18NUtil.getMessage(id, params); } } } return result; }
Example #5
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 #6
Source File: DateModel.java From pcgen with GNU Lesser General Public License v2.1 | 4 votes |
@Override public int getDateType() { return TemplateDateModel.UNKNOWN; }
Example #7
Source File: DateModel.java From pcgen with GNU Lesser General Public License v2.1 | 4 votes |
@Override public int getDateType() { return TemplateDateModel.UNKNOWN; }
Example #8
Source File: AbstractMessageFormatTemplateMethod.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public Object exec( @SuppressWarnings( "rawtypes" ) List arguments ) throws TemplateModelException { int argsSize = arguments.size( ); if ( argsSize < 1 ) { throw new TemplateModelException( "Must be called with at least one argument (the message key)" ); } String key = ( (TemplateScalarModel) arguments.get( 0 ) ).getAsString( ); Locale locale = Environment.getCurrentEnvironment( ).getLocale( ); if ( argsSize == 1 ) { // no arguments, the message is not a MessageFormat return getPattern( key, locale ); } Object [ ] args = new Object [ argsSize - 1]; for ( int i = 1; i < argsSize; i++ ) { TemplateModel arg = (TemplateModel) arguments.get( i ); if ( arg instanceof TemplateScalarModel ) { args [i - 1] = ( (TemplateScalarModel) arg ).getAsString( ); } else if ( arg instanceof TemplateNumberModel ) { args [i - 1] = ( (TemplateNumberModel) arg ).getAsNumber( ); } else if ( arg instanceof TemplateDateModel ) { args [i - 1] = ( (TemplateDateModel) arg ).getAsDate( ); } else { throw new TemplateModelException( "Unsupported argument type : " + arg ); } } return new MessageFormat( getPattern( key, locale ), locale ).format( args, new StringBuffer( ), null ).toString( ); }