package com.xero.api.jaxb;

import com.xero.api.XeroClientException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

public class XeroJAXBMarshaller {

    private final Marshaller marshaller;
    private final Unmarshaller unmarshaller;

    public XeroJAXBMarshaller() {
        try {
            JAXBContext context = JAXBContext.newInstance("com.xero.model");
            marshaller = context.createMarshaller();
            unmarshaller = context.createUnmarshaller();
        } catch (Exception e) {
            throw new XeroClientException(e.getMessage(), e);
        }
    }

    public String marshall(JAXBElement<?> object) {
        try {
            StringWriter writer = new StringWriter();
            marshaller.marshal(object, writer);
            return writer.toString();
        } catch (JAXBException e) {
            throw new IllegalStateException("Error marshalling request object " + object.getClass(), e);
        }
    }

    public <T> T unmarshall(String responseBody, Class<T> clazz) throws UnsupportedEncodingException {
        try {
            Source source = new StreamSource(new StringReader(responseBody));
            return unmarshaller.unmarshal(source, clazz).getValue();
        } catch (JAXBException e) {
            throw new IllegalStateException("Error unmarshalling response: " + responseBody, e);
        }
    }

}

@@ -427,16 +427,271 @@
 ption;%0A%0A
+/**%0A * Marshall and unmarshall from schema derived class and/or java%0A * to schema (JAXB-annotated) mapped classes found in com.xero.model%0A * Will throw %7B@link XeroClientException%7D if %7B@link JAXBContext%7D%0A * cannot create the marshaller or unmarshaller%0A */%0A
 public c

update java doc