package fr.insee.eno.postprocessing.pdf;

import java.io.File;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.insee.eno.Constants;
import fr.insee.eno.exception.EnoGenerationException;
import fr.insee.eno.parameters.PostProcessing;
import fr.insee.eno.postprocessing.Postprocessor;
import fr.insee.eno.transform.xsl.XslTransformation;

/**
 * Customization of FO postprocessor.
 */
public class PDFMailingPostprocessor implements Postprocessor {

	private static final Logger logger = LoggerFactory.getLogger(PDFMailingPostprocessor.class);

	private XslTransformation saxonService = new XslTransformation();

	@Override
	public File process(File input, byte[] parameters, String surveyName) throws Exception {

		File outputForFOFile = new File(input.getParent(),
				Constants.BASE_NAME_FORM_FILE +
				Constants.MAILING_FO_EXTENSION);
		logger.debug("Output folder for basic-form : " + outputForFOFile.getAbsolutePath());

		InputStream FO_XSL = Constants.getInputStreamFromPath(Constants.TRANSFORMATIONS_CUSTOMIZATION_FO_4PDF_2);
		try {
			saxonService.transformFOToStep1FO(FileUtils.openInputStream(input),
					FileUtils.openOutputStream(outputForFOFile), FO_XSL);
		}catch(Exception e) {
			String errorMessage = "An error was occured during the " + toString() + " transformation. "+e.getMessage();
			logger.error(errorMessage);
			throw new EnoGenerationException(errorMessage);
		}
		
		FO_XSL.close();
		logger.info("End of Mailing post-processing : ");

		return outputForFOFile;
	}

	public String toString() {
		return PostProcessing.PDF_MAILING.name();
	}

}

@@ -83,16 +83,45 @@
 tStream;
+%0Aimport java.io.OutputStream;
 %0A%0Aimport
@@ -1036,16 +1036,20 @@
 Path());
+%09%0A%09%09
 %0A%0A%09%09Inpu
@@ -1156,51 +1156,37 @@
 ;%0A%09%09
-try %7B%0A%09%09%09saxonService.transformFOToStep1FO(
+%0A%09%09InputStream inputStream = 
 File
@@ -1217,15 +1217,40 @@
 put)
-,%0A%09%09%09%09%09
+;%0A%09%09OutputStream outputStream = 
 File
@@ -1288,16 +1288,88 @@
 rFOFile)
+;%0A%09%09try %7B%0A%09%09%09saxonService.transformFOToStep1FO(inputStream, outputStream
 , FO_XSL
@@ -1595,16 +1595,63 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09inputStream.close();%0A%09%09outputStream.close();%0A
 %09%09FO_XSL

Update PDFMailingPostprocessor.java