Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.Node;
import javax.xml.bind.*;
import java.io.*;
import java.net.URL;
import java.util.Hashtable;

public class Main {
    private static Hashtable<String, JAXBContext> contextbuf = new Hashtable<String, JAXBContext>();

    public static Object importXmlJAXB(Class<?> clazz, File file) throws JAXBException {
        return importXmlJAXB(new Class<?>[] { clazz }, file);
    }

    public static Object importXmlJAXB(Class<?> clazz, Node node) throws JAXBException {
        return importXmlJAXB(new Class<?>[] { clazz }, node);
    }

    public static Object importXmlJAXB(Class<?> clazz, String filename) throws JAXBException {
        return importXmlJAXB(clazz, new File(filename));
    }

    public static Object importXmlJAXB(Class<?>[] clazz, File file) throws JAXBException {
        JAXBContext jc = getJAXBContext(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return u.unmarshal(file);
    }

    public static Object importXmlJAXB(Class<?>[] clazz, Node node) throws JAXBException {
        long start = System.currentTimeMillis();
        try {
            JAXBContext jc = getJAXBContext(clazz);
            Unmarshaller u = jc.createUnmarshaller();
            return u.unmarshal(node);
        } catch (JAXBException ex) {
            throw ex;
        } finally {

        }

    }

    public static Object importXmlJAXB(Class<?>[] clazz, String filename) throws JAXBException {
        return importXmlJAXB(clazz, new File(filename));
    }

    public static Object importXmlJAXB(Class<?>[] clazz, URL url) throws JAXBException {
        JAXBContext jc = getJAXBContext(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return u.unmarshal(url);
    }

    public static Object importXmlJAXB(Class<?> clazz, URL url) throws JAXBException {
        return importXmlJAXB(new Class<?>[] { clazz }, url);
    }

    public static Object importXmlJAXB(Class<?>[] clazz, InputStream in) throws JAXBException {
        JAXBContext jc = getJAXBContext(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return u.unmarshal(in);
    }

    public static Object importXmlJAXB(Class<?> clazz, InputStream in) throws JAXBException {
        return importXmlJAXB(new Class<?>[] { clazz }, in);
    }

    public static Object importXmlJAXB(Class<?>[] clazz, Reader in) throws JAXBException {
        JAXBContext jc = getJAXBContext(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return u.unmarshal(in);
    }

    public static Object importXmlJAXB(Class<?> clazz, Reader in) throws JAXBException {
        return importXmlJAXB(new Class<?>[] { clazz }, in);
    }

    private static JAXBContext getJAXBContext(Class<?>[] clazzs) throws JAXBException {
        if (clazzs != null) {
            StringBuilder sb = new StringBuilder();
            for (Class<?> c : clazzs) {
                sb.append(c.getName()).append(",");
            }
            String str = sb.toString();
            if (contextbuf.containsKey(str)) {
                return contextbuf.get(str);
            } else {
                JAXBContext c = JAXBContext.newInstance(clazzs);
                contextbuf.put(str, c);
                return c;
            }
        } else {
            throw new JAXBException(new NullPointerException("Class"));
        }

    }
}