Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Main {
    /**
     * Liefert das {@link Document} aus dem Byte-Array.
     * 
     * @param bytes byte[]
     * @return {@link Document}
     * @throws Exception Falls was schief geht.
     */
    public static Document getDocument(final byte[] bytes) throws Exception {
        return getDocument(new InputSource(new ByteArrayInputStream(bytes)));
    }

    /**
     * Liefert das {@link Document} aus der {@link InputSource}.
     * 
     * @param inputSource {@link InputSource}
     * @return {@link Document}
     * @throws Exception Falls was schief geht.
     */
    public static Document getDocument(final InputSource inputSource) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);

        Document document = factory.newDocumentBuilder().parse(inputSource);

        return document;
    }

    /**
     * Liefert das {@link Document} aus dem Stream.
     * 
     * @param inputStream {@link InputStream}
     * @return {@link Document}
     * @throws Exception Falls was schief geht.
     */
    public static Document getDocument(final InputStream inputStream) throws Exception {
        InputStream is = inputStream;

        if (!(is instanceof BufferedInputStream)) {
            is = new BufferedInputStream(is, 8192 * 4);
        }

        return getDocument(new InputSource(is));
    }

    /**
     * Liefert das {@link Document} aus dem String.
     * 
     * @param string String
     * @return {@link Document}
     * @throws Exception Falls was schief geht.
     */
    public static Document getDocument(final String string) throws Exception {
        return getDocument(new InputSource(new StringReader(string)));
    }
}