Example usage for javax.xml.bind JAXBContext createJAXBIntrospector

List of usage examples for javax.xml.bind JAXBContext createJAXBIntrospector

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createJAXBIntrospector.

Prototype

public JAXBIntrospector createJAXBIntrospector() 

Source Link

Document

Creates a JAXBIntrospector object that can be used to introspect JAXB objects.

Usage

From source file:example.Main.java

public static void main(String[] args) throws Exception {
    Class[] classes = new Class[3];
    classes[0] = A.class;
    classes[1] = B.class;
    classes[2] = C.class;

    JAXBContext jc = JAXBContext.newInstance(classes);

    JAXBIntrospector ji = jc.createJAXBIntrospector();
    Map<QName, Class> classByQName = new HashMap<QName, Class>(classes.length);
    for (Class clazz : classes) {
        QName qName = ji.getElementName(clazz.newInstance());
        if (null != qName) {
            classByQName.put(qName, clazz);
        }// ww  w . ja va  2s.  c om
    }

    QName qName = new QName("http://www.example.com", "EH");
    System.out.println(classByQName.get(qName));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Object value = "Hello World";

    JAXBContext jc = JAXBContext.newInstance(String.class, Bar.class);
    JAXBIntrospector introspector = jc.createJAXBIntrospector();
    Marshaller marshaller = jc.createMarshaller();
    if (null == introspector.getElementName(value)) {
        JAXBElement jaxbElement = new JAXBElement(new QName("ROOT"), Object.class, value);
        marshaller.marshal(jaxbElement, System.out);
    } else {/*w ww .j  a  v  a 2 s .  co m*/
        marshaller.marshal(value, System.out);
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.jaxb.JAXBUtil.java

/**
 * Convenience method for retrieving and instance of {@link JAXBIntrospector} for JAXB generated
 * objects from a specific JAXB package name.
 *
 * @param jaxbPackageName - a string representing the package name of JAXB generated objects
 * @return an instance of {@link JAXBIntrospector} that corresponds to the JAXB package name
 * @throws JAXBException if an error occurs while creating a JAXB context
 *//*from   www  .j  ava2 s.  c  o  m*/
public static JAXBIntrospector getJAXBIntrospector(final String jaxbPackageName) throws JAXBException {

    if (jaxbPackageName != null) {
        final JAXBContext context = JAXBContext.newInstance(jaxbPackageName);
        return context.createJAXBIntrospector();
    } else {
        throw new JAXBException(
                "Could not instantiate JAXB introspector because the JAXB package namespace was null");
    }
}

From source file:org.castor.jaxb.CastorJAXBIntrospectorTest.java

/**
 * Sets up the test environment./*w w  w . j a va  2  s. c  om*/
 *
 * @throws javax.xml.bind.JAXBException
 *             if any error occurs
 */
@Before
public void setUp() throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(Entity.class);
    introspector = context.createJAXBIntrospector();
}

From source file:org.opennms.netmgt.ackd.readers.HypericAckProcessor.java

/**
 * <p>parseHypericAlerts</p>
 *
 * @param reader a {@link java.io.Reader} object.
 * @return a {@link java.util.List} object.
 * @throws javax.xml.bind.JAXBException if any.
 * @throws javax.xml.stream.XMLStreamException if any.
 *///from  www  .ja  v  a2 s  . c o  m
public static List<HypericAlertStatus> parseHypericAlerts(Reader reader)
        throws JAXBException, XMLStreamException {
    List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>();

    // Instantiate a JAXB context to parse the alert status
    JAXBContext context = JAXBContext
            .newInstance(new Class[] { HypericAlertStatuses.class, HypericAlertStatus.class });
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLEventReader xmler = xmlif.createXMLEventReader(reader);
    EventFilter filter = new EventFilter() {
        @Override
        public boolean accept(XMLEvent event) {
            return event.isStartElement();
        }
    };
    XMLEventReader xmlfer = xmlif.createFilteredReader(xmler, filter);
    // Read up until the beginning of the root element
    StartElement startElement = (StartElement) xmlfer.nextEvent();
    // Fetch the root element name for {@link HypericAlertStatus} objects
    String rootElementName = context.createJAXBIntrospector().getElementName(new HypericAlertStatuses())
            .getLocalPart();
    if (rootElementName.equals(startElement.getName().getLocalPart())) {
        Unmarshaller unmarshaller = context.createUnmarshaller();
        // Use StAX to pull parse the incoming alert statuses
        while (xmlfer.peek() != null) {
            Object object = unmarshaller.unmarshal(xmler);
            if (object instanceof HypericAlertStatus) {
                HypericAlertStatus alertStatus = (HypericAlertStatus) object;
                retval.add(alertStatus);
            }
        }
    } else {
        // Try to pull in the HTTP response to give the user a better idea of what went wrong
        StringBuffer errorContent = new StringBuffer();
        LineNumberReader lineReader = new LineNumberReader(reader);
        try {
            String line;
            while (true) {
                line = lineReader.readLine();
                if (line == null) {
                    break;
                } else {
                    errorContent.append(line.trim());
                }
            }
        } catch (IOException e) {
            errorContent.append("Exception while trying to print out message content: " + e.getMessage());
        }

        // Throw an exception and include the erroneous HTTP response in the exception text
        throw new JAXBException("Found wrong root element in Hyperic XML document, expected: \""
                + rootElementName + "\", found \"" + startElement.getName().getLocalPart() + "\"\n"
                + errorContent.toString());
    }
    return retval;
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBUtils.java

private static JAXBIntrospector internalCreateIntrospector(final JAXBContext context) {
    JAXBIntrospector i;//from w w  w .j a  v  a 2s .  c o m
    i = (JAXBIntrospector) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return context.createJAXBIntrospector();
        }
    });
    return i;
}