Example usage for javax.xml.transform TransformerFactory newTransformer

List of usage examples for javax.xml.transform TransformerFactory newTransformer

Introduction

In this page you can find the example usage for javax.xml.transform TransformerFactory newTransformer.

Prototype

public abstract Transformer newTransformer(Source source) throws TransformerConfigurationException;

Source Link

Document

Process the Source into a Transformer Object .

Usage

From source file:Main.java

/**
 * Use the transform specified by xslSrc and transform the document specified by docSrc, and return the resulting
 * document./*from w  w  w  . j a v a 2  s.  c om*/
 * 
 * @param xslSrc
 *          StreamSrc containing the xsl transform
 * @param docSrc
 *          StreamSrc containing the document to be transformed
 * @param params
 *          Map of properties to set on the transform
 * @param resolver
 *          URIResolver instance to resolve URI's in the output document.
 * 
 * @return StringBuffer containing the XML results of the transform
 * @throws TransformerConfigurationException
 *           if the TransformerFactory fails to create a Transformer.
 * @throws TransformerException
 *           if actual transform fails.
 */
protected static final StringBuffer transformXml(final StreamSource xslSrc, final StreamSource docSrc,
        final Map params, final URIResolver resolver)
        throws TransformerConfigurationException, TransformerException {

    StringBuffer sb = null;
    StringWriter writer = new StringWriter();

    TransformerFactory tf = TransformerFactory.newInstance();
    if (null != resolver) {
        tf.setURIResolver(resolver);
    }
    // TODO need to look into compiling the XSLs...
    Transformer t = tf.newTransformer(xslSrc); // can throw
    // TransformerConfigurationException
    // Start the transformation
    if (params != null) {
        Set<?> keys = params.keySet();
        Iterator<?> it = keys.iterator();
        String key, val;
        while (it.hasNext()) {
            key = (String) it.next();
            val = (String) params.get(key);
            if (val != null) {
                t.setParameter(key, val);
            }
        }
    }
    t.transform(docSrc, new StreamResult(writer)); // can throw
    // TransformerException
    sb = writer.getBuffer();

    return sb;
}

From source file:gr.abiss.calipso.util.XmlUtils.java

public static Document transform(Document source, Document stylesheet) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {/*from   w  w  w . j a v a2s  . c  om*/
        Transformer transformer = factory.newTransformer(new DocumentSource(stylesheet));
        DocumentResult result = new DocumentResult();
        transformer.transform(new DocumentSource(source), result);
        return result.getDocument();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.net2plan.utils.HTMLUtils.java

/**
 * Converts an XML file to a formatted HTML output via an XSLT definition.
 * //from ww w  .j a  v a 2 s .  c om
 * @param xml String containing an XML file
 * @param xsl URL containing an XSLT definition
 * @return Formatted HTML output
 */
public static String getHTMLFromXML(String xml, URL xsl) {
    try {
        Source xmlDoc = new StreamSource(new StringReader(xml));
        Source xslDoc = new StreamSource(xsl.openStream());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(xslDoc);
        transformer.transform(xmlDoc, new StreamResult(baos));
        String html = baos.toString(StandardCharsets.UTF_8.name());
        html = prepareImagePath(html, xsl);
        return html;
    } catch (IOException | TransformerFactoryConfigurationError | TransformerException e) {
        throw new RuntimeException(e);
    }
}