Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright 2006 OCLC Online Computer Library Center Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or
 * agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

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.InputSource;
import org.xml.sax.SAXException;

public class Main {
    private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    /**
     * Grab the Document found at the specified URL.
     * 
     * @param ref the URL location of an XML document.
     * @return a Document loaded from the specified URL.
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final String ref) throws SAXException, IOException, ParserConfigurationException {
        final String protocol = ref.split(":", 2)[0];
        return parse(protocol, new InputSource(ref));
    }

    /**
     * Grab the Document found at the specified URL.
     * 
     * @param protocol
     * @param is
     * @return a Document loaded from the specified URL.
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final String protocol, final InputSource is)
            throws SAXException, IOException, ParserConfigurationException {
        if ("http".equals(protocol)) {
            return getThreadedDocumentBuilder().parse(is);
        }
        throw new IOException("Protocol handler not implemented yet: " + protocol);
    }

    /**
     * Grab the Document loaded from the specified InputSource.
     * 
     * @param is
     * @return a Document loaded from the specified InputSource
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final InputSource is)
            throws SAXException, IOException, ParserConfigurationException {
        return getThreadedDocumentBuilder().parse(is);
    }

    /**
     * Grab the Document loaded from the specified InputStream
     * 
     * @param is
     * @return a Document loaded from the specified InputStream.
     * @throws SAXException
     * @throws IOException
     * @throws ParserConfigurationException
     */
    public static Document parse(final InputStream is)
            throws SAXException, IOException, ParserConfigurationException {
        return getThreadedDocumentBuilder().parse(is);
    }

    /**
     * Get a thread-safe DocumentBuilder
     * 
     * @return a namespaceAware DocumentBuilder assigned to the current thread
     * @throws ParserConfigurationException
     */
    public static DocumentBuilder getThreadedDocumentBuilder() throws ParserConfigurationException {
        // Thread currentThread = Thread.currentThread();
        // DocumentBuilder builder =
        // (DocumentBuilder) builderMap.get(currentThread);
        // if (builder == null) {
        // builder = dbFactory.newDocumentBuilder();
        // builderMap.put(currentThread, builder);
        // }
        final DocumentBuilder builder = dbFactory.newDocumentBuilder();
        return builder;
    }
}