package test.com;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.util.Properties;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.OutputKeys;
import java.io.*;
import java.util.*;
/**
 * This class is used to perform the transformation from XML file to TEXT (Del) file using XSL. 
 * 
 */
public class TranformXML {

	public static void main(String args[]) {

		try {

			long startTime = System.currentTimeMillis();

			System.out.println("Transformation Process is Started :");

			String xmlFile = "c:\\data.xml";
			String xslFile = "c:\\data.xsl";
			String xltFile = "c:\\data.txt";

			// check the file existance
			File f = new File(xmlFile);

			if (!f.exists()) {
				System.out.println("XML File does not exist : " + xmlFile);
				System.exit(0);
			}

			f = new File(xslFile);

			if (!f.exists()) {
				System.out.println("XSL File does not exist : " + xslFile);
				System.exit(0);
			}

			// create a transform factory instance
			TransformerFactory tfactory = TransformerFactory.newInstance();

			// create template instance
			Templates templates =
				(Templates) tfactory.newTemplates(new StreamSource(xslFile));

			//maintain XML identation
			Properties oprops = templates.getOutputProperties();
			oprops.put(OutputKeys.INDENT, "yes");

			//create a tarnsformer object  
			Transformer transformer = templates.newTransformer();
			transformer.setOutputProperties(oprops);

			ByteArrayOutputStream ba = new ByteArrayOutputStream();
			String encoding = "ISO-8859-1";
			// set encoding
			OutputStreamWriter output = new OutputStreamWriter(ba, encoding);

			// seting up errorlistener to transformer object
			XMLErrorChecker errors = new XMLErrorChecker();
			transformer.setErrorListener(errors);

			//We can also use XML String or XSL String incase of file: transformer.transform(new StreamSource(new StringReader(xmlFile)),new StreamResult(output));
			transformer.transform(
				new StreamSource(xmlFile),
				new StreamResult(output));

			System.out.println("Output Result:");
			System.out.println(ba.toString(encoding));
			// creating xslt File 

			FileWriter fw = new FileWriter(xltFile);
			BufferedWriter bw = new BufferedWriter(fw);

			bw.write(ba.toString(encoding));

			// closing file            
			bw.close();
			fw.close();

			long endTime = System.currentTimeMillis();
			System.out.println("XSLT File is created: " + xltFile);
			System.out.println(
				"Total Time Taken (in Sec) to Complte the Transformation : "
					+ (double) ((endTime - startTime) / 1000.0));

		} catch (TransformerException te) {
			System.out.println("TransformerException");
			te.printStackTrace(System.out);
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}

	}

}
