/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 L.Meckert - lmeckert@club-internet.fr
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;

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 javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.w3c.dom.Node;

//import fr.gouv.finances.cp.utils.xml.xpath.XPathExtensionHandler;

/**
 * 
 * 
 */
public class DematTransform {
    private static Logger logger = Logger.getLogger(DematTransform.class);

    public static InputStream transform(InputStream xmlStream, InputStream xslStream, String encoding, Map<String,Object> parameters) throws IOException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = tFactory.newTransformer(new StreamSource(xslStream));
            for(String key:parameters.keySet()) {
                transformer.setParameter(key,parameters.get(key));
            }
//            logger.debug(transformer.getClass().getName());
//            if(transformer instanceof TransformerImpl) {
//            	TransformerImpl impl = (TransformerImpl)transformer;
//            	impl.getExtensionsTable().addExtensionNamespace(XPathExtensionHandler.NAMESPACE_PREFIX,new XPathExtensionHandler());
//            }
        }
        catch (TransformerConfigurationException e) {
            logger.error(e);
            return null;
        }
        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
        try {
            transformer.transform(new StreamSource(xmlStream), new StreamResult(outArray));
        }
        catch (TransformerException e) {
            logger.error(e);
            return null;
        }
        return new ByteArrayInputStream(outArray.toString(encoding).getBytes());
    }
    public static InputStream transform(InputStream xmlStream, InputStream xslStream, String encoding) throws IOException {
        return transform(xmlStream, xslStream, encoding, new Hashtable<String,Object>());
    }
    public static InputStream transform(Node xmlNode, InputStream xslStream/*, String encoding*/) throws IOException {
//    	return transform(xmlNode,xslStream,encoding,new Hashtable<String,Object>());
    	return transform(xmlNode,xslStream,new Hashtable<String,Object>());
    }
    public static InputStream transform(Node xmlNode, InputStream xslStream/*, String encoding*/, Map<String,Object> parameters) throws IOException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = tFactory.newTransformer(new StreamSource(xslStream));
            for(String key:parameters.keySet()) {
                transformer.setParameter(key,parameters.get(key));
            }
        }
        catch (TransformerConfigurationException e) {
            logger.error(e);
            return null;
        }
        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
        try {
            transformer.transform(new DOMSource(xmlNode), new StreamResult(outArray));
        }
        catch (TransformerException e) {
            logger.error(e);
            return null;
        }

        //return new ByteArrayInputStream(outArray.toString(encoding).getBytes());
        return new ByteArrayInputStream(outArray.toByteArray());
    }
}
