Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;

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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {

    public synchronized static Document loadDocument(File file)
            throws SAXException, IOException, ParserConfigurationException {
        if (file != null && file.exists()) {
            DocumentBuilder builder = createDocumentBuilder();
            if (builder != null) {
                return builder.parse(file);
            }
        }
        return null;
    }

    public synchronized static Document loadDocument(InputStream is)
            throws SAXException, IOException, ParserConfigurationException {
        if (is != null) {
            DocumentBuilder builder = createDocumentBuilder();
            if (builder != null) {
                return builder.parse(is);
            }
        }
        return null;
    }

    public synchronized static Document loadDocument(String data)
            throws SAXException, IOException, ParserConfigurationException {
        if (data != null && !data.isEmpty()) {
            return loadDocument(new ByteArrayInputStream(data.getBytes("UTF-8")));
        }
        return null;
    }

    public synchronized static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        return factory.newDocumentBuilder();
    }
}