jOOX
jOOX copied to clipboard
Add support for xslt params in transform method
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;
}
}
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?
Why not? It would be the first pull request of my life, i'll be glad to do it.
Excellent, I'm flattered :) Let me know if you need any help