in src/main/java/org/apache/sling/scripting/sightly/impl/engine/extension/FormatFilterExtension.java [70:104]
public Object call(final RenderContext renderContext, Object... arguments) {
ExtensionUtils.checkArgumentCount(RuntimeExtension.FORMAT, arguments, 2);
RuntimeObjectModel runtimeObjectModel = renderContext.getObjectModel();
String source = runtimeObjectModel.toString(arguments[0]);
Map<String, Object> options = (Map<String, Object>) arguments[1];
String formattingType = runtimeObjectModel.toString(options.get(TYPE_OPTION));
Object formatObject = options.get(FORMAT_OPTION);
boolean hasPlaceHolders = PLACEHOLDER_REGEX.matcher(source).find() || COMPLEX_PLACEHOLDER_REGEX.matcher(source).find();
if (STRING_FORMAT_TYPE.equals(formattingType)) {
return getFormattedString(runtimeObjectModel, source, options, formatObject);
} else if (DATE_FORMAT_TYPE.equals(formattingType) || (!hasPlaceHolders && runtimeObjectModel.isDate(formatObject))) {
return getDateFormattedString(runtimeObjectModel, source, options, formatObject);
} else if (NUMBER_FORMAT_TYPE.equals(formattingType) || (!hasPlaceHolders && runtimeObjectModel.isNumber(formatObject))) {
return getNumberFormattedString(runtimeObjectModel, source, options, formatObject);
}
if (hasPlaceHolders) {
return getFormattedString(runtimeObjectModel, source, options, formatObject);
}
try {
// try to parse as DateTimeFormatter
DateTimeFormatter.ofPattern(source);
return getDateFormattedString(runtimeObjectModel, source, options, formatObject);
} catch (IllegalArgumentException ex) {
LOG.trace("Not a datetime format: {}", source, ex);
}
try {
// for this too, but such is life
new DecimalFormat(source);
return getNumberFormattedString(runtimeObjectModel, source, options, formatObject);
} catch (IllegalArgumentException e) {
// ignore
}
return getFormattedString(runtimeObjectModel, source, options, formatObject);
}