package com.ejie.ab39a.utils;

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

/**
 * Utilidades para el manejo de XMLs
 * 
 *  
 * 
 */
public class UtilXML {

	private static final Logger logger = LoggerFactory.getLogger(UtilXML.class);

	/**
	 * 'UtilXML'
	 */
	private UtilXML() {
	}

	/**
	 * parse Document 2String
	 * 
	 * @param node
	 *            Document node
	 * @return String String
	 */
	public static String parseDocument2String(Document node) {
		UtilXML.logger.debug("INI parseDocumen2String");
		try {
			Source source = new DOMSource(node);
			StringWriter stringWriter = new StringWriter();
			Result result = new StreamResult(stringWriter);
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();
			transformer.transform(source, result);
			String resultado = stringWriter.getBuffer().toString();
			UtilXML.logger.debug("FIN parseDocument2String");
			return resultado;
		} catch (TransformerConfigurationException e) {
			UtilXML.logger.error("Se ha producido un error en el metodo parseDocumen2String: " + e.getMessage());
		} catch (TransformerException e) {
			UtilXML.logger.error("Se ha producido un error en el metodo parseDocumen2String: " + e.getMessage());
		}
		return null;
	}

	/**
	 * Obtiene el XML en formato String
	 * 
	 * @param node
	 *            Document node
	 * @return String String
	 */
	public static String document2String(Document node) {
		UtilXML.logger.trace("document2String INI");
		try {
			Source source = new DOMSource(node);
			StringWriter stringWriter = new StringWriter();
			Result result = new StreamResult(stringWriter);
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();
			transformer.transform(source, result);
			String resultado = stringWriter.getBuffer().toString();
			UtilXML.logger.trace("document2String FIN");
			return resultado;
		} catch (TransformerConfigurationException e) {
			UtilXML.logger.error("document2String", e);
		} catch (TransformerException e) {
			UtilXML.logger.error("document2String", e);
		}
		return null;
	}

	/**
	 * Obtiene un objeto Document(XMl) a partir de un InputStream
	 * 
	 * @param input
	 *            InputStream input
	 * @return Document el document
	 */
	public static Document inputStream2Document(InputStream input) {
		UtilXML.logger.trace("inputStream2Document INI");
		Document tmpX = null;
		DocumentBuilder builder = null;
		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			tmpX = builder.parse(input);
			tmpX.getDocumentElement().normalize();
		} catch (ParserConfigurationException e) {
			UtilXML.logger.error("inputStream2Document", e);
		} catch (SAXException e) {
			UtilXML.logger.error("inputStream2Document", e);
		} catch (IOException e) {
			UtilXML.logger.error("inputStream2Document", e);
		}

		UtilXML.logger.trace("inputStream2Document FIN");
		return tmpX;

	}

	/**
	 * Obtiene un Document a partir de una cadena
	 * 
	 * @param s
	 *            la cadena
	 * @return Document el document
	 */
	public static Document string2Document(String s) {
		UtilXML.logger.debug("INI string2Document");
		Document tmpX = null;
		DocumentBuilder builder = null;

		try {
			builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			tmpX = builder.parse(new ByteArrayInputStream(s.getBytes()));
		} catch (Exception e) {
			UtilXML.logger.error("Error al transformar el String a Document", e);
		}
		return tmpX;

	}

}