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());
}
}
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());
}
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);
}
Aggregations