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.FileInputStream;
import java.io.IOException;

import java.nio.charset.Charset;

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

import org.w3c.dom.Document;

import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class Main {
    private final static String USER_DIR_KEY = "user.dir";

    public static void validateDTD(String xmlFile) throws ParserConfigurationException, SAXException, IOException {

        validateDTD0(xmlFile, null);
    }

    public static void validateDTD(String xmlFile, String dtdPath)
            throws ParserConfigurationException, SAXException, IOException {

        String userDir = System.getProperty(USER_DIR_KEY);

        try {
            File dtdPathFile = new File(dtdPath);
            if (dtdPathFile.isDirectory()) {
                System.setProperty(USER_DIR_KEY, dtdPath);
                validateDTD0(xmlFile, null);
            } else {
                validateDTD0(xmlFile, dtdPath);
            }
        } finally {
            System.setProperty(USER_DIR_KEY, userDir);
        }
    }

    private static void validateDTD0(String xmlFile, final String dtdPath)
            throws ParserConfigurationException, SAXException, IOException {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        if (dtdPath != null && !dtdPath.isEmpty()) {
            documentBuilder.setEntityResolver(new EntityResolver() {

                public InputSource resolveEntity(String publicId, String systemId)
                        throws SAXException, IOException {

                    return new InputSource(new FileInputStream(dtdPath));
                }
            });
        }
        documentBuilder.setErrorHandler(new ErrorHandler() {

            public void warning(SAXParseException exception) throws SAXException {

            }

            public void fatalError(SAXParseException exception) throws SAXException {

                throw exception;
            }

            public void error(SAXParseException exception) throws SAXException {

                throw exception;
            }
        });

        documentBuilder.parse(new FileInputStream(xmlFile));

    }

    /**
     *
     * @param xmlContent
     * @param charset
     * @return
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    private static Document parse(String xmlContent, Charset charset)
            throws SAXException, IOException, ParserConfigurationException {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(false);
        documentBuilderFactory.setValidating(false);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset)));

        return document;
    }
}