Example usage for javax.xml.transform TransformerFactory newInstance

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

Introduction

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

Prototype

public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError 

Source Link

Document

Obtain a new instance of a TransformerFactory .

Usage

From source file:Main.java

public static void getWordDocument1(ByteArrayOutputStream recvstram, String filename, String xslfilename)
        throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError {

    try {/*from  ww  w  .  j a  v  a 2  s  .co m*/

        byte[] myBytes = recvstram.toByteArray();
        FileOutputStream out = new FileOutputStream("g:/input.txt");
        try {
            out.write(myBytes);
        } finally {
            out.close();
        }
        (TransformerFactory.newInstance().newTransformer(new StreamSource(new File("g:/" + xslfilename))))
                .transform(new StreamSource(new File("g:/input.txt")),
                        new StreamResult(new File("g:/channel.doc")));

    } catch (Exception e) {

    }

}

From source file:Main.java

/**
 * //from   w w  w.jav  a2s .  co m
 * @param fileName : output file
 * @param document :  xml document
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
private static void writeDocIntoFile(final String fileName, final Document document)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    // update the XML file
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    final Transformer transformer = transformerFactory.newTransformer();
    final DOMSource source = new DOMSource(document);
    final StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
}

From source file:Main.java

private static void transform(Document doc, Result result) throws Exception {
    try {//from w w  w  . j av a2  s  .com
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw e;
    } catch (TransformerException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }

}

From source file:Main.java

/**
 * @return a new XSLT transformer//from w  w  w  .  ja  v  a 2 s.  c o  m
 * @throws TransformerConfigurationException if no TransformerFactory can be located in the
 * runtime environment.
 */
public static Transformer getTransformer() throws TransformerConfigurationException {
    TransformerFactory tf;
    try {
        tf = TransformerFactory.newInstance();
    } catch (TransformerFactoryConfigurationError e) {
        System.setProperty("javax.xml.transform.TransformerFactory", TRANSFORMER_FACTORY_JDK5);
        tf = TransformerFactory.newInstance();
    }
    if (tf != null) {
        return tf.newTransformer();
    } else {
        throw new TransformerConfigurationException("Unable to instantiate a TransformerFactory");
    }
}

From source file:Main.java

/**
 * Writes the XML document to the particular file specified as argument
 * /*from  ww  w  .j a  v  a2  s .c o  m*/
 * @param doc the document
 * @param filename the path to the file in which to write the XML data writing
 *            operation fails
 */
public static void writeXMLDocument(Document doc, String filename) {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filename));
        transformer.transform(source, result);
        log.fine("writing operation to " + filename + " successful!");
    } catch (TransformerConfigurationException e) {
        log.warning(e.getMessage());
    } catch (TransformerException e) {
        log.warning(e.getMessage());
    }
}

From source file:Main.java

public static void saveDocumentToFile(Document xmlDoc, File file)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Converts an XML file to an XSL-FO file using JAXP (XSLT).
 * @param xml the XML file/*from   w  w w  .  ja va  2  s .  com*/
 * @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

/**
 * Creates a new document transformer/* w  w  w  . j  a va2 s .c  o m*/
 * @return the transformer
 */
public static Transformer createTransformer() throws TransformerConfigurationException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    return transformer;
}

From source file:Main.java

/**
 * Write a XML document to file//from   ww w.j a va  2  s  .c om
 * @param doc document to write
 * @param filename name of file to create
 */
public static void writeXmlFile(Document doc, String filename) {
    try {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File(filename);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
    }
}

From source file:Main.java

public static String parse(Document document) {
    try {/*  w ww  .  j a  v  a2  s .c  o  m*/
        TransformerFactory dbf = TransformerFactory.newInstance();
        Transformer t = dbf.newTransformer();
        t.setOutputProperty("encoding", "utf-8");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        t.transform(new DOMSource(document), new StreamResult(bos));
        return bos.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}