Example usage for org.springframework.oxm Marshaller marshal

List of usage examples for org.springframework.oxm Marshaller marshal

Introduction

In this page you can find the example usage for org.springframework.oxm Marshaller marshal.

Prototype

void marshal(Object graph, Result result) throws IOException, XmlMappingException;

Source Link

Document

Marshal the object graph with the given root into the provided Result .

Usage

From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializer.java

/**
 * Serializes the identified stream from the package state to the supplied result.
 *
 * @param state    the package state object containing the identified stream
 * @param streamId the stream identifier for the content being serialized
 * @param result   holds the output stream for the serialization result
 *///from  www  .  ja  v a  2 s  .c  o m
void serializeToResult(PackageState state, StreamId streamId, StreamResult result) {

    if (marshallerMap == null) {
        throw new IllegalStateException(ERR_MISSING_MARSHALLINGMAP);
    }

    PropertyDescriptor pd = propertyDescriptors.get(streamId);

    if (pd == null) {
        throw new IllegalArgumentException(String.format(ERR_INVALID_STREAMID, streamId.name(),
                PackageState.class.getName(),
                propertyDescriptors.keySet().stream().map(Enum::name).collect(Collectors.joining(", "))));
    }

    Object toSerialize;

    try {
        toSerialize = pd.getReadMethod().invoke(state);
        if (toSerialize == null) {
            // The field on the package state had a null value, which is OK.  We have nothing to serialize.
            return;
        }
    } catch (Exception e) {
        String err = String.format(ERR_INVOKING_METHOD, pd.getReadMethod(), state.getClass().getName(),
                e.getMessage());
        throw new RuntimeException(err, e);
    }

    try {
        StreamMarshaller streamMarshaller = marshallerMap.get(streamId);
        if (streamMarshaller == null) {
            throw new RuntimeException(String.format(ERR_MISSING_STREAMMARSHALLER, streamId));
        }

        Marshaller marshaller = streamMarshaller.getMarshaller();
        if (marshaller == null) {
            throw new RuntimeException(String.format(ERR_MISSING_SPRINGMARSHALLER, streamId, streamId));
        }

        marshaller.marshal(toSerialize, result);
    } catch (Exception e) {
        throw new RuntimeException(String.format(ERR_MARSHALLING_STREAM, streamId, e.getMessage()), e);
    }
}

From source file:org.apereo.portal.io.xml.IdentityImportExportTestUtilities.java

public static <T> void testIdentityImportExport(TransactionOperations transactionOperations,
        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   w  ww  .j a  v a 2  s  .com
        @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:org.apereo.portal.io.xml.JaxbPortalDataHandlerService.java

@Override
public String exportData(String typeId, String dataId, Result result) {
    final IDataExporter<Object> portalDataExporter = this.getPortalDataExporter(typeId);
    final Object data = portalDataExporter.exportData(dataId);
    if (data == null) {
        return null;
    }/*from   w  w w.ja  v a2  s  .com*/

    final Marshaller marshaller = portalDataExporter.getMarshaller();
    try {
        marshaller.marshal(data, result);
        return portalDataExporter.getFileName(data);
    } catch (XmlMappingException e) {
        throw new RuntimeException("Failed to map provided portal data to XML", e);
    } catch (IOException e) {
        throw new RuntimeException("Failed to write the provided XML data", e);
    }
}

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  2 s .  c  om
        @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);
    }
}