Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import org.w3c.dom.Document;

public class Main {
    private static final String YES = "yes";
    private static final String NO = "no";
    private static final int INDENT_AMOUNT = 2;

    /**
     * Convert a Document into an input stream
     * 
     * @param document The source document
     * @param isFragment
     * @param indent If true, then the XML will be indented
     * @param charset Encoding, e.g. utf-8
     * @return XML document as InputStream
     */
    public static final java.io.InputStream getInputStream(Document document, boolean isFragment, boolean indent,
            String charset) throws Exception {
        java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream(); // INFO: Also see http://ostermiller.org/convert_java_outputstream_inputstream.html
        writeDocument(document, bout, indent);
        return new java.io.ByteArrayInputStream(bout.toByteArray());
    }

    /**
     * Write DOM document into output stream
     *
     * @param doc DOM document which will be written into OutputStream
     * @param out OutputStream into which the XML document is written
     */
    public static void writeDocument(Document doc, java.io.OutputStream out) throws Exception {
        writeDocument(doc, out, false);
    }

    /**
     * Write DOM document into output stream
     *
     * @param doc DOM document which will be written into OutputStream
     * @param out OutputStream into which the XML document is written
     * @param indent If true, then the XML will be indented
     */
    private static void writeDocument(Document doc, java.io.OutputStream out, boolean indent) throws Exception {
        if (doc == null) {
            throw new Exception("Document is null");
        } else if (out == null) {
            throw new Exception("OutputStream is null");
        } else {
            int indentationValue = 0;
            if (indent) {
                indentationValue = INDENT_AMOUNT;
            }
            Transformer t = getXMLidentityTransformer(indentationValue);
            t.transform(new javax.xml.transform.dom.DOMSource(doc),
                    new javax.xml.transform.stream.StreamResult(out));
            out.close();
        }
    }

    /**
     * @param indentation If greater than zero, then the XML will be indented by the value specified
     */
    private static Transformer getXMLidentityTransformer(int indentation) throws Exception {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer t = factory.newTransformer(); // identity transform
        t.setOutputProperty(OutputKeys.INDENT, (indentation != 0) ? YES : NO);
        t.setOutputProperty(OutputKeys.METHOD, "xml"); //xml, html, text
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + indentation);
        return t;
    }
}