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

/**
 * Transforms a memory document with XML format into an string according an
 * XSL//from  w w  w  .j  ava2s .  c o m
 * 
 * @param doc
 *            The document to read
 * @param xsl
 *            An string 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_String(Document doc, String 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(new ByteArrayInputStream(xsl.getBytes())));

    // 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

private static Transformer transformer(Source xsltSource) throws TransformerConfigurationException {
    TransformerFactory tf = TRANSFORMER_FACTORY.get();
    if (tf == null) {
        tf = TransformerFactory.newInstance();
        TRANSFORMER_FACTORY.set(tf);//from  w  w  w. j av  a  2 s . c  o  m
    }
    return tf.newTransformer(xsltSource);
}

From source file:Main.java

public static void transformDocument(Document doc, String xsl, String targetFile) {
    try {//from  w  w  w  . j  a v a 2s .c o  m
        Source source = new DOMSource(doc);

        // Creation of the output file
        File file = new File(targetFile);
        Result result = new StreamResult(file);

        // configuration of the transformer
        TransformerFactory factoryT = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 *
 * @param source//from   ww  w  . jav a 2  s .  co  m
 * @param xsltSource
 * @return
 * @throws TransformerConfigurationException
 * @throws JAXBException
 * @throws TransformerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static synchronized Document deserializeToDom(InputStream source, InputStream xsltSource)
        throws TransformerConfigurationException, JAXBException, TransformerException,
        ParserConfigurationException, SAXException, IOException {
    Document obj;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xsltSource));
        obj = dbf.newDocumentBuilder().newDocument();
        Result result = new DOMResult(obj);
        transformer.transform(new StreamSource(source), result);
    } else {
        obj = dbf.newDocumentBuilder().parse(source);

    }
    return obj;
}

From source file:Main.java

public static void saveDocument(Document doc, File transform, File file) throws IOException {
    try {// w  ww .  ja  v  a2  s  .  c om
        TransformerFactory tFactory = TransformerFactory.newInstance();
        tFactory.setAttribute("indent-number", new Integer(2));
        Transformer transformer = tFactory.newTransformer(new StreamSource(transform));
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        FileOutputStream fos = new FileOutputStream(file.getPath());

        DOMSource source = new DOMSource(doc);
        CharsetEncoder utf8Encoder = Charset.forName("UTF-8").newEncoder();
        StreamResult result = new StreamResult(new OutputStreamWriter(fos, utf8Encoder));

        transformer.transform(source, result);
        fos.close();
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static void transformDocument(final Document doc, final String xsl, final String targetFile) {
    try {// w w w  .j  a  v a  2s  . c  om
        final Source source = new DOMSource(doc);

        // Creation of the output file
        final File file = new File(targetFile);
        final Result result = new StreamResult(file);

        // configuration of the transformer
        final TransformerFactory factoryT = TransformerFactory.newInstance();
        final StreamSource stylesource = new StreamSource(xsl);
        Transformer transformer;

        transformer = factoryT.newTransformer(stylesource);
        transformer.setOutputProperty(OutputKeys.METHOD, "text");

        // Transformation
        transformer.transform(source, result);
    } catch (final TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (final TransformerException e) {
        e.printStackTrace();
    }

}

From source file:info.mikaelsvensson.devtools.report.xslt.XSLTReport.java

private static void writeFile(Document input, File output, Source xsltSource, Map<String, String> parameters)
        throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xsltSource);
    StreamResult outputTarget = new StreamResult(output);

    Source xmlSource = new DOMSource(input);
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        transformer.setParameter(entry.getKey(), entry.getValue());
    }/*from w  w w . j  a v a  2 s.  com*/

    transformer.setParameter("outputFile", output.getName());
    transformer.transform(xmlSource, outputTarget);
}

From source file:Main.java

/**
 *
 * @param elmnt//from w  w  w.  j  a v  a2  s.  c om
 * @param xsltSource
 * @param cls
 * @return
 * @throws TransformerConfigurationException
 * @throws JAXBException
 * @throws TransformerException
 */
public static synchronized Object deserialize(Element elmnt, InputStream xsltSource, Class cls)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    Object obj = null;
    JAXBContext jc = JAXBContext.newInstance(cls);

    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = factory.newTransformer(new StreamSource(xsltSource));

        JAXBResult result = new JAXBResult(jc);
        transformer.transform(new DOMSource(elmnt), result);
        obj = result.getResult();
    } else {
        obj = jc.createUnmarshaller().unmarshal(elmnt);
    }
    return obj;
}

From source file:Main.java

/**
 *
 * @param source/*from   w w w . j a  v a  2 s  .  c o m*/
 * @param xsltSource
 * @param cls
 * @return
 * @throws TransformerConfigurationException
 * @throws JAXBException
 * @throws TransformerException
 */
public static synchronized Object deserialize(InputStream source, InputStream xsltSource, Class cls)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    Object obj = null;
    JAXBContext jc = JAXBContext.newInstance(cls);

    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = factory.newTransformer(new StreamSource(xsltSource));

        JAXBResult result = new JAXBResult(jc);
        transformer.transform(new StreamSource(source), result);
        obj = result.getResult();
    } else {
        obj = jc.createUnmarshaller().unmarshal(source);
    }
    return obj;
}

From source file:net.dinkla.mof2ecore.MOF2Ecore.java

/**
 * /*from w w w.  j av a  2s.co  m*/
 * @param fileXSL
 * @param fileInput
 * @param fileOutput
 */
public static void mof2ecore(String fileXSL, String fileInput, String fileOutput) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    try {
        System.out.println("Processing " + fileInput);
        Transformer transformer = tFactory.newTransformer(new StreamSource(fileXSL));
        transformer.setParameter("ecore_toplevel_package", namePackage);
        transformer.setParameter("ecore_container_suffix", suffixContainer);

        // TODO remove broken files and throw error
        transformer.transform(new StreamSource(fileInput), new StreamResult(new FileOutputStream(fileOutput)));
        System.out.println("Finished processing " + fileInput);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
}