use of net.sf.ezmorph.Morpher in project jaffa-framework by jaffa-projects.
the class ExcelExportService method registerCustomDateMorpher.
/**
* Registers a custom Morpher to handle, based on the input flag, either a DateTime or DateOnly class.
*/
private static void registerCustomDateMorpher(boolean dateTime) {
final Class targetType = dateTime ? DateTime.class : DateOnly.class;
Morpher targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
synchronized (JSONUtils.getMorpherRegistry()) {
targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
// Create a custom Morpher
targetMorpher = new ObjectMorpher() {
/**
* Returns the target Class (DateTime.class or DateOnly.class) for conversion.
*/
public Class morphsTo() {
return targetType;
}
/**
* Returns true if the Morpher supports conversion from this Class. Only the String class is supported currently.
*/
public boolean supports(Class clazz) {
return clazz == String.class;
}
/**
* Morphs the input object into an output object of the supported type.
*/
public Object morph(Object value) {
try {
String layout = "yyyy-MM-dd'T'HH:mm:ss";
return targetType == DateTime.class ? Parser.parseDateTime((String) value, layout) : Parser.parseDateOnly((String) value, layout);
} catch (Exception e) {
if (log.isDebugEnabled())
log.debug("Error in converting '" + value + "' to " + (targetType == DateTime.class ? "DateTime" : "DateOnly"), e);
return value;
}
}
};
JSONUtils.getMorpherRegistry().registerMorpher(targetMorpher);
}
}
}
}
use of net.sf.ezmorph.Morpher in project jaffa-framework by jaffa-projects.
the class ExcelExportService method registerCustomDateMorpher.
/**
* Registers a custom Morpher to handle, based on the input flag, either a DateTime or DateOnly class.
*/
private static void registerCustomDateMorpher(boolean dateTime) {
final Class targetType = dateTime ? DateTime.class : DateOnly.class;
Morpher targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
synchronized (JSONUtils.getMorpherRegistry()) {
targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
// Create a custom Morpher
targetMorpher = new ObjectMorpher() {
/**
* Returns the target Class (DateTime.class or DateOnly.class) for conversion.
*/
public Class morphsTo() {
return targetType;
}
/**
* Returns true if the Morpher supports conversion from this Class. Only the String class is supported currently.
*/
public boolean supports(Class clazz) {
return clazz == String.class;
}
/**
* Morphs the input object into an output object of the supported type.
*/
public Object morph(Object value) {
try {
String layout = "yyyy-MM-dd'T'HH:mm:ss";
return targetType == DateTime.class ? Parser.parseDateTime((String) value, layout) : Parser.parseDateOnly((String) value, layout);
} catch (Exception e) {
if (log.isDebugEnabled())
log.debug("Error in converting '" + value + "' to " + (targetType == DateTime.class ? "DateTime" : "DateOnly"), e);
return value;
}
}
};
JSONUtils.getMorpherRegistry().registerMorpher(targetMorpher);
}
}
}
}
Aggregations