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 void transform(InputStream xslis, InputStream xmlis, OutputStream xmlos) {
    try {//from  w  w w . jav a 2  s . com
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslis));
        transformer.transform(new StreamSource(xmlis), new StreamResult(xmlos));
        xslis.close();
        xmlis.close();
        xmlos.close();
    } catch (Exception e) {
        throw new RuntimeException("Fail to do XSLT transformation", e);
    }
}

From source file:Main.java

private static void doTransform(Source xslSource, Source xmlSource, Result xslResult) {
    try {//from w w w  .j  a va 2  s  .  com
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(xslSource);
        transformer.transform(xmlSource, xslResult);
    } catch (TransformerFactoryConfigurationError ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static Document xslt(InputStream stylesheet, Document input)
        throws FileNotFoundException, TransformerException, ParserConfigurationException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));
    Document result = newDocument();
    DOMResult domResult = new DOMResult(result);
    transformer.transform(new DOMSource(input), domResult);
    return result;
}

From source file:Main.java

/**
 * This method will apply an XSLT transformation
 * @param source the source reader/*from w w  w .  j a v a2 s. c om*/
 * @param result the target writter
 * @param style the stylesheet to be applied
 * @throws TransformerException
 */
static void transform(Reader source, Writer result, String style) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(style)));
    transformer.transform(new StreamSource(source), new StreamResult(result));
}

From source file:Main.java

/**
 * Performs XSL transformation.//from   w w w. j  a v  a 2  s . co  m
 * @param xmlDoc Document
 * @param xslDoc Document
 * @throws TransformerConfigurationException
 * @throws TransformerException
 * @return String The output text.
 */
public static String xslTransform(Document xmlDoc, Document xslDoc)
        throws TransformerConfigurationException, TransformerException {
    DOMSource source = new DOMSource(xmlDoc);
    DOMSource xslSrc = new DOMSource(xslDoc);

    TransformerFactory xformFactory = TransformerFactory.newInstance();
    Transformer transformer = xformFactory.newTransformer(xslSrc);

    java.io.StringWriter sw = new java.io.StringWriter();
    StreamResult outXml = new StreamResult(sw);

    transformer.transform(source, outXml);

    return sw.toString();
}

From source file:Main.java

public static void transform(InputStream doc, URL xsltUrl, OutputStream out)
        throws URISyntaxException, TransformerException {
    StreamSource source = new StreamSource(doc);
    StreamSource xslt = new StreamSource(new File(xsltUrl.toURI()));
    TransformerFactory fac = TransformerFactory.newInstance();
    Transformer t = fac.newTransformer(xslt);
    Result result = new StreamResult(out);
    t.transform(source, result);/*from  ww  w . j  a  v a2s. co  m*/
}

From source file:Main.java

/**
 * Converts an XML file to an XSL-FO file using JAXP (XSLT).
 * @param xml the XML file// w w  w  .j  av  a2  s.c om
 * @param xslt the stylesheet file
 * @param fo the target XSL-FO file
 * @throws IOException In case of an I/O problem
 * @throws TransformerException In case of a XSL transformation problem
 */
public static void convertXML2FO(InputStream xml, File xslt, File fo) {
    //Setup output
    OutputStream out = null;
    try {
        out = new java.io.FileOutputStream(fo);
        //Setup XSLT
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslt));

        //Setup input for XSLT transformation
        Source src = new StreamSource(xml);
        //Resulting SAX events (the generated FO) must be piped through to FOP
        Result res = new StreamResult(out);
        //Start XSLT transformation and FOP processing
        transformer.transform(src, res);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

/**
 * Transforms an xml-file (xmlFilename) using an xsl-file (xslFilename) and
 * writes the output into file (outputFilename).
 * /*from ww w .  j  a  v a2s  . co  m*/
 * @param xmlFile
 *            File: the xml-source-file to be transformed
 * @param xslFile
 *            File: the xsl-file with the transformation rules
 * @param outputFilename
 *            String: the name of the file the result will be written to
 */
public static void applyXSL(File xmlFile, File xslFile, String outputFilename) {
    try {
        // DocumentBuilderFactory docBFactory = DocumentBuilderFactory
        // .newInstance();
        // DocumentBuilder docBuilder = docBFactory.newDocumentBuilder();
        StreamSource xslStream = new StreamSource(xslFile);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer(xslStream);
        StreamSource xmlStream = new StreamSource(xmlFile);
        StreamResult result = new StreamResult(new BufferedWriter(new FileWriter(outputFilename)));
        transformer.transform(xmlStream, result);
        result.getWriter().close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

/**
 * /* ww  w.j  a  v a  2s . c om*/
 * <B>Purpose:</B> XML transformation using XSL
 * 
 * @param doc
 * @param xslInput
 * @param systemid
 * @return
 * @throws TransformerException
 */
public static Node transform(Document doc, StreamSource xslInput, String systemid) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(xslInput);
    DOMResult domResult = new DOMResult();
    DOMSource xmlDomSource = null;
    xmlDomSource = new DOMSource(doc);
    xmlDomSource.setSystemId(systemid);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
    transformer.transform(xmlDomSource, domResult);
    return domResult.getNode();
}

From source file:Main.java

public static void transformDocument(Document document, Writer out, File stylesheet)
        throws TransformerException {
    document.normalizeDocument();//from   w  ww  .  j a  v a2 s  .c om
    Transformer idTransform = null;
    TransformerFactory transFactory = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(stylesheet);
    idTransform = transFactory.newTransformer(stylesource);
    Source source = new DOMSource(document);
    Result result = new StreamResult(out);
    idTransform.transform(source, result);
}