//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.coreImpl.xpdl.serializer;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.SQLException;

import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.serializer.XPDLSerializer;
import org.huihoo.workflow.xpdl.serializer.XPDLSerializeException;

import org.huihoo.commons.jdbc.Store;


public class DefaultXPDLSerializer implements XPDLSerializer
{

	public void serialize(WorkflowPackage pkg, Store store) throws XPDLSerializeException,IOException,SQLException
	{
		String storeType = store.getType();
		if (Store.XML_STORE.equals(storeType))
		{
			FileOutputStream fos= new FileOutputStream(store.getProperty(org.huihoo.workflow.WorkflowGlobals.XPDL_FILE_KEY));
			serialize(pkg,fos);
			fos.close();
		}
		else
		{
			XPDLSerializerJDBC.serialize(pkg, store);
		}
	}
	
	public void serialize(WorkflowPackage pkg, OutputStream out)
		throws XPDLSerializeException, IOException
	{
		serialize(pkg,out,System.getProperty("file.encoding"));
	}

	public void serialize(
		WorkflowPackage pkg,
		OutputStream out,
		String charsetName)
		throws XPDLSerializeException, IOException
	{
		serialize(pkg,new OutputStreamWriter(out,charsetName));
	}

	public void serialize(WorkflowPackage pkg, Writer out)
		throws XPDLSerializeException, IOException
	{
		XPDLSerializerIO.serialize(pkg,out);
		out.flush();
	}
}
