Java XML Transform transform(final Source xml, final Source xslt, final Map params, final Writer err)

Here you can find the source of transform(final Source xml, final Source xslt, final Map params, final Writer err)

Description

transform

License

Open Source License

Declaration

public static String transform(final Source xml, final Source xslt, final Map params, final Writer err)
            throws TransformerException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.transform.ErrorListener;
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;

public class Main {
    public static String transform(final Source xml, final Source xslt, final Map params, final Writer err)
            throws TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setErrorListener(new ErrorListener() {
            public void warning(final TransformerException e) throws TransformerException {
                append(e.getMessageAndLocation());
            }/* ww w. j  av a  2  s. co  m*/

            public void error(final TransformerException e) throws TransformerException {
                append(e.getMessageAndLocation());
            }

            public void fatalError(final TransformerException e) throws TransformerException {
                append(e.getMessageAndLocation());
            }

            private void append(final String msg) {
                if (err != null) {
                    try {
                        err.append(msg + "\n");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Transformer transform = factory.newTransformer(xslt);

        if (params != null) {
            for (Iterator iter = params.entrySet().iterator(); iter.hasNext();) {
                Entry param = (Entry) iter.next();
                transform.setParameter((String) param.getKey(), param.getValue());
            }
        }

        StreamResult result = new StreamResult(new StringWriter());
        transform.transform(xml, result);

        return result.getWriter().toString();
    }
}

Related

  1. transform(byte[] doc, URL xsltUrl, OutputStream out)
  2. transform(byte[] source, InputStream xslInputStream, File output)
  3. transform(File inputFile, Transformer transformer, Writer out)
  4. transform(File xslt_source, File xml_source)
  5. transform(final File sourceXML, final File xslt, final File targetXML)
  6. transform(InputStream styleSheet, InputStream xml, Writer out)
  7. transform(InputStream styleSheet, InputStream xml, Writer out)
  8. transform(InputStream xmlStream, InputStream xslStream, OutputStream outputStream)
  9. transform(InputStream xslis, InputStream xmlis, OutputStream xmlos)