com.itelg.spring.xml.marshaller.XomWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-xml-extensions Show documentation
Show all versions of spring-xml-extensions Show documentation
Spring Marshaller/Unmarshaller based on Xom
package com.itelg.spring.xml.marshaller;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
public abstract class XomWriter extends AbstractWriter
{
private final DateTimeFormatter format = DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault());
@Override
protected String doWrite(T object)
{
return build(object).toXML();
}
protected abstract Document build(T object);
protected void addAttribute(Element element, String name, Object value)
{
if (value != null)
{
element.addAttribute(new Attribute(name, value.toString()));
}
}
protected void addAttribute(Element element, String name, int value)
{
element.addAttribute(new Attribute(name, String.valueOf(value)));
}
protected void addAttribute(Element element, String name, long value)
{
element.addAttribute(new Attribute(name, String.valueOf(value)));
}
protected void addAttribute(Element element, String name, double value)
{
element.addAttribute(new Attribute(name, String.valueOf(value)));
}
protected void addAttribute(Element element, String name, Date value)
{
if (value != null)
{
String dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S 'UTC'").format(value);
element.addAttribute(new Attribute(name, dateString));
}
}
protected void addAttribute(Element element, String name, LocalDateTime value)
{
if (value != null)
{
String dateString = ZonedDateTime.of(value, ZoneId.systemDefault()).format(format);
element.addAttribute(new Attribute(name, dateString));
}
}
}