package Juno2.DataLoad;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public class CustomerDOMParser {

	////public static String OUTPUT_FILE = "customers.xml";

    private static TransformerFactory tf = TransformerFactory.newInstance();

    public static Document createDocument(CustomerInputSource inputSource)
		throws SAXException, IOException,
	    TransformerConfigurationException, TransformerException,
	    ParserConfigurationException {

		DOMResult dr = null;

		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			SAXSource ss = new SAXSource(new CustomerSAXParser(),inputSource);
			dr = new DOMResult(db.newDocument());
			Transformer t = tf.newTransformer();
			t.transform(ss,dr);

			////Serializer serializer = SerializerFactory.getSerializer
			////							(OutputProperties.getDefaultMethodProperties("xml"));
			////serializer.setOutputStream(new FileOutputStream(OUTPUT_FILE));
			////serializer.asDOMSerializer().serialize(dr.getNode());

			////System.out.println("Ouput is in " + OUTPUT_FILE);
		}

		catch(Exception exp)
		{
		   exp.printStackTrace();
		}

		return (Document) dr.getNode();
	}
}
