Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//* Licensed Materials - Property of IBM                              *

import java.util.List;

import javax.xml.bind.JAXBElement;

public class Main {
    /**
     * Given an object that is presumably a JAXBElement<clazz>, return object.getValue(). This method
     * is useful for parsing XML elements of type xs:any (but where you know the type is clazz).
     * 
     * If object is not a JAXBElement<clazz>, return null else return
     * ((JAXBElement<?>)object).getValue()
     * 
     * @param jaxbElement
     * @param clazz
     * @return
     */
    public static Object unwrap(Object jaxbElement, Class<?> clazz) {
        if (jaxbElement instanceof JAXBElement<?>) {
            Object ret = ((JAXBElement<?>) jaxbElement).getValue();
            if (clazz.isInstance(ret)) {
                return ret;
            } else {
                System.err.println("Cannot cast " + ret + " to class " + clazz + " (actual class is " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        + ret.getClass() + ")."); //$NON-NLS-1$
                return null; // TODO(enr): Throw an exception here
            }
        } else if (clazz.isInstance(jaxbElement)) {
            return jaxbElement;
        } else {
            System.err.println("Cannot cast " + jaxbElement + " to class JAXBElement<?> or " + clazz //$NON-NLS-1$ //$NON-NLS-2$
                    + " (actual class is " + jaxbElement.getClass() + ")."); //$NON-NLS-1$ //$NON-NLS-2$
            return null; // TODO(enr): Throw an exception here
        }
    }

    /**
     * Given a list of objects that presumably contains one and only one JAXBElement<clazz>, return
     * list.get(0).getObject(). This method is useful for parsing a sequence of XML elements of type
     * xs:any (but where you know that there is only one element of type clazz).
     * 
     * If the list contains 0, or more than 2 elements, return null. If the first element of the list
     * is not a JAXBElement<clazz>, return null else return ((JAXBElement<?>)list.get(0)).getValue()
     * 
     * @param jaxbElementList
     * @param clazz
     * @return
     */
    public static Object unwrap(List<Object> jaxbElementList, Class<?> clazz) {
        if (jaxbElementList.size() == 1) {
            return unwrap(jaxbElementList.get(0), clazz);
        } else {
            System.err.println("Cannot unwrap " + jaxbElementList + ". Size is " + jaxbElementList.size() //$NON-NLS-1$ //$NON-NLS-2$
                    + " (expected 1)."); //$NON-NLS-1$
            return null; // TODO(enr): Throw an exception here
        }
    }
}