Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for xslt params in transform method #143

Open
jechaviz opened this issue Oct 27, 2016 · 3 comments
Open

Add support for xslt params in transform method #143

jechaviz opened this issue Oct 27, 2016 · 3 comments

Comments

@jechaviz
Copy link

jechaviz commented Oct 27, 2016

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;
    }
}
@lukaseder
Copy link
Member

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?

@jechaviz
Copy link
Author

jechaviz commented Nov 3, 2016

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

@lukaseder
Copy link
Member

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

@lukaseder lukaseder modified the milestones: Version 1.6.0, Version 1.7.0 Nov 30, 2017
@lukaseder lukaseder modified the milestones: Version 2.0.0, Version 2.1.0 Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants