/*
 * 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.dgfip.xemelios.common;

import fr.gouv.finances.dgfip.utils.xml.transform.CustomURIResolver;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;

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.URIResolver;
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 = new net.sf.saxon.TransformerFactoryImpl();
        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 StreamSource(xmlStream), new StreamResult(outArray));
        }
        catch (TransformerException e) {
            logger.error(e);
            return null;
        }
        return new ByteArrayInputStream(outArray.toString(encoding).getBytes());
    }
    @Deprecated
    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 base) throws IOException {
    	return transform(xmlNode,xslStream,new Hashtable<String,Object>(),base);
    }
    public static InputStream transform(Node xmlNode, InputStream xslStream, Map<String,Object> parameters, String base) throws IOException {
        TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl();
        tFactory.setURIResolver(new CustomURIResolver(tFactory.getURIResolver(),base));
        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);
            e.printStackTrace();
            return null;
        }
        ByteArrayOutputStream outArray = new ByteArrayOutputStream();
        try {
            transformer.transform(new DOMSource(xmlNode), new StreamResult(outArray));
        }
        catch (TransformerException e) {
            logger.error(e);
            e.printStackTrace();
            return null;
        }

        return new ByteArrayInputStream(outArray.toByteArray());
    }

//    private static class MyURIResolver implements URIResolver {
//
//        private URIResolver _parent;
//        private String _base;
//
//        public MyURIResolver(URIResolver parent,String base) {
//            super();
//            this._parent=parent;
//            this._base=base;
//        }
//
//        public Source resolve(String href, String base) throws TransformerException {
//            Source src = null;
//            if(href.startsWith(".")) {
//                File f = new File(new File(_base),href);
//                if(f.exists()) src = new StreamSource(f);
//            }
//            if(src==null) {
//                src = _parent.resolve(href, _base);
//            }
//            logger.debug("resolve("+href+","+_base+") -> "+src);
//            return src;
//        }
//
//    }
}
