Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;

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 {
    /**
     * Parses the specified XML file and returns it as a {@link Document}.
     *
     * @param xmlFilePath
     *            The path to the file to parse.
     * @return The {@code Document} created from the file.
     * @throws SAXException
     *             If any parse error occurs.
     * @throws IOException
     *             If any IO error occurs.
     */
    public static Document getDomDocumentFromFile(String xmlFilePath) throws SAXException, IOException {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            return documentBuilder.parse(fixUri(xmlFilePath));
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("Internal error: ParserConfigurationException: " + e.getMessage());
        }
    }

    /**
     * Fix problems in the URIs (spaces for instance).
     *
     * @param uri
     *            The original URI.
     * @return The corrected URI.
     */
    private static String fixUri(String uri) {
        // handle platform dependent strings
        String path = uri.replace(java.io.File.separatorChar, '/');
        // Windows fix
        if (path.length() >= 2) {
            final char ch1 = path.charAt(1);
            // change "C:blah" to "/C:blah"
            if (ch1 == ':') {
                final char ch0 = Character.toUpperCase(path.charAt(0));
                if (ch0 >= 'A' && ch0 <= 'Z') {
                    path = "/" + path;
                }
            }
            // change "//blah" to "file://blah"
            else if (ch1 == '/' && path.charAt(0) == '/') {
                path = "file:" + path;
            }
        }
        // replace spaces in file names with %20.
        // Original comment from JDK5: the following algorithm might not be
        // very performant, but people who want to use invalid URI's have to
        // pay the price.
        final int pos = path.indexOf(' ');
        if (pos >= 0) {
            final StringBuilder sb = new StringBuilder(path.length());
            // put characters before ' ' into the string builder
            for (int i = 0; i < pos; i++) {
                sb.append(path.charAt(i));
            }
            // and %20 for the space
            sb.append("%20");
            // for the remaining part, also convert ' ' to "%20".
            for (int i = pos + 1; i < path.length(); i++) {
                if (path.charAt(i) == ' ') {
                    sb.append("%20");
                } else {
                    sb.append(path.charAt(i));
                }
            }
            return sb.toString();
        }
        return path;
    }
}