/*
 * Created on Oct 11, 2006
 * 
 * (C) Copyright TANDBERG Television Ltd.
 */

package com.tandbergtv.workflow.sanmanager.internal;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Class that uses Java's XML Serialization to serialize / deserialize objects to XML. 
 * 
 * @author Vijay Silva
 */
final class XMLObjectSerializer
{
	// Cannot instantiate
	private XMLObjectSerializer()
	{
		super();
	}

	/**
	 * Method to Serialize the Object into an XML String.
	 * 
	 * @param obj
	 *            the object to serialize
	 * @return The String containing the XML Serialization of the object
	 */
	public static String serializeObject(Object obj)
	{
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		BufferedOutputStream bufStream = new BufferedOutputStream(outStream);
		XMLEncoder encoder = new XMLEncoder(bufStream);
		encoder.writeObject(obj);
		encoder.close();

		return outStream.toString();
	}

	/**
	 * Method to Deserialize the Object from an XML String to the Java Object.
	 * 
	 * @param objString
	 *            the xml String representation of the object
	 * @return The Deserialized Object
	 */
	public static Object deserializeObject(String objString)
	{
		ByteArrayInputStream inStream = new ByteArrayInputStream(objString.getBytes());
		BufferedInputStream bufStream = new BufferedInputStream(inStream);
		XMLDecoder decoder = new XMLDecoder(bufStream);
		return decoder.readObject();
	}
}
