Search in sources :

Example 6 with JAXBSource

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

the class OrcidValidationJaxbContextResolver method validate.

public void validate(Object toValidate) {
    String apiVersion = getApiVersion();
    String schemaFilenamePrefix = getSchemaFilenamePrefix(toValidate.getClass(), apiVersion);
    try {
        Schema schema = getSchema(schemaFilenamePrefix, apiVersion);
        JAXBContext context = JAXBContext.newInstance(toValidate.getClass());
        Source source = new JAXBSource(context, toValidate);
        Validator validator = schema.newValidator();
        validator.validate(source);
    } catch (SAXException | JAXBException | IOException e) {
        //Validation exceptions should return BAD_REQUEST status
        throw new WebApplicationException(e, Status.BAD_REQUEST.getStatusCode());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Schema(javax.xml.validation.Schema) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) JAXBSource(javax.xml.bind.util.JAXBSource) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException)

Example 7 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ddf by codice.

the class TestXmlParser method testUnmarshalSource.

@Test
public void testUnmarshalSource() throws Exception {
    JAXBContext motherContext = JAXBContext.newInstance(MotherElement.class);
    @SuppressWarnings("unchecked") JAXBElement<MotherElement> motherElementJAXBElement = new JAXBElement(new QName("mother"), MotherElement.class, mother);
    JAXBSource motherSource = new JAXBSource(motherContext, motherElementJAXBElement);
    MotherElement unmarshal = parser.unmarshal(configurator, MotherElement.class, motherSource);
    assertEquals(mother.getAge(), unmarshal.getAge());
    assertEquals(mother.getFirstname(), unmarshal.getFirstname());
    assertEquals(mother.getLastname(), unmarshal.getLastname());
    assertEquals(mother.getChild().size(), unmarshal.getChild().size());
    assertEquals(luke.getFirstname(), unmarshal.getChild().get(0).getFirstname());
    assertEquals(leia.getAge(), unmarshal.getChild().get(1).getAge());
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) JAXBSource(javax.xml.bind.util.JAXBSource) Test(org.junit.Test)

Example 8 with JAXBSource

use of javax.xml.bind.util.JAXBSource in project ddf by codice.

the class TestXmlParser method testUnmarshalSourceJAXBException.

@Test
public void testUnmarshalSourceJAXBException() throws Exception {
    JAXBContext motherContext = JAXBContext.newInstance(MotherElement.class);
    @SuppressWarnings("unchecked") JAXBElement<MotherElement> motherElementJAXBElement = new JAXBElement(new QName("mother"), MotherElement.class, mother);
    JAXBSource motherSource = new JAXBSource(motherContext, motherElementJAXBElement);
    configurator.addProperty("BadKey", "BadValue");
    thrown.expect(ParserException.class);
    parser.unmarshal(configurator, MotherElement.class, motherSource);
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) JAXBSource(javax.xml.bind.util.JAXBSource) Test(org.junit.Test)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)8 JAXBSource (javax.xml.bind.util.JAXBSource)8 JAXBException (javax.xml.bind.JAXBException)6 Test (org.junit.Test)3 IOException (java.io.IOException)2 JAXBElement (javax.xml.bind.JAXBElement)2 QName (javax.xml.namespace.QName)2 Source (javax.xml.transform.Source)2 StreamSource (javax.xml.transform.stream.StreamSource)2 Schema (javax.xml.validation.Schema)2 Validator (javax.xml.validation.Validator)2 MotherElement (org.codice.ddf.parser.xml.domain.MotherElement)2 SAXException (org.xml.sax.SAXException)2 WebApplicationException (javax.ws.rs.WebApplicationException)1 SchemaFactory (javax.xml.validation.SchemaFactory)1 GroupIdRecord (org.orcid.jaxb.model.groupid_rc1.GroupIdRecord)1 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)1