* @param pattern The string pattern representing the formatter pattern.
* @param locale The ULocale of the formatter.
* @return A formatter for the given type, pattern and locale, or null if the pattern is illegal.
*/
public static ValueFormatter createFromPattern(ValueType type, String pattern, ULocale locale) {
UFormat uFormat = null;
if(pattern == null) {
pattern = getDefaultPatternByType(type);
}
if(locale == null) {
locale = LocaleUtil.getDefaultLocale();
}
// For whichever formatter is created, try to format some arbitrary value, and see if an
// exception was thrown. If it was thrown, conclude the pattern was illegal, and return null.
try {
switch(type) {
case BOOLEAN:
uFormat = new BooleanFormat(pattern);
uFormat.format(BooleanValue.TRUE.getObjectToFormat());
break;
case TEXT:
// Dummy format so no need to check it for problems.
uFormat = new TextFormat();
break;
case DATE:
uFormat = new SimpleDateFormat(pattern, locale);
((SimpleDateFormat) uFormat).setTimeZone(TimeZone.getTimeZone("GMT"));
uFormat.format(new DateValue(1995, 7, 3).getObjectToFormat());
break;
case TIMEOFDAY:
uFormat = new SimpleDateFormat(pattern, locale);
((SimpleDateFormat) uFormat).setTimeZone(TimeZone.getTimeZone("GMT"));
uFormat.format(new TimeOfDayValue(2, 59, 12, 123).getObjectToFormat());
break;
case DATETIME:
uFormat = new SimpleDateFormat(pattern, locale);
((SimpleDateFormat) uFormat).setTimeZone(TimeZone.getTimeZone("GMT"));
uFormat.format(new DateTimeValue(1995, 7, 3, 2, 59, 12, 123).getObjectToFormat());
break;
case NUMBER:
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
uFormat = new DecimalFormat(pattern, symbols);
uFormat.format(new NumberValue(-12.3).getObjectToFormat());
break;
}
}
catch(RuntimeException e) {
// The formatter is illegal return null.