1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
@Slf4j public class XmlIntegerConverter implements Converter {
public XmlIntegerConverter() { }
@Override public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { String target = format((Integer) source); log.debug("java {} -> xml String, source: {}, target: {}", source.getClass(), source, target); writer.setValue(target); }
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Class type = context.getRequiredType(); String source = reader.getValue(); Integer target = parse(source); log.debug("xml String -> java {}, source: {}, target: {}", type, source, target); return target; }
@Override public boolean canConvert(Class type) { return Integer.class.equals(type); }
private String format(Integer value) { String string = String.format("%s", value); if (string.length() < 4) { string = StringUtils.repeat("0", 4 - string.length()) + string; } return string; }
private static Integer parse(String string) { if (StringUtils.isBlank(string)) { return null; } return Integer.valueOf(string); } }
|