package pt.link.utils.xml;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import pt.link.NotImplementedException;

public class CustomUnmarshaller implements IXMLUnmarshaller {
	
	static Log log = LogFactory.getLog(CustomUnmarshaller.class);

	/**
	 * TODO incompleto
	 * @throws XMLMarshallException 
	 */
	public Object unmarshall(Class arg0, InputSource arg1) throws XMLMarshallException{
		DocumentBuilderFactory builderFactory =
	        DocumentBuilderFactory.newInstance();
	      try {
			DocumentBuilder builder =
			    builderFactory.newDocumentBuilder();
			
			Document document = builder.parse(arg1);
			
			NodeList nodes = document.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) {
				Element element = (Element) nodes.item(i);
				//element.getN
			}
			
		} catch (Exception e) {
			String messageError = "Exception caught while unmarshalling" + this.getClass().getName();
			log.error(messageError, e);
			throw new XMLMarshallException(messageError, e);
		} 
	    
		throw new NotImplementedException();
	}
	
	public Object unmarshall(Class arg, Node node) throws XMLMarshallException {
		throw new NotImplementedException();
	}
	
	public static void main(String[] args) {
		String response = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
		response+="<M101V01>";
		response+="<C007>";response+="ola";response+="</C007>";
		response+="</M101V01>";
		InputSource inputSource = new InputSource(new StringReader(response));
		try {
			new CustomUnmarshaller().unmarshall(CustomUnmarshaller.class, inputSource);
		} catch (XMLMarshallException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}





}
