jOOX icon indicating copy to clipboard operation
jOOX copied to clipboard

Add support for xslt params in transform method

Open jechaviz opened this issue 9 years ago • 3 comments

Currently we can do this: $(person).transform("say.xsl")

But if xslt has params, say.xsl contains <xsl:param name="word" value="hi"/>

We could do this: Map params=new HashMap(); params.put("word","hello"); $(person).transform("say.xsl",params);

At this moment, i have a verbose class to do this.

package com.mtm.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
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.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.FileUtils;
import org.joox.Match;
import static org.joox.JOOX.$;
import org.xml.sax.SAXException;

public class Xalan {
    private Map<String, String> params=new HashMap<String,String>();
    private File xsl;
    private StreamSource xslStreamSource;
    private boolean clearParams;
    private TransformerFactory transformerFactory; 
        private Transformer transformer;
        private OutputStream output = new ByteArrayOutputStream();
    public Xalan(String xslPath) {
        xsl=new File(xslPath);
    }
    public Xalan(File xsl) {
        this.xsl=xsl;
    }
    public Xalan(String xslPath,Map params) throws TransformerConfigurationException {
        xsl=new File(xslPath);
        this.params=params;
        setXsltProcessor();
    }
    public Xalan(File xsl,Map params) throws TransformerConfigurationException {
        this.xsl=xsl;
        this.params=params;
        setXsltProcessor();
    }
    public void setParams(Map params) throws TransformerConfigurationException {
        this.params=params;
        setXsltProcessor();
    }
    public void clearParams(boolean clearParams) {
        this.clearParams=clearParams;
    }
    public void setParam(String key, String value) {
        params.put(key, value);
    }
    public File transform(File xml, String outputFilePath) throws IOException, SAXException {
        File output=new File(outputFilePath);
        return transform(xml, output);
    }
    public File transform(File xml, File output) throws IOException, SAXException {
        String transformed=transform(xml);
        FileUtils.writeStringToFile(output, transformed, "UTF-8");
        return output;
    }
    public String transform(File xml) throws SAXException {
        StreamSource xmlStreamSource = new StreamSource(xml);
        return transform(xmlStreamSource);
    }
    public String transform(String xmlPath) throws SAXException{
        File xml=new File(xmlPath);
        StreamSource sourceXML = new StreamSource(xml);//new StringReader(xml), para xml string
        return transform(sourceXML);
    }
    public String transform(Match xmlMatch) {
        StreamSource sourceXML = new StreamSource(new StringReader($(xmlMatch).toString()));
        return transform(sourceXML);
    }
    public void setXsltProcessor() throws TransformerConfigurationException {
        transformerFactory= TransformerFactory.newInstance();
        transformer = transformerFactory.newTransformer(xslStreamSource);
        if (params!=null){
            for(Map.Entry<String, String> param:params.entrySet()) {
                transformer.setParameter(param.getKey(), param.getValue());
            }
            if(clearParams)
                params.clear();
        }
    }
    private String transform(StreamSource xmlStreamSource) {
        try {
            transformer.transform(xmlStreamSource, new StreamResult(output));
        } catch (TransformerException e) {

        }
        String transformedXml = null;
        try {
            transformedXml = ((ByteArrayOutputStream) output).toString("UTF-8");
        } catch (UnsupportedEncodingException e) {

        }
        return transformedXml;
    }
}

jechaviz avatar Oct 27 '16 19:10 jechaviz

That's an excellent idea. We already have this with xpath(String, Object...) as well. Would you be interested in having a stab at a pull request for this?

lukaseder avatar Oct 28 '16 13:10 lukaseder

Why not? It would be the first pull request of my life, i'll be glad to do it.

jechaviz avatar Nov 03 '16 00:11 jechaviz

Excellent, I'm flattered :) Let me know if you need any help

lukaseder avatar Nov 03 '16 09:11 lukaseder