Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;
import java.io.InputStream;

import java.io.StringReader;

public class Main {
    private static JAXBContext jaxbContext;
    private static ThreadLocal<Unmarshaller> unmarshaller = new ThreadLocal<Unmarshaller>();

    @SuppressWarnings("unchecked")
    public static <T> T unmarshal(Class<T> beanClass, String xml) throws JAXBException {
        StringReader reader = new StringReader(xml);
        return (T) getUnmarshaller().unmarshal(reader);
    }

    @SuppressWarnings("unchecked")
    public static <T> T unmarshal(Class<T> beanClass, InputStream is) throws JAXBException {
        return (T) getUnmarshaller().unmarshal(is);
    }

    private static Unmarshaller getUnmarshaller() throws JAXBException {
        Unmarshaller um = unmarshaller.get();
        if (um == null) {
            um = jaxbContext.createUnmarshaller();
            unmarshaller.set(um);
        }
        return um;
    }
}