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.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

public class Main {

    public static Document load(InputStream stream) {
        try {
            BufferedInputStream bs = new BufferedInputStream(stream);
            bs.mark(1);

            if (-1 == bs.read())
                return null;

            bs.reset();
            return newDocumentBuilder().parse(bs);
        } catch (Exception e) {
            return null;
        }
    }

    public static Document load(String uri) {
        Document dom = null;
        try {
            DocumentBuilder db = newDocumentBuilder();
            dom = db.parse(uri);
        } catch (Exception e) {
        }
        return dom;
    }

    private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
        return newDocumentBuilderFactory().newDocumentBuilder();
    }

    private static DocumentBuilderFactory newDocumentBuilderFactory() {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setIgnoringComments(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setExpandEntityReferences(false);
        return dbf;
    }
}