Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.StringReader;

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 {
    static TransformerFactory factory = TransformerFactory.newInstance();

    public static String transform(String xslt, String xml, String charsetName) throws TransformerException {
        return transform(new StreamSource(new StringReader(xslt)), new StreamSource(new StringReader(xml)),
                charsetName);
    }

    public static String transform(InputStream xsltStream, InputStream xmlStream, String charset)
            throws TransformerException {
        return transform(new StreamSource(xsltStream), new StreamSource(xmlStream), charset);
    }

    public static String transform(Source xsltStream, Source xmlStream, String charset)
            throws TransformerException {
        try {

            Transformer transformer = factory.newTransformer(xsltStream);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            transformer.transform(xmlStream, new StreamResult(outputStream));
            transformer = null;
            xmlStream = null;
            return outputStream.toString(charset);
        } catch (Exception e) {
            throw new TransformerException(e);
        }
    }
}