package pt.link.utils.xml;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.UnmarshalListener;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

public class CastorUnmarshallerWithValidation extends CastorUnmarshaller {
static Log log = LogFactory.getLog(CastorUnmarshaller.class);
	
	static Mapping mapping;
	
	private static void init() throws XMLMarshallException {
		mapping = new Mapping(CastorUnmarshaller.class.getClassLoader());
		InputStream mappingIS = CastorUnmarshaller.class.getResourceAsStream("/castor-mapping.xml");
		if(mappingIS == null){
			String messageError = "castor mapping file not found"; 
			log.error(messageError);
			throw new XMLMarshallException(messageError);
		}
		try {
			mapping.loadMapping(new InputSource(mappingIS));
		} catch (Exception e) {
			String messageError = "Exception caught while initializing " + CastorUnmarshallerWithValidation.class.getName();
			log.error(messageError,e);
			throw new XMLMarshallException(messageError, e);
		}  
	}
	
	protected CastorUnmarshallerWithValidation() throws XMLMarshallException{
		init();
	}
	
	private static CastorUnmarshallerWithValidation _instance = null;
	public static CastorUnmarshaller getInstance() throws XMLMarshallException{
		if(_instance==null){
			_instance = new CastorUnmarshallerWithValidation();
		}
		return _instance;
	}

	/* (non-Javadoc)
	 * @see pt.link.utils.xml.CastorUnmarshaller#unmarshall(java.lang.Class, org.xml.sax.InputSource)
	 */
	public Object unmarshall(Class arg, InputSource xmlInputSource) throws XMLMarshallException {
		try {
			Unmarshaller unmarshaller = new Unmarshaller(arg, this.getClass().getClassLoader());
			unmarshaller.setMapping(mapping);
			UnmarshalListener validationListener = new CustomUnmarshalListener();
			
			unmarshaller.setUnmarshalListener(validationListener);
			Object unmarshalled = unmarshaller.unmarshal(xmlInputSource);
			return unmarshalled;
		}
		catch (Exception e) {
			String messageError = "Exception caught while unmarshalling" + this.getClass().getName();
			log.error(messageError, e);
			throw new XMLMarshallException(messageError, e);
		}  
	}
	

//	public static void main(String[] args) {
//		String response = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
//		response+="<M1101V01>";
//		response+="<C007>";response+="ola";response+="</C007>";
//		response+="</M1101V01>";
//		InputSource inputSource = new InputSource(new StringReader(response));
//		IXMLUnmarshaller unMarshaller;
//		try {
//			unMarshaller = CastorUnmarshallerWithValidation.getInstance();
//			Object unmarshalled = unMarshaller.unmarshall(AuthResponse_RAW.class, inputSource);
//			AuthResponse_RAW raw = (AuthResponse_RAW) unmarshalled;
//			System.out.println("c007: " + raw.getC007());
//		} catch (XMLMarshallException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//	}
	

}
