Java XML Format applyTransformation(InputStream xsltStream, Map xsltParameters, InputStream inputXmlStream, OutputStream outputStream)

Here you can find the source of applyTransformation(InputStream xsltStream, Map xsltParameters, InputStream inputXmlStream, OutputStream outputStream)

Description

Apply the XSLT transformation (xsltStream) to the xml given (inputXmlStream) and write the output to outputStream

License

LGPL

Parameter

Parameter Description
xsltStream a parameter
xsltParameters a parameter
inputXmlStream a parameter
outputStream a parameter

Exception

Parameter Description
TransformerException an exception

Return

the with the transformation output.

Declaration

public static OutputStream applyTransformation(InputStream xsltStream, Map<String, Object> xsltParameters,
        InputStream inputXmlStream, OutputStream outputStream) throws TransformerException 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;

import java.util.Map;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    /**/*from  w w w.  ja  v  a  2  s  .c om*/
     * Apply the XSLT transformation (<code>xsltStream</code>) to the xml given
     * (<code>inputXmlStream</code>) and write the output to
     * <code>outputStream</code>
     * 
     * @param xsltStream
     * @param xsltParameters
     * @param inputXmlStream
     * @param outputStream
     * @return the {@link OutputStream} with the transformation output.
     * @throws TransformerException
     */
    public static OutputStream applyTransformation(InputStream xsltStream, Map<String, Object> xsltParameters,
            InputStream inputXmlStream, OutputStream outputStream) throws TransformerException {

        return applyTransformation(new StreamSource(xsltStream), xsltParameters, inputXmlStream, outputStream);
    }

    /**
     * Apply the XSLT transformation (<code>xslt</code>) to the XML given (
     * <code>inputXmlStream</code>) and write the output to
     * <code>outputStream</code>
     * 
     * @param xslt
     * @param xsltParameters
     * @param inputXmlStream
     * @param outputStream
     * 
     * @return the {@link OutputStream} with the transformation output.
     * 
     * @throws TransformerException
     */
    public static OutputStream applyTransformation(String xslt, Map<String, Object> xsltParameters,
            InputStream inputXmlStream, OutputStream outputStream) throws TransformerException {

        return applyTransformation(new StreamSource(new StringReader(xslt)), xsltParameters, inputXmlStream,
                outputStream);
    }

    private static OutputStream applyTransformation(Source xsltSource, Map<String, Object> xsltParameters,
            InputStream inputXmlStream, OutputStream outputStream) throws TransformerException {

        // Create a transform factory instance.
        TransformerFactory tfactory = TransformerFactory.newInstance();

        // Create a transformer for the stylesheet.
        Transformer transformer = tfactory.newTransformer(xsltSource);

        if (xsltParameters != null) {
            for (String paramName : xsltParameters.keySet()) {
                transformer.setParameter(paramName, xsltParameters.get(paramName));
            }
        }

        // Transform the source XML to outputStream.
        transformer.transform(new StreamSource(inputXmlStream), new StreamResult(outputStream));

        return outputStream;
    }
}

Related

  1. format(final String xml)
  2. format(Node node)
  3. format(Node node, String indent)
  4. format(String unformattedXml)