All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.java.trueupdate.jms.JAXB Maven / Gradle / Ivy

There is a newer version: 0.8.1
Show newest version
/*
 * Copyright (C) 2013 Schlichtherle IT Services & Stimulus Software.
 * All rights reserved. Use is subject to license terms.
 */
package net.java.trueupdate.jms;

import java.io.*;
import javax.xml.bind.*;
import net.java.trueupdate.message.*;

/**
 * Provides functions for JAXB.
 *
 * @author Christian Schlichtherle
 */
final class JAXB {

    private static final JAXBContext CONTEXT;
    static {
        try {
            CONTEXT = JAXBContext.newInstance(UpdateMessageDto.class);
        } catch (JAXBException ex) {
            throw new IllegalStateException(ex);
        }
    }

    static String encode(final UpdateMessage message) throws Exception {
        final StringWriter sw = new StringWriter(1024);
        final Marshaller m = marshaller();
        if (!message.attachedLogs().isEmpty()) try {
            m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
                    new DtoNamespaceMapper());
        } catch(Exception anIncompatibleJaxbImplementationIsUsed) {
        }
        m.marshal(adapter().marshal(message), sw);
        return sw.toString();
    }

    static UpdateMessage decode(String string) throws Exception {
        return (UpdateMessage) adapter().unmarshal(
                (UpdateMessageDto) unmarshaller().unmarshal(
                    new StringReader(string)));
    }

    private static Marshaller marshaller() throws JAXBException {
        return CONTEXT.createMarshaller();
    }

    private static Unmarshaller unmarshaller() throws JAXBException {
        return CONTEXT.createUnmarshaller();
    }

    private static UpdateMessageAdapter adapter() {
        return new UpdateMessageAdapter();
    }

    private JAXB() { }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy