 /*
 * Copyright (C) 11-sept.-2003  Jean-Yves Beaujean 
 * 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 2 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, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */
package jxtray.io;


import java.io.IOException;
import java.io.FileOutputStream;
import java.io.File;
import java.util.jar.JarOutputStream;
import java.util.jar.JarEntry;
/**
 * @author Jean-Yves Beaujean
 *
 * This class allows to write a String in an jar file.  
 */
public class JarWriterStrategy extends WriterStrategy
{
	
	private JarOutputStream jarOutput = null;

	public JarWriterStrategy(String filename)
	{
		this._fileName = filename;
	}
	
	public void open() throws IOException
	{
		if ( _fileName != null )
		{
			jarOutput = new JarOutputStream(new FileOutputStream(new File(_fileName.substring(0,_fileName.lastIndexOf("."))+".jar")));
			jarOutput.putNextEntry(new JarEntry((new File(_fileName)).getName()));
		}
	}
	
	public void close() throws IOException
	{
		if ( _fileName != null && jarOutput != null )
		{
			jarOutput.closeEntry();
			jarOutput.close();
		}
	}		
	/** Return the writer name 
	 *  @return String
	 */
	public String getName() { return "Jar Writer";}
		
	/** Write the data <code>data</code> in file <code>file</file>
	 *  It use <code>JarOutputStream</code>
	 *  @throws IOException
	 */		
	public void write ( String data) throws IOException
	{
		jarOutput.write(data.getBytes());
	}
}
