use of javax.xml.bind.Marshaller in project ORCID-Source by ORCID.
the class ValidateV2RC4SamplesTest method marshall.
private void marshall(Object object, String path) throws JAXBException, SAXException, URISyntaxException {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(getClass().getResource(path).toURI()));
marshaller.setSchema(schema);
marshaller.marshal(object, System.out);
}
use of javax.xml.bind.Marshaller in project opennms by OpenNMS.
the class AbstractSpringJerseyRestTestCase method putXmlObject.
protected void putXmlObject(final JAXBContext context, final String url, final int expectedStatus, final Object object) throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Marshaller marshaller = context.createMarshaller();
marshaller.marshal(object, out);
final byte[] content = out.toByteArray();
final MockHttpServletRequest request = createRequest(servletContext, PUT, url, getUser(), getUserRoles());
request.setContentType(MediaType.APPLICATION_XML);
request.setContent(content);
final MockHttpServletResponse response = createResponse();
dispatch(request, response);
assertEquals(expectedStatus, response.getStatus());
}
use of javax.xml.bind.Marshaller in project ORCID-Source by ORCID.
the class ValidateV2RC3Identifiers method marshallToDOM.
/** Marshals to a DOM
*
* @param type
* @param obj
* @return
*/
private Source marshallToDOM(Class<?> type, Object obj) throws JAXBException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(obj, document);
DOMSource source = new DOMSource(document);
return source;
}
use of javax.xml.bind.Marshaller in project ORCID-Source by ORCID.
the class ValidateV2IdentifiersTest method marshallToDOM.
/** Marshals to a DOM
*
* @param type
* @param obj
* @return
*/
private Source marshallToDOM(Class<?> type, Object obj) throws JAXBException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
JAXBContext context = JAXBContext.newInstance(type);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(obj, document);
DOMSource source = new DOMSource(document);
return source;
}
use of javax.xml.bind.Marshaller in project ORCID-Source by ORCID.
the class MarshallingTest method testMarshallingV2_0_RC4.
@Test
public void testMarshallingV2_0_RC4() throws JAXBException, IOException, SAXException {
JAXBContext context = JAXBContext.newInstance("org.orcid.jaxb.model.notification.permission_rc4");
Unmarshaller unmarshaller = context.createUnmarshaller();
InputStream inputStream = MarshallingTest.class.getResourceAsStream(SAMPLE_PATH_RC4);
org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission notification = (org.orcid.jaxb.model.notification.permission_rc4.NotificationPermission) unmarshaller.unmarshal(inputStream);
assertNotNull(notification);
assertEquals(org.orcid.jaxb.model.notification_rc4.NotificationType.PERMISSION, notification.getNotificationType());
assertEquals(2, notification.getItems().getItems().size());
assertEquals("2014-01-01T14:45:32", notification.getSentDate().toXMLFormat());
// Back the other way
String expected = IOUtils.toString(getClass().getResourceAsStream(SAMPLE_PATH_RC4), "UTF-8");
Pattern pattern = Pattern.compile("<!--.*?-->\\s*", Pattern.DOTALL);
expected = pattern.matcher(expected).replaceAll("");
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.orcid.org/ns/notification ../notification-permission-2.0_rc4.xsd");
StringWriter writer = new StringWriter();
marshaller.marshal(notification, writer);
XMLUnit.setIgnoreWhitespace(true);
Diff diff = new Diff(expected, writer.toString());
assertTrue(diff.identical());
}
Aggregations