/*
 * FOPCreator.java
 *
 * Created on 25 de Maio de 2005, 15:36
 */

package siprp.util.fop;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
 *
 * @author  lflores
 */
public class FOPCreator
{
	private static final Object LOCK = new Object();
	
	private static FOPCreator fopCreator;
	
	/** Creates a new instance of FOPPrinter */
	private FOPCreator()
	{
	}
	
	public static FOPCreator getFOPCreator()
	{
		synchronized( LOCK )
		{
			if( fopCreator == null )
			{
				fopCreator = new FOPCreator();
			}
		}
		return fopCreator;
	}
	
	
	public void createFOfromXML( InputStream xml, InputStream xslt, OutputStream fo )
		throws IOException, TransformerException
	{
		 try
		 {
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer( new StreamSource( xslt ) );
			Source src = new StreamSource( xml );
			Result res = new StreamResult( fo );
			transformer.transform( src, res );
		}
		finally
		{
			fo.close();
		}
	}
}
