Example usage for javax.xml.bind JAXBElement getDeclaredType

List of usage examples for javax.xml.bind JAXBElement getDeclaredType

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement getDeclaredType.

Prototype

public Class<T> getDeclaredType() 

Source Link

Document

Returns the Java binding of the xml element declaration's type attribute.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class, ObjectFactory.class);

    StringReader xml = new StringReader("<foo><C>Hello World</C></foo>");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    JAXBElement<?> aOrBOrCOrD = foo.aOrBOrCOrD;
    System.out.println(aOrBOrCOrD.getName().getLocalPart());
    System.out.println(aOrBOrCOrD.getDeclaredType());
    System.out.println(aOrBOrCOrD.getValue());
}

From source file:Main.java

public static <BoundType> boolean isJAXBElement(Class<BoundType> declaredType, QName name, Class scope,
        Object value) {//from   w w w .  ja  v a2s  .co  m
    if (value == null) {
        return false;
    } else if (value instanceof JAXBElement) {
        final JAXBElement<?> element = (JAXBElement<?>) value;

        return element.getName().equals(name) && declaredType.isAssignableFrom(element.getDeclaredType());
    } else {
        return false;
    }
}

From source file:com.vmware.vchs.api.samples.services.Compute.java

/**
 * This method uses the vCloud Query API to retrieve vCloud VDC Templates. It will return a
 * collection of VAppTemplateType instances for each template retrieved.
 * /*from  w  w  w.  j a  va2s.c  o m*/
 * @param vdc
 *            the VDC to find and return all templates from
 */
public static final Collection<VAppTemplateType> getTemplatesForVdc(String computeUrl, VdcType vdc,
        String version, String token) {
    // Query the vCloud Query API to search for a vAppTemplate matching the
    // options.templateName (command line option --templatename)
    QueryResultRecordsType queryResults = HttpUtils.getQueryResults(getBaseUrl(computeUrl), "type=vAppTemplate",
            version, token);

    List<JAXBElement<? extends QueryResultRecordType>> rslt = queryResults.getRecord();

    // We should have only one record with the name matching templateName
    if (null != rslt && rslt.size() > 0) {
        Collection<VAppTemplateType> templates = new ArrayList<VAppTemplateType>();

        for (JAXBElement<? extends QueryResultRecordType> record : rslt) {
            if (record.getDeclaredType().equals(QueryResultVAppTemplateRecordType.class)) {
                QueryResultVAppTemplateRecordType template = (QueryResultVAppTemplateRecordType) record
                        .getValue();

                // invoke the GET request to the template href to get the VAppTemplateType
                HttpGet get = new HttpGet(template.getHref());
                get.setHeader(HttpHeaders.ACCEPT,
                        SampleConstants.APPLICATION_PLUS_XML_VERSION + version + ";charset=utf-8");
                get.setHeader(SampleConstants.VCD_AUTHORIZATION_HEADER, token);
                HttpResponse response = HttpUtils.httpInvoke(get);

                // make sure the status is 200 OK
                if (null != response && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    // unmarshal the response entity into a VAppTemplateType
                    VAppTemplateType vat = HttpUtils.unmarshal(response.getEntity(), VAppTemplateType.class);
                    templates.add(vat);
                }
            }
        }

        return templates;
    }

    return null;
}

From source file:org.wallerlab.yoink.molecular.service.translator.ParameterTranslator.java

private void checkIfParameterList(Map<JobParameter, Object> parameters, Object elementList) {
    // check parameterList
    JAXBElement element = (JAXBElement) elementList;
    if (element.getDeclaredType() == ParameterList.class) {
        parseParameters(parameters, element);
    }/*  www .j  ava  2  s  .  co m*/
}

From source file:org.wallerlab.yoink.molecular.service.translator.ParameterTranslator.java

private void parseParameter(Map<JobParameter, Object> parameters, ParameterList cmlParameterList) {
    for (Object elementParameter : cmlParameterList.getAnyCmlOrAnyOrAny()) {
        JAXBElement elementJAXB = (JAXBElement) elementParameter;
        // check parameter
        if (elementJAXB.getDeclaredType() == Parameter.class) {
            parseParameterValue(parameters, elementJAXB);
        }/*from  w ww.  ja  va2s.  c  o m*/
    }
}

From source file:org.wallerlab.yoink.service.PropertyWrapper.java

private void changeMoleculeId(Job<JAXBElement> job, JAXBElement<Cml> cmlElement) {
    for (Object elementList : cmlElement.getValue().getAnyCmlOrAnyOrAny()) {
        // check moleucleList
        JAXBElement element = (JAXBElement) elementList;
        if (element.getDeclaredType() == MoleculeList.class) {
            loopOverAllMolecules(job, elementList);
        }/*from  w w  w. ja v a2  s  .c  om*/

    }
}

From source file:be.fedict.eid.pkira.contracts.EIDPKIRAContractsClient.java

/**
 * Unmarshals an XML from a reader.//from w  w w .  ja v a 2s.  c  o  m
 * 
 * @param <T>
 *            the type of document that is expected.
 * @param reader
 *            the reader to get the document from.
 * @param clazz
 *            the type of object.
 * @return the unmarshalled object.
 * @throws XmlMarshallingException
 *             when this fails.
 */
@SuppressWarnings("unchecked")
public <T extends EIDPKIRAContractType> T unmarshal(Reader reader, Class<T> clazz)
        throws XmlMarshallingException {
    Object unmarshalled;
    try {
        unmarshalled = getUnmarshaller().unmarshal(reader);
    } catch (JAXBException e) {
        throw new XmlMarshallingException("Cannot unmarshal XML data.", e);
    }

    if (unmarshalled != null && unmarshalled instanceof JAXBElement<?>) {
        JAXBElement<?> jaxbElement = (JAXBElement<?>) unmarshalled;
        if (jaxbElement.getDeclaredType() == clazz) {
            return (T) jaxbElement.getValue();
        } else {
            throw new XmlMarshallingException("XML contains an invalid element: "
                    + jaxbElement.getDeclaredType().getSimpleName() + ". Expected " + clazz.getSimpleName());
        }
    }

    throw new XmlMarshallingException("XML did not contain a valid element.");
}

From source file:org.javelin.sws.ext.bind.SweJaxbUnmarshallerTest.java

@Test
public void unmarshalVeryComplexContentExplicit() throws Exception {
    JAXBContext context = JAXBContext.newInstance("org.javelin.sws.ext.bind.context1");
    // ClassWithVeryComplexContent value = new ClassWithVeryComplexContent("test", "str", new ClassWithComplexContent("test", 42, "inside"));
    Unmarshaller um = context.createUnmarshaller();
    InputStream inputStream = new ClassPathResource("very-complex-content-01.xml", this.getClass())
            .getInputStream();//from  w  ww  . j a va 2 s . c o m
    JAXBElement<ClassWithVeryComplexContent> je = um.unmarshal(
            XMLInputFactory.newFactory().createXMLEventReader(inputStream), ClassWithVeryComplexContent.class);
    assertTrue(je.getDeclaredType() == ClassWithVeryComplexContent.class);
    assertFalse("Should not be nil", je.isNil());
    assertTrue(je.getValue() instanceof ClassWithVeryComplexContent);
    ClassWithVeryComplexContent ob = (ClassWithVeryComplexContent) je.getValue();
    assertThat(ob.getStr(), equalTo("test-1"));
    assertThat(ob.getInside(), equalTo("str"));
    assertThat(ob.getInside2().getNumber(), equalTo(42));
    assertThat(ob.getInside2().getStr(), equalTo("test-2"));
    assertThat(ob.getInside2().getInside(), equalTo("inside"));
}

From source file:be.fedict.eid.pkira.xkmsws.util.XMLMarshallingUtil.java

/**
 * Extracts the first element from the list matching the type.
 * //from www  .  j  a va2  s .co  m
 * @param <T>
 *            expected type.
 * @param clazz
 *            expected type.
 * @param list
 *            list with either this type of a JAXBElement with this type.
 * @return the first matching element.
 */
public <T> T getFromList(Class<T> clazz, List<Object> list) {
    for (Object object : list) {
        if (clazz.isInstance(object)) {
            return clazz.cast(object);
        }
        if (object instanceof JAXBElement<?>) {
            JAXBElement<?> jaxbElement = (JAXBElement<?>) object;
            if (jaxbElement.getDeclaredType().equals(clazz)) {
                return clazz.cast(jaxbElement.getValue());
            }
        }
    }

    return null;
}

From source file:org.wallerlab.yoink.molecular.service.translator.MolecularSystemTranslator.java

private void checkIfMoleculeList(Object elementMoleculeList) {
    @SuppressWarnings("unchecked")
    JAXBElement<MoleculeList> element = (JAXBElement<MoleculeList>) elementMoleculeList;
    if (element.getDeclaredType() == MoleculeList.class) {
        MoleculeList moleculeList = (MoleculeList) element.getValue();
        loopOverElementsInCmlMoleculeList(moleculeList);
    }/*from ww  w. j a v  a2 s  .  com*/
}