Search in sources :

Example 81 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project poi by apache.

the class WordToHtmlConverter method main.

/**
     * Java main() interface to interact with {@link WordToHtmlConverter}<p>
     * 
     * Usage: WordToHtmlConverter infile outfile<p>
     * 
     * Where infile is an input .doc file ( Word 95-2007) which will be rendered
     * as HTML into outfile
     */
public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
    if (args.length < 2) {
        System.err.println("Usage: WordToHtmlConverter <inputFile.doc> <saveTo.html>");
        return;
    }
    System.out.println("Converting " + args[0]);
    System.out.println("Saving output to " + args[1]);
    Document doc = WordToHtmlConverter.process(new File(args[0]));
    DOMSource domSource = new DOMSource(doc);
    StreamResult streamResult = new StreamResult(new File(args[1]));
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    // TODO set encoding from a command argument
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "html");
    serializer.transform(domSource, streamResult);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Document(org.w3c.dom.Document) File(java.io.File)

Example 82 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project poi by apache.

the class WordToTextConverter method getText.

public String getText() throws Exception {
    StringWriter stringWriter = new StringWriter();
    DOMSource domSource = new DOMSource(getDocument());
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    // TODO set encoding from a command argument
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "no");
    serializer.setOutputProperty(OutputKeys.METHOD, "text");
    serializer.transform(domSource, streamResult);
    return stringWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult)

Example 83 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project poi by apache.

the class WordToFoConverter method main.

/**
     * Java main() interface to interact with {@link WordToFoConverter}
     * 
     * <p>
     * Usage: WordToFoConverter infile outfile
     * </p>
     * Where infile is an input .doc file ( Word 97-2007) which will be rendered
     * as XSL-FO into outfile
     */
public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        System.err.println("Usage: WordToFoConverter <inputFile.doc> <saveTo.fo>");
        return;
    }
    System.out.println("Converting " + args[0]);
    System.out.println("Saving output to " + args[1]);
    Document doc = WordToFoConverter.process(new File(args[0]));
    DOMSource domSource = new DOMSource(doc);
    StreamResult streamResult = new StreamResult(new File(args[1]));
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    // TODO set encoding from a command argument
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.transform(domSource, streamResult);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Document(org.w3c.dom.Document) File(java.io.File)

Example 84 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project poi by apache.

the class PkiTestUtils method toString.

static String toString(Node dom) throws TransformerException {
    Source source = new DOMSource(dom);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    /*
         * We have to omit the ?xml declaration if we want to embed the
         * document.
         */
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(source, result);
    return stringWriter.getBuffer().toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 85 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project cloudstack by apache.

the class PaloAltoResource method prettyFormat.

// pretty printing of xml strings
private String prettyFormat(String input) {
    int indent = 4;
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)257 Transformer (javax.xml.transform.Transformer)221 StreamResult (javax.xml.transform.stream.StreamResult)198 DOMSource (javax.xml.transform.dom.DOMSource)157 TransformerException (javax.xml.transform.TransformerException)86 StringWriter (java.io.StringWriter)77 StreamSource (javax.xml.transform.stream.StreamSource)77 Document (org.w3c.dom.Document)67 Source (javax.xml.transform.Source)56 IOException (java.io.IOException)55 File (java.io.File)47 DocumentBuilder (javax.xml.parsers.DocumentBuilder)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 Element (org.w3c.dom.Element)36 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35 Result (javax.xml.transform.Result)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 StringReader (java.io.StringReader)28