All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.itelg.spring.xml.marshaller.XomWriter Maven / Gradle / Ivy

There is a newer version: 0.3.0-RELEASE
Show newest version
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));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy