Example usage for org.springframework.oxm Unmarshaller unmarshal

List of usage examples for org.springframework.oxm Unmarshaller unmarshal

Introduction

In this page you can find the example usage for org.springframework.oxm Unmarshaller unmarshal.

Prototype

Object unmarshal(Source source) throws IOException, XmlMappingException;

Source Link

Document

Unmarshal the given Source into an object graph.

Usage

From source file:org.jasig.portal.io.xml.AbstractIdentityImportExportTest.java

protected final <T> void testIdentityImportExport(final IDataImporter<T> dataImporter,
        final IDataExporter<?> dataExporter, Resource resource, Function<T, String> getName) throws Exception {

    final String importData = toString(resource);

    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new StringReader(importData));

    //Unmarshall from XML
    final Unmarshaller unmarshaller = dataImporter.getUnmarshaller();
    final StAXSource source = new StAXSource(xmlEventReader);
    @SuppressWarnings("unchecked")
    final T dataImport = (T) unmarshaller.unmarshal(source);

    //Make sure the data was unmarshalled
    assertNotNull("Unmarshalled import data was null", dataImport);

    //Import the data
    dataImporter.importData(dataImport);

    //Export the data
    final String name = getName.apply(dataImport);
    final Object dataExport = transactionOperations.execute(new TransactionCallback<Object>() {
        /* (non-Javadoc)
         * @see org.springframework.transaction.support.TransactionCallback#doInTransaction(org.springframework.transaction.TransactionStatus)
         *///from ww w  . j a v  a  2s  .  co  m
        @Override
        public Object doInTransaction(TransactionStatus status) {
            return dataExporter.exportData(name);
        }
    });

    //Make sure the data was exported
    assertNotNull("Exported data was null", dataExport);

    //Marshall to XML
    final Marshaller marshaller = dataExporter.getMarshaller();

    final StringWriter result = new StringWriter();
    marshaller.marshal(dataExport, new StreamResult(result));

    //Compare the exported XML data with the imported XML data, they should match
    final String resultString = result.toString();
    try {
        XMLUnit.setIgnoreWhitespace(true);
        Diff d = new Diff(new StringReader(importData), new StringReader(resultString));
        assertTrue("Export result differs from import" + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between import XML and export XML", resultString,
                e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between import XML and export XML", resultString,
                e);
    }
}

From source file:uk.ac.ebi.interpro.scan.model.SignatureLibraryReleaseTest.java

public static void main(String[] args) throws IOException {
    //http://www.bioinf.manchester.ac.uk/dbbrowser/xmlprints/PR00460.xml
    if (args.length < 1) {
        System.err.println("Usage: SignatureLibraryReleaseTest <signatures-xml-file>");
    }//from  w  w  w. j  a  v  a  2s  .  co m
    String signaturesFile = args[0];
    org.springframework.core.io.Resource resource;
    if (signaturesFile.startsWith("http")) {
        resource = new UrlResource(signaturesFile);
    } else {
        resource = new FileSystemResource(signaturesFile);
    }
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "uk/ac/ebi/interpro/scan/model/oxm-context.xml");
    // Unmarshall
    Unmarshaller unmarshaller = (Unmarshaller) ctx.getBean("unmarshaller");
    SignatureLibraryRelease slr = (SignatureLibraryRelease) unmarshaller
            .unmarshal(new StreamSource(resource.getInputStream()));
    // Marshall
    // Marshaller marshaller     = (Marshaller)ctx.getBean("marshaller");
    //Writer writer = new StringWriter();
    //marshaller.marshal(slr, new StreamResult(writer));
    // Print
    LOGGER.debug("Received:");
    LOGGER.debug(slr.toString());
    //LOGGER.debug(writer.toString());
}