Transforms a DOM via a stylesheet, and returns the result in another DOM Document - Java XML

Java examples for XML:XSLT

Description

Transforms a DOM via a stylesheet, and returns the result in another DOM Document

Demo Code


import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main{
    /**************************************************************************
     ************                  Private Methods           ******************
     *************************************************************************/
    //    private static TransformerFactory transFact = TransformerFactory.newInstance();
    private static TransformerFactory transFact = TransformerFactory
            .newInstance();//  www  . ja va2 s  .c om
    private static Map<String, Templates> templates = new HashMap<String, Templates>();
    /** Transforms a DOM via a stylesheet, and returns the result in another DOM
     * Document
     * @param doc a DOM Document
     * @param stylesheet complete path to the stylesheet
     * @return transformed DOM
     */
    public static Document transformDomIntoDom(Document doc,
            String stylesheet) {
        return transformDomIntoDom(doc, stylesheet, null);
    }
    /** Transforms a DOM via a stylesheet, and returns the result in another DOM
     * Document
     * @param doc a DOM Document
     * @param stylesheet complete path to the stylesheet
     * @return transformed DOM
     */
    public static Document transformDomIntoDom(Document doc,
            String stylesheet, Map parameters) {
        try {
            //            TransformerFactory tFactory = TransformerFactory.newInstance();
            //            StreamSource stylesource = new StreamSource(stylesheet);            
            //            Transformer transformer = tFactory.newTransformer(stylesource);
            Transformer transformer = getTransformer(stylesheet);
            if (parameters != null) {
                for (Object o : parameters.keySet()) {
                    String k = (String) o;
                    transformer.setParameter(k, parameters.get(k));
                }
            }
            DOMSource source = new DOMSource(doc);
            DOMResult result = new DOMResult();
            transformer.transform(source, result);
            return (Document) result.getNode();
        } catch (TransformerConfigurationException tce) {
            Throwable x = tce;
            if (tce.getException() != null)
                x = tce.getException();
            throw new RuntimeException(stylesheet, x);
        } catch (TransformerException te) {
            Throwable x = te;
            if (te.getException() != null)
                x = te.getException();
            throw new RuntimeException(stylesheet, x);
        }
    }
    private static Transformer getTransformer(String xsltFile)
            throws TransformerConfigurationException {
        Templates tpl = (Templates) templates.get(xsltFile);
        if (tpl == null) {
            Source xsltSource = new StreamSource(xsltFile);
            tpl = transFact.newTemplates(xsltSource);
            if (tpl == null) {
                throw new RuntimeException("Stylesheet does not exist: "
                        + xsltFile);
            }
            templates.put(xsltFile, tpl);
        }
        return tpl.newTransformer();
    }
}

Related Tutorials