org.genericprodigy.rp.heroquest.XmlMarshaller.java Source code

Java tutorial

Introduction

Here is the source code for org.genericprodigy.rp.heroquest.XmlMarshaller.java

Source

/*
 * Copyright (C) 2011 Phil J. Laszkowicz (p.j.laszkowicz@genericprodigy.org)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.genericprodigy.rp.heroquest;

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Phil J. Laszkowicz (p.j.laszkowicz@genericprodigy.org)
 */
public class XmlMarshaller<T extends Model> extends AbstractMarshaller {

    /** 
     * Static reference to Jakarta Logging for application logging.
     *
     * @since 0.1
     */
    private static Log log = LogFactory.getLog(XmlMarshaller.class);

    /**
     * Factory method to create an instance of the {@link XmlMarshaller}
     * object.
     *
     * @since 0.1
     * @param <T> Generic type extended from the {@link Model} class.
     * @param model Model object to be marshalled or unmarshalled.
     * @return New instance of the {@link XmlMarshaller} object.
     */
    public static <T extends Model> XmlMarshaller createInstance(T model) {
        return new XmlMarshaller(model);
    }

    /**
     * Fundamental {@link Model} object marshalled or unmarshalled during
     * the process.
     *
     * @since 0.1
     */
    private T model = null;

    /**
     * Instantiates a new instance of the {@link XmlMarshaller} object.
     * This constructor is private and can only be invoked by the
     * {@code XmlMarshaller} factory method; {@code createInstance}.
     * @param model
     */
    private XmlMarshaller(T model) {
        this.model = model;
    }

    /**
     * Marshals the {@link Model} object passed in to the {@code createInstance}
     * factory method into a new {@code MarshalledModel} entity object.
     *
     * @return {@link MarshalledModel} entity object with an XML representation
     * of the object state.
     */
    @Override
    public MarshalledModel marshal() {
        MarshalledXml response = new MarshalledXml();

        String data = null;

        try {
            JAXBContext context = JAXBContext.newInstance(this.model.getClass());
            Marshaller marshaller = context.createMarshaller();
            StringWriter sw = new StringWriter();
            marshaller.marshal(this.model, sw);
            data = sw.toString();
            response.setData(data);
            log.debug("Output Xml = " + sw.toString());
        } catch (javax.xml.bind.PropertyException propEx) {
            log.error("javax.xml.bind.PropertyException caught: " + propEx.getMessage(), propEx);
            propEx.printStackTrace();
        } catch (javax.xml.bind.JAXBException jaxbEx) {
            log.error("javax.xml.bind.JAXBException caught: " + jaxbEx.getMessage(), jaxbEx);
            jaxbEx.printStackTrace();
        } catch (Exception ex) {
            log.error("Exception caught: " + ex.getMessage(), ex);
            ex.printStackTrace();
        }

        return (MarshalledModel) response;
    }

    /**
     * Unmarshals the {@link MarshalledModel} entity object from XML to a
     * {@link Model} object complete with existing state.
     *
     * @param data Marshalled entity representation of the object.
     * @return Unmarshalled entity object complete with state.
     */
    @Override
    public T unmarshal(MarshalledModel data) {

        MarshalledXml xml = (MarshalledXml) data;
        T response = null;

        try {
            JAXBContext context = JAXBContext.newInstance(this.model.getClass());
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xml.getData());
            this.model = (T) unmarshaller.unmarshal(sr);
        } catch (javax.xml.bind.PropertyException propEx) {
            log.error("javax.xml.bind.PropertyException caught: " + propEx.getMessage(), propEx);
            propEx.printStackTrace();
        } catch (javax.xml.bind.JAXBException jaxbEx) {
            log.error("javax.xml.bind.JAXBException caught: " + jaxbEx.getMessage(), jaxbEx);
            jaxbEx.printStackTrace();
        } catch (Exception ex) {
            log.error("Exception caught: " + ex.getMessage(), ex);
            ex.printStackTrace();
        }

        return this.model;

    }
}