Search in sources :

Example 56 with Marshaller

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);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Marshaller(javax.xml.bind.Marshaller) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) File(java.io.File)

Example 57 with Marshaller

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());
}
Also used : Marshaller(javax.xml.bind.Marshaller) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 58 with Marshaller

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document)

Example 59 with Marshaller

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document)

Example 60 with Marshaller

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());
}
Also used : Pattern(java.util.regex.Pattern) Marshaller(javax.xml.bind.Marshaller) Diff(org.custommonkey.xmlunit.Diff) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) StringWriter(java.io.StringWriter) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Aggregations

Marshaller (javax.xml.bind.Marshaller)280 JAXBContext (javax.xml.bind.JAXBContext)162 JAXBException (javax.xml.bind.JAXBException)100 StringWriter (java.io.StringWriter)82 Test (org.junit.Test)33 JAXBElement (javax.xml.bind.JAXBElement)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 File (java.io.File)29 Unmarshaller (javax.xml.bind.Unmarshaller)21 IOException (java.io.IOException)20 FileOutputStream (java.io.FileOutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)15 QName (javax.xml.namespace.QName)14 Element (org.w3c.dom.Element)14 HashMap (java.util.HashMap)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)11 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7