Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import javax.xml.bind.*;

import java.io.InputStream;

public class Main {
    public static <T> T parse(final Class<T> clazz, final InputStream inputStream) {
        try {
            final JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            final Object deserialized = jaxbUnmarshaller.unmarshal(inputStream);
            if (clazz.isAssignableFrom(deserialized.getClass())) {
                return clazz.cast(deserialized);
            } else {
                final JAXBElement<T> jaxbElement = (JAXBElement<T>) deserialized;
                return jaxbElement.getValue();
            }
        } catch (final JAXBException e) {
            throw new RuntimeException(e);
        }
    }
}