package com.tvd12.ezyfox.mapping.jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class EzySimplXmlMapper implements EzyXmlMapper {

	private Unmarshaller unmarshaller;
	
	private  EzySimplXmlMapper(Builder builder) {
		this.unmarshaller = builder.newUnmarshaller();
	}
	
	@Override
	public <T> T read(File xmlFile, Class<T> outputType) {
		try {
			return unmarshaller.unmarshal(new StreamSource(xmlFile), outputType).getValue();
		} catch (JAXBException e) {
			throw new IllegalArgumentException("Can not read xml file " + xmlFile.getAbsolutePath() + " with " + outputType, e);
		}
	}
	
	public static Builder builder() {
		return new Builder();
	}

	public static class Builder {
		private String contextPath;
		private ClassLoader classLoader;
		
		public Builder contextClass(Class<?> clazz) {
		    this.classLoader = clazz.getClassLoader();
		    this.contextPath = clazz.getPackage().getName();
		    return this;
		}
		
		public Builder contextPath(String contextPath) {
			this.contextPath = contextPath;
			return this;
		}
		
		public Builder classLoader(ClassLoader classLoader) {
			this.classLoader = classLoader;
			return this;
		}
		
		protected Unmarshaller newUnmarshaller() {
			try {
				JAXBContext context = newJAXBContext();
				Unmarshaller unmarshaller = context.createUnmarshaller();
				return unmarshaller;
			} catch (JAXBException e) {
				throw new IllegalArgumentException(e);
			}
		}
		
		public EzySimplXmlMapper build() {
			return new EzySimplXmlMapper(this);
		}
		
		protected JAXBContext newJAXBContext() throws JAXBException {
			System.out.println("\n\n" + contextPath + "\n\n");
		    return JAXBContext.newInstance(contextPath, classLoader);
		}
	}
	
}

@@ -1712,62 +1712,8 @@
 n %7B%0A
-%09%09%09System.out.println(%22%5Cn%5Cn%22 + contextPath + %22%5Cn%5Cn%22);%0A
 %09%09  

update mapping