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

public static String transform(String xmlString, String stylesheetPathname) throws TransformerException {
    try {/*from w w w. j  a  v  a  2s .c  o m*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new TransformerException(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new TransformerException(te.getMessageAndLocation());
    }
}

From source file:Main.java

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));
        }//w w w.j a  v a 2s  .  c om
    }

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

    return outputStream;
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws Exception {
    try {/*from w  w w .  ja va  2s  .c om*/
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new Exception(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new Exception(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static void style(Reader xsl, Reader xml, Writer out, Map params) throws TransformerException {
    Source xmlSource = new javax.xml.transform.stream.StreamSource(xml);
    TransformerFactory factory = TransformerFactory.newInstance();
    Source xslSource = new javax.xml.transform.stream.StreamSource(xsl);
    Transformer transformer;/*from  ww  w.  java2  s.  com*/
    transformer = factory.newTransformer(xslSource);
    if (params != null && !params.isEmpty()) {
        Iterator entries = params.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            transformer.setParameter((String) entry.getKey(), entry.getValue());
        }
    }
    StreamResult result = new StreamResult(out);
    transformer.transform(xmlSource, result);
}

From source file:Main.java

/**
 * Converts a DOM document to an xml String.
 *
 * @param doc DOM document// ww  w  . j av  a 2 s  .com
 * @return xml String.
 * @throws TransformerException
 */
public static String getStringFromDomDocument(org.w3c.dom.Document doc, org.w3c.dom.Document xslt)
        throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    if (null != xslt) {
        DOMSource dxsltsource = new DOMSource(xslt);
        transformer = tf.newTransformer(dxsltsource);
    } else {
        transformer = tf.newTransformer();
    }
    transformer.transform(domSource, result);
    return writer.toString();
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL file, and stores it in a file/* w ww .  java2  s  . c  o m*/
 * 
 * @param doc
 *            The document to read
 * @param xslFile
 *            The name of the XSL file which allows transformate XML into
 *            String according its style
 * 
 * @return String value of a DOM
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String(Document doc, String xslFile)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

From source file:Main.java

/**
 * Transform input xml given a stylesheet.
 * //from  www . j av a2  s.  c o  m
 * @param styleSheet the style-sheet
 * @param xml input xml data
 * @param out output
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void transform(InputStream styleSheet, InputStream xml, Writer out)
        throws TransformerConfigurationException, TransformerException {
    // Instantiate a TransformerFactory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to process the  
    // stylesheet and generate a Transformer
    Transformer transformer = tFactory.newTransformer(new StreamSource(styleSheet));

    // Use the Transformer to transform an XML Source 
    // and send the output to a Result object.
    transformer.transform(new StreamSource(xml), new StreamResult(out));
}

From source file:Main.java

/**
 * Transforms a memory document with XML format into an string according an
 * XSL//from  w w w.j a  v a  2s .  co m
 * 
 * @param doc
 *            The document to read
 * @param iS_xsl
 *            An InputStream with the XSL which allows transformate XML into
 *            String
 * 
 * @return An String with the DOM transformated
 * 
 * @throws FileNotFoundException
 *             java.io.FileNotFoundException
 * @throws TransformerException
 *             javax.xml.transform.TransformerException
 */
public static String write_DOM_into_an_String_With_An_XSL_InputStream(Document doc, InputStream iS_xsl)
        throws FileNotFoundException, TransformerException {
    // An instance of a object transformer factory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Creates a transformer object associated to the XSL file
    Transformer transformer = tFactory.newTransformer(new StreamSource(iS_xsl));

    // Holds a tree with the information of a document in the form of a DOM
    // tree
    DOMSource sourceId = new DOMSource(doc);

    // Makes the transformation from the source in XML to the output in
    // stream according the transformer in XSL
    ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
    transformer.transform(sourceId, new StreamResult(bAOS));

    // Returns the string value of the stream
    return bAOS.toString();
}

From source file:Main.java

/**
 * This method performs XSL Transformation. <br>
 * <b>Deprecated use XmlTransformer.transform</b>
 * /*from  w w  w . j ava 2s  .c  o  m*/
 * @param source
 *            The input XML document
 * @param stylesheet
 *            The XSL stylesheet
 * @param params
 *            parameters to apply to the XSL Stylesheet
 * @param outputProperties
 *            properties to use for the xsl transform. Will overload the xsl output definition.
 * @return The output document transformed
 * @throws Exception
 *             The exception
 */
@Deprecated
public static String transform(Source source, Source stylesheet, Map<String, String> params,
        Properties outputProperties) throws Exception {
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(stylesheet);

        if (outputProperties != null) {
            transformer.setOutputProperties(outputProperties);
        }

        if (params != null) {
            transformer.clearParameters();

            for (Entry<String, String> entry : params.entrySet()) {
                String name = entry.getKey();
                String value = entry.getValue();
                transformer.setParameter(name, value);
            }
        }

        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        transformer.transform(source, result);

        return sw.toString();
    } catch (TransformerConfigurationException e) {
        String strMessage = e.getMessage();

        if (e.getLocationAsString() != null) {
            strMessage += ("- location : " + e.getLocationAsString());
        }

        throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause());
    } catch (TransformerFactoryConfigurationError e) {
        throw new Exception("Error transforming document XSLT : " + e.getMessage(), e);
    } catch (TransformerException e) {
        String strMessage = e.getMessage();

        if (e.getLocationAsString() != null) {
            strMessage += ("- location : " + e.getLocationAsString());
        }

        throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause());
    } catch (Exception e) {
        throw new Exception("Error transforming document XSLT : " + e.getMessage(), e);
    }
}

From source file:com.redhat.akashche.wixgen.cli.Launcher.java

private static void transformWithXsl(String inputPath, String xslPath, String outputPath) throws Exception {
    File file = new File(outputPath);
    if (file.exists())
        throw new IOException("Output file already exists: [" + outputPath + "]");
    TransformerFactory factory = TransformerFactory.newInstance();
    Source xsl = new StreamSource(new File(xslPath));
    Transformer transformer = factory.newTransformer(xsl);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    Source text = new StreamSource(new File(inputPath));
    transformer.transform(text, new StreamResult(new File(outputPath)));
}