Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;

import java.io.IOException;

import java.io.Reader;
import java.io.StringReader;

import java.net.MalformedURLException;
import java.net.URL;

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

import org.w3c.dom.Document;

import org.w3c.dom.Element;

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

public class Main {
    public static Element loadDocument(File location) {
        Document doc = null;
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
            doc = parser.parse(location);
            Element root = doc.getDocumentElement();
            root.normalize();

            return root;
        } catch (SAXParseException err) {
            System.err.println("URLMappingsXmlDAO ** Parsing error, line " + err.getLineNumber() + ", uri "
                    + err.getSystemId());
            System.err.println("URLMappingsXmlDAO error: " + err.getMessage());
        } catch (SAXException e) {
            System.err.println("URLMappingsXmlDAO error: " + e);
        } catch (MalformedURLException mfx) {
            System.err.println("URLMappingsXmlDAO error: " + mfx);
        } catch (IOException e) {
            System.err.println("URLMappingsXmlDAO error: " + e);
        } catch (Exception pce) {
            System.err.println("URLMappingsXmlDAO error: " + pce);
        }
        return null;
    }

    public static Element loadDocument(String value, String type) {
        Document doc = null;
        InputSource xmlInp = null;
        try {
            if (type.equals("location")) {
                URL url = new URL(value);
                xmlInp = new InputSource(url.openStream());
            } else {
                xmlInp = new InputSource(new StringReader(value));
            }
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
            doc = parser.parse(xmlInp);
            Element root = doc.getDocumentElement();
            root.normalize();
            return root;
        } catch (SAXParseException err) {
            System.err.println("URLMappingsXmlDAO ** Parsing error, line " + err.getLineNumber() + ", uri "
                    + err.getSystemId());
            System.err.println("URLMappingsXmlDAO error: " + err.getMessage());
        } catch (SAXException e) {
            System.err.println("URLMappingsXmlDAO error: " + e);
        } catch (MalformedURLException mfx) {
            System.err.println("URLMappingsXmlDAO error: " + mfx);
        } catch (IOException e) {
            System.err.println("URLMappingsXmlDAO error: " + e);
        } catch (Exception pce) {
            System.err.println("URLMappingsXmlDAO error: " + pce);
        }
        return null;
    }

    public static Element loadDocument(Reader target) {
        Document doc = null;
        try {
            InputSource xmlInp = new InputSource(target);

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
            doc = parser.parse(xmlInp);
            Element root = doc.getDocumentElement();
            root.normalize();
            return root;
        } catch (SAXParseException err) {
            System.err.println("URLMappingsXmlDAO ** Parsing error, line " + err.getLineNumber() + ", uri "
                    + err.getSystemId());
            System.err.println("URLMappingsXmlDAO error: " + err.getMessage());
        } catch (SAXException e) {
            System.err.println("URLMappingsXmlDAO error: " + e);
        } catch (MalformedURLException mfx) {
            System.err.println("URLMappingsXmlDAO error: " + mfx);
        } catch (IOException e) {
            System.err.println("URLMappingsXmlDAO error: " + e);
        } catch (Exception pce) {
            System.err.println("URLMappingsXmlDAO error: " + pce);
        }
        return null;
    }
}