package de.ugoe.cs.swe.bnftools.generator;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;



import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;


public class foToPdf {


	
	public static void createPdfFromFo(String fileName) throws IOException, FOPException,
	TransformerException{

		createfoFromPdf(fileName,fileName);
	}
	
	private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException,
	TransformerException{
		File file = new File(foFileName+".fo");
		// Step 1: Construct a FopFactory
		FopFactory fopFactory = FopFactory.newInstance();

		// Step 2: Set up output stream.
		OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));

		try {
			// Step 3: Construct fop with desired output format
			Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

			// Step 4: Setup JAXP using identity transformer
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();

			// Step 5: Setup input and output for XSLT transformation
			Source src = new StreamSource(file);

			// Resulting SAX events (the generated FO) must be piped through to FOP		
			Result res = new SAXResult(fop.getDefaultHandler());

			// Step 6: Start XSLT transformation and FOP processing
			transformer.transform(src, res);
		} finally {
			out.close();
		}
	}
	
	public static void createPdfFromFo(File file,String fileName) throws IOException, FOPException,
	TransformerException{

		createfoFromPdf(file,"mytest");
	}
	
	private static void createfoFromPdf(File file, String pdfFileName)throws IOException, FOPException,
	TransformerException{
		// Step 1: Construct a FopFactory
		FopFactory fopFactory = FopFactory.newInstance();

		// Step 2: Set up output stream.
		OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));

		try {
			// Step 3: Construct fop with desired output format
			Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

			// Step 4: Setup JAXP using identity transformer
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();

			// Step 5: Setup input and output for XSLT transformation
			Source src = new StreamSource(file);

			// Resulting SAX events (the generated FO) must be piped through to FOP		
			Result res = new SAXResult(fop.getDefaultHandler());

			// Step 6: Start XSLT transformation and FOP processing
			transformer.transform(src, res);
		} finally {
			out.close();
		}
	}
}
