Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *  $Id$
 *
 *  This code is derived from public domain sources. Commercial use is allowed.
 *  However, all rights remain permanently assigned to the public domain.
 *
 *  XMLUtils.java : A class of static XML utility methods.
 *
 *  Copyright (C) 2009, 2010  Scott Herman
 *
 *  This is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This code is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this code.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

import java.net.URI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

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

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Reads the argument XML input stream.
     * @param inStream An input stream carrying the only subject XML
     * @return The root node of the read DOM document.
     */
    public static Node readXML(InputStream inStream) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document doc = builder.parse(inStream);
            inStream.close();
            return doc.getDocumentElement();
        } catch (SAXException saxEx) {
            throw new IllegalArgumentException(null, saxEx);
        } catch (IOException ioEx) {
            throw new IllegalArgumentException(null, ioEx);
        } catch (ParserConfigurationException parsEx) {
            throw new IllegalArgumentException(null, parsEx);
        } // try - catch
    }

    /**
     * Reads the argument XML input String.
     * @param xmlString The XML input String to be parsed.
     * @return The root node of the read DOM document.
     */
    public static Node readXML(String xmlString) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
            return doc.getDocumentElement();
        } catch (SAXException saxEx) {
            throw new IllegalArgumentException("readXML() Caught SAXException: ", saxEx);
        } catch (IOException ioEx) {
            throw new IllegalArgumentException("readXML() Caught IOException: " + ioEx.getMessage(), ioEx);
        } catch (ParserConfigurationException parsEx) {
            throw new IllegalArgumentException("readXML() Caught ParserConfigurationException: ", parsEx);
        } // try - catch
    }

    public static Node readXML(URI uri) {
        try {
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document doc = builder.parse(uri.toString());
            return doc.getDocumentElement();
        } catch (SAXException saxEx) {
            throw new IllegalArgumentException("readXML() Caught SAXException: ", saxEx);
        } catch (IOException ioEx) {
            throw new IllegalArgumentException("readXML() Caught IOException: " + ioEx.getMessage(), ioEx);
        } catch (ParserConfigurationException parsEx) {
            throw new IllegalArgumentException("readXML() Caught ParserConfigurationException: ", parsEx);
        } // try - catch
    }
}