Example usage for javax.xml.transform OutputKeys METHOD

List of usage examples for javax.xml.transform OutputKeys METHOD

Introduction

In this page you can find the example usage for javax.xml.transform OutputKeys METHOD.

Prototype

String METHOD

To view the source code for javax.xml.transform OutputKeys METHOD.

Click Source Link

Document

method = "xml" | "html" | "text" | expanded name.

Usage

From source file:Main.java

public static final void writeXml(Transformer transformer, OutputStream outputEntry, Document document) {
    Assert.notNull(transformer, "Transformer required");
    Assert.notNull(outputEntry, "Output entry required");
    Assert.notNull(document, "Document required");

    transformer.setOutputProperty(OutputKeys.METHOD, "xml");

    try {//  w  w w .  ja  va2  s .  c  o  m
        transformer.transform(new DOMSource(document), createUnixStreamResultForEntry(outputEntry));
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:Main.java

static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;/*w ww.  ja  va2s  . com*/
    try {
        t = tf.newTransformer();
        //SF:no dtd            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
    } catch (TransformerConfigurationException tce) {
        assert (false);
    }
    DOMSource doms = new DOMSource(doc);
    StreamResult sr = new StreamResult(os);
    try {
        t.transform(doms, sr);
    } catch (TransformerException te) {
        IOException ioe = new IOException();
        ioe.initCause(te);
        throw ioe;
    }
}

From source file:Main.java

static void emitDocument(Document doc, OutputStream os, String encoding) throws IOException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = null;//from  ww  w  .  j av  a2  s.  c  o m
    try {
        t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, PROPS_DTD_URI);
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);
    } catch (TransformerConfigurationException tce) {
        assert (false);
    }
    DOMSource doms = new DOMSource(doc);
    StreamResult sr = new StreamResult(os);
    try {
        t.transform(doms, sr);
    } catch (TransformerException te) {
        IOException ioe = new IOException();
        ioe.initCause(te);
        throw ioe;
    }
}

From source file:Main.java

public static String tryFormattingString(String s) {
    try {//from ww w .  j av  a  2 s  .c o  m
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

            @Override
            public void warning(SAXParseException exception) throws SAXException {
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
            }

            @Override
            public void error(SAXParseException exception) throws SAXException {
            }
        });
        Document doc = builder.parse(in);
        TransformerFactory tf = TransformerFactory.newInstance();
        // tf.setAttribute("indent-number", new Integer(2));
        Transformer trans = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(out);
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        trans.transform(source, result);
        return out.toString();

    } catch (Exception e) {
        // Console.println("Could not validate the soapmessage, although I'll show it anyway..");
    }
    return s;
}

From source file:Main.java

public static void printDocument(Document doc, OutputStream out, boolean prettyPrint)
        throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    if (prettyPrint) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    }//w w  w.  j a  v  a2 s. com

    transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

From source file:Main.java

/**
 * Executes a transformation./*from  w  w w. j a  va2 s  .  co  m*/
 * <br>The output encoding is set to UTF-8
 * @param source the transformation source
 * @param result the transformation result
 * @param indent if true, the output indent key is set to "yes"
 * @throws TransformerException if an exception occurs
 */
public static void transform(javax.xml.transform.Source source, javax.xml.transform.Result result,
        boolean indent) throws TransformerException {
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
    factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true);
    //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true); 
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING);
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    if (indent) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    }
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Write DOM to a file/*w w w  .  j a va  2s  .  co m*/
 *
 * @param doc
 *            DOM document
 * @param filename
 *            target file name
 * @param encoding
 *            specified encoding
 * @param omitXmlDeclaration
 *            flag to indicate if xml declaration statement is included
 * @throws Exception
 */
public static void writeXmlFile(Document doc, String filename, String encoding, String omitXmlDeclaration)
        throws Exception {

    // Prepare the DOM document for writing
    Source source = new DOMSource(doc);

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

    // Write the DOM document to the file
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration);

    transformer.transform(source, result);

    outputStream.flush();
    outputStream.close();
}

From source file:Main.java

/**
 * Method to convert document to string.
 * /*from  w  w  w  .java  2s  . c om*/
 * @param doc
 * @return document content as string
 */
public static String doc2String(Document doc) {
    try {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Error converting to String", ex);
    }
}

From source file:Main.java

public static void saveXml(Document dom, File file, boolean omitXmlDeclaration) throws IOException {
    try {//from ww w  .  j  a  v  a  2 s .  c  o m
        Transformer transformer = getNewTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
        transformer.transform(new DOMSource(dom.getDocumentElement()),
                new StreamResult(file.toURI().getPath()));
    } catch (Exception e) {
        throw new IOException("saveXml failed because : " + e.getMessage());
    }
}

From source file:Main.java

/**
 * Creates a {@link TransformerHandler}.
 * //from w ww.jav  a 2 s .  c  o m
 * @param commentHeader the comment header
 * @param rootTag the root tag
 * @param streamResult stream result
 */
public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag,
        StreamResult streamResult, Charset charset)
        throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException {
    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
    try {
        tf.setAttribute("indent-number", new Integer(2));
    } catch (Exception e) {
        // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510
    }
    TransformerHandler transformerHandler = tf.newTransformerHandler();
    Transformer serializer = transformerHandler.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, charset.name());
    serializer.setOutputProperty(OutputKeys.METHOD, "xml");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformerHandler.setResult(streamResult);
    transformerHandler.startDocument();
    String newline = System.getProperty("line.separator");
    if (newline == null) {
        newline = "\n";
    }
    commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " ");
    transformerHandler.characters("\n".toCharArray(), 0, 1);
    transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length);
    transformerHandler.characters("\n".toCharArray(), 0, 1);
    if (rootTag.length() > 0) {
        transformerHandler.startElement("", "", rootTag, null);
    }
    return transformerHandler;
}