Search in sources :

Example 56 with JAXBContext

use of javax.xml.bind.JAXBContext in project opennms by OpenNMS.

the class ValidatingMessageBodyReader method readFrom.

@Override
public T readFrom(final Class<T> clazz, final Type type, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, String> parameters, final InputStream stream) throws IOException, WebApplicationException {
    LOG.debug("readFrom: {}/{}/{}", clazz.getSimpleName(), type, mediaType);
    JAXBContext jaxbContext = null;
    final ContextResolver<JAXBContext> resolver = providers.getContextResolver(JAXBContext.class, mediaType);
    try {
        if (resolver != null) {
            jaxbContext = resolver.getContext(clazz);
        }
        if (jaxbContext == null) {
            jaxbContext = JAXBContext.newInstance(clazz);
        }
        return JaxbUtils.unmarshal(clazz, new InputSource(stream), jaxbContext);
    } catch (final JAXBException e) {
        LOG.warn("An error occurred while unmarshaling a {} object", clazz.getSimpleName(), e);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : InputSource(org.xml.sax.InputSource) WebApplicationException(javax.ws.rs.WebApplicationException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 57 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class JpaJaxbEducationAdapterTest method getEducation.

private Education getEducation(boolean full) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(new Class[] { Education.class });
    Unmarshaller unmarshaller = context.createUnmarshaller();
    String name = "/record_2.0/samples/read_samples/education-2.0.xml";
    if (full) {
        name = "/record_2.0/samples/read_samples/education-full-2.0.xml";
    }
    InputStream inputStream = getClass().getResourceAsStream(name);
    return (Education) unmarshaller.unmarshal(inputStream);
}
Also used : Education(org.orcid.jaxb.model.record_v2.Education) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 58 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class TestXmlValidity method before.

@Before
public void before() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(OrcidMessage.class);
    unmarshaller = context.createUnmarshaller();
}
Also used : JAXBContext(javax.xml.bind.JAXBContext) Before(org.junit.Before)

Example 59 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class ConvertVrc3ToVrc4Test method upgradeAddressesToVrc3Test.

@Test
public void upgradeAddressesToVrc3Test() throws JAXBException {
    JAXBContext jaxbContext1 = JAXBContext.newInstance(Addresses.class);
    JAXBContext jaxbContext2 = JAXBContext.newInstance(org.orcid.jaxb.model.record_rc4.Addresses.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext1.createUnmarshaller();
    InputStream rc3Stream = ConvertVrc3ToVrc4Test.class.getClassLoader().getResourceAsStream("test-addresses-2.0_rc3.xml");
    InputStream rc4Stream = ConvertVrc3ToVrc4Test.class.getClassLoader().getResourceAsStream("test-addresses-2.0_rc4.xml");
    Addresses rc4Element = (Addresses) jaxbUnmarshaller.unmarshal(rc3Stream);
    jaxbUnmarshaller = jaxbContext2.createUnmarshaller();
    org.orcid.jaxb.model.record_rc4.Addresses rc4Element1 = (org.orcid.jaxb.model.record_rc4.Addresses) jaxbUnmarshaller.unmarshal(rc4Stream);
    V2Convertible result = versionConverterV2_0_rc3ToV2_0_rc4.upgrade(new V2Convertible(rc4Element, "v2_rc3"));
    org.orcid.jaxb.model.record_rc4.Addresses rc4Element2 = (org.orcid.jaxb.model.record_rc4.Addresses) result.getObjectToConvert();
    assertEquals(rc4Element1, rc4Element2);
}
Also used : Addresses(org.orcid.jaxb.model.record_rc3.Addresses) V2Convertible(org.orcid.core.version.V2Convertible) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Example 60 with JAXBContext

use of javax.xml.bind.JAXBContext in project ORCID-Source by ORCID.

the class ConvertVrc3ToVrc4Test method upgradePeerReviewsToVrc3Test.

@Test
public void upgradePeerReviewsToVrc3Test() throws JAXBException {
    JAXBContext jaxbContext1 = JAXBContext.newInstance(PeerReviews.class);
    JAXBContext jaxbContext2 = JAXBContext.newInstance(org.orcid.jaxb.model.record.summary_rc4.PeerReviews.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext1.createUnmarshaller();
    InputStream rc3Stream = ConvertVrc3ToVrc4Test.class.getClassLoader().getResourceAsStream("test-peer-reviews-2.0_rc3.xml");
    InputStream rc4Stream = ConvertVrc3ToVrc4Test.class.getClassLoader().getResourceAsStream("test-peer-reviews-2.0_rc4.xml");
    PeerReviews rc4PeerReviews = (PeerReviews) jaxbUnmarshaller.unmarshal(rc3Stream);
    jaxbUnmarshaller = jaxbContext2.createUnmarshaller();
    org.orcid.jaxb.model.record.summary_rc4.PeerReviews rc4PeerReviews1 = (org.orcid.jaxb.model.record.summary_rc4.PeerReviews) jaxbUnmarshaller.unmarshal(rc4Stream);
    V2Convertible result = versionConverterV2_0_rc3ToV2_0_rc4.upgrade(new V2Convertible(rc4PeerReviews, "v2_rc3"));
    org.orcid.jaxb.model.record.summary_rc4.PeerReviews rc4PeerReviews2 = (org.orcid.jaxb.model.record.summary_rc4.PeerReviews) result.getObjectToConvert();
    assertEquals(rc4PeerReviews1, rc4PeerReviews2);
}
Also used : V2Convertible(org.orcid.core.version.V2Convertible) InputStream(java.io.InputStream) PeerReviews(org.orcid.jaxb.model.record.summary_rc3.PeerReviews) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)1150 Unmarshaller (javax.xml.bind.Unmarshaller)558 Marshaller (javax.xml.bind.Marshaller)371 JAXBException (javax.xml.bind.JAXBException)330 Test (org.junit.Test)226 File (java.io.File)161 InputStream (java.io.InputStream)150 StringWriter (java.io.StringWriter)138 IOException (java.io.IOException)105 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 StringReader (java.io.StringReader)74 StreamSource (javax.xml.transform.stream.StreamSource)73 JAXBElement (javax.xml.bind.JAXBElement)72 ArrayList (java.util.ArrayList)58 URL (java.net.URL)55 ByteArrayInputStream (java.io.ByteArrayInputStream)50 Schema (javax.xml.validation.Schema)48 SchemaFactory (javax.xml.validation.SchemaFactory)48 OutputStream (java.io.OutputStream)42 BaseTest (org.orcid.core.BaseTest)39