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

ru.asubezdna.edata.EDUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Бездна (c) 2022.
 */
package ru.asubezdna.edata;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;

import ru.asubezdna.edata.v1_10.Message;
import ru.asubezdna.edata.v1_10.ObjectFactory;

/**
 * @author Minu <[email protected]>
 * @since 08.11.2022 10:58:51
 */
public class EDUtils {

	private static JAXBContext context;

	private static JAXBContext getContext() throws JAXBException {
		if (context == null)
			context = JAXBContext.newInstance(ObjectFactory.class);
		return context;
	}

	@SuppressWarnings("unchecked")
	public static Message readED(File file) throws JAXBException {
		Object ret = getContext().createUnmarshaller().unmarshal(file);
        return ((ret != null) && (ret instanceof JAXBElement)) ? ((JAXBElement) ret).getValue() : null;
	}

	@SuppressWarnings("unchecked")
	public static Message readED(InputStream stream) throws JAXBException {
		Object ret = getContext().createUnmarshaller().unmarshal(stream);
        return ((ret != null) && (ret instanceof JAXBElement)) ? ((JAXBElement) ret).getValue() : null;
	}

	public static void writeED(Message edata, File file) throws JAXBException {
        getContext().createMarshaller().marshal(edata, file);
	}

	public static void writeED(Message edata, OutputStream stream) throws JAXBException {
        getContext().createMarshaller().marshal(edata, stream);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy