Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Document;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;

public class Main {
    public static Document createDocument(String filePath)
            throws SAXException, ParserConfigurationException, IOException {
        File file = new File(filePath);
        return createDocument(file);
    }

    public static Document createDocument(File file)
            throws SAXException, ParserConfigurationException, IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        return createDocument(bis);
    }

    public static Document createDocument(InputStream inputStream)
            throws SAXException, ParserConfigurationException, IOException {
        return createDocument(createInputSource(inputStream));
    }

    public static Document createDocument(InputSource inputSource)
            throws SAXException, ParserConfigurationException, IOException {
        Document doc = null;
        DocumentBuilderFactory factory = createDocumentBuilderFactory();
        DocumentBuilder builder = createDocumentBuilder(factory);
        builder.setEntityResolver(new EntityResolver() {
            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        doc = builder.parse(inputSource);
        return doc;
    }

    public static InputSource createInputSource(InputStream inputStream) {
        return new InputSource(inputStream);
    }

    public static DocumentBuilderFactory createDocumentBuilderFactory() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        return factory;
    }

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