package ieg.util.xml;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * Xml serializer with <a href="https://jaxb.dev.java.net">jaxb</a>
 * 
 * @author Alex Rind, Thomas Turic 
 */
public class JaxbMarshaller {

	/**
	 * loads an object from an XML file. The XML file has to be in UTF-8.
	 * 
	 * @param xmlFile
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("rawtypes")
    public static Object load(String xmlFile, Class... clazz) {
		Object model = null;
		try {
			model = loadUser(xmlFile, clazz);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return model;
	}

	/**
	 * loads an object from an XML file without catching exceptions. 
	 * The XML file has to be in UTF-8.
	 * 
	 * @param xmlFile
	 * @param clazz
	 * @return
	 * @throws IOException
	 * @throws JAXBException
	 */
	@SuppressWarnings("rawtypes")
	public static Object loadUser(String xmlFile, Class... clazz)
			throws IOException, JAXBException {
		Reader reader;
		Object model = null;
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(
				xmlFile), "UTF-8"));

		JAXBContext context;

		context = JAXBContext.newInstance(clazz);

		Unmarshaller m;

		m = context.createUnmarshaller();
		model = m.unmarshal(reader);

		reader.close();

		return model;
	}

	/**
	 * serializes an object and writes it to an XML file. The XML file is always
	 * in UTF-8.
	 * 
	 * @param xmlFile
	 * @param obj
	 */
    public static void save(String xmlFile, Object o) {
        save(xmlFile, o, o.getClass());
    }
    
    /**
     * serializes an object and writes it to an XML file. The XML file is always
     * in UTF-8.
     * 
     * @param xmlFile
     * @param obj
     * @param clazz
     */
	@SuppressWarnings("rawtypes")
    public static void save(String xmlFile, Object o, Class... clazz) {
		try {
			Writer writer = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(xmlFile), "UTF-8"));
			JAXBContext context = JAXBContext.newInstance(clazz);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(o, writer);
			writer.close();

		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

@@ -170,32 +170,61 @@
 o.IOException;%0D%0A
+import java.io.InputStream;%0D%0A
 import java.io.I
@@ -325,16 +325,38 @@
 Writer;%0D
+%0Aimport java.net.URL;%0D
 %0A%0D%0Aimpor
@@ -1487,51 +1487,458 @@
 %7B%0D%0A%09
-%09Reader reader;%0D%0A%09%09Object model = null;%0D%0A%09%09
+    return loadUser(new FileInputStream(xmlFile), clazz);%0D%0A%09%7D%0D%0A%09%09%0D%0A    @SuppressWarnings(%22rawtypes%22)%0D%0A    public static Object loadUser(URL xmlFile, Class... clazz)%0D%0A            throws IOException, JAXBException %7B%0D%0A        return loadUser(xmlFile.openStream(), clazz);%0D%0A    %7D%0D%0A        %0D%0A%09@SuppressWarnings(%22rawtypes%22)%0D%0A%09public static Object loadUser(InputStream xmlFile, Class... clazz) %0D%0A            throws IOException, JAXBException %7B%0D%0A%09    Reader 
 read
@@ -1987,42 +1987,15 @@
 der(
-new FileInputStream(%0D%0A%09%09%09%09
 xmlFile
-)
 , %22U
@@ -2024,22 +2024,8 @@
 ext 
-context;%0D%0A%0D%0A%09%09
 cont
@@ -2063,18 +2063,16 @@
 lazz);%0D%0A
-%0D%0A
 %09%09Unmars
@@ -2082,16 +2082,8 @@
 ler 
-m;%0D%0A%0D%0A%09%09
 m = 
@@ -2115,16 +2115,27 @@
 r();%0D%0A%09%09
+%0D%0A%09%09Object 
 model = 

load classes from an URL (e.g. a resource)