package fr.insee.eno.preprocessing;

import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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.transform.xsl.XslTransformation;

/**
 * A DDI specific preprocessor.
 */
public class DDISplittingPreprocessor  {

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

	private XslTransformation saxonService = new XslTransformation();

	public List<File> splitDDI(File inputFile, String survey) throws Exception {
		logger.info("DDI splitting preprocessing Target : START");

		String sUB_TEMP_FOLDER = Constants.sUB_TEMP_FOLDER(survey);
		// ----- Dereferencing
		logger.debug("Dereferencing : -Input : " + inputFile + " -Output : " + Constants.tEMP_NULL_TMP(sUB_TEMP_FOLDER)
		+ " -Stylesheet : " + Constants.UTIL_DDI_SPLITTING_XSL + " -Parameters : " + sUB_TEMP_FOLDER);

		InputStream isDDI_DEREFERENCING_XSL = Constants.getInputStreamFromPath(Constants.UTIL_DDI_SPLITTING_XSL);
		InputStream isInputFile = FileUtils.openInputStream(inputFile);
		OutputStream osTEMP_NULL_TMP = FileUtils.openOutputStream(Constants.tEMP_NULL_TMP(sUB_TEMP_FOLDER));
		
		try {
			saxonService.transformDereferencing(isInputFile, isDDI_DEREFERENCING_XSL, osTEMP_NULL_TMP,
					Constants.sUB_TEMP_FOLDER_FILE(survey));
		}catch(Exception e) {
			throw new EnoGenerationException("An error was occured during the " + toString() + " transformation. "+e.getMessage());
		}

		isInputFile.close();
		isDDI_DEREFERENCING_XSL.close();
		osTEMP_NULL_TMP.close();
		// ----- Cleaning
		logger.debug("Cleaning target");
		File f = Constants.sUB_TEMP_FOLDER_FILE(survey);
		File[] matchCleaningInput = f.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return !name.startsWith("null");
			}
		});

		List<File> outputFiles = new ArrayList<File>();
		String cleaningInput = null;

		logger.debug("Searching matching files in : " + sUB_TEMP_FOLDER);
		for (File file : matchCleaningInput) {
			if(!file.isDirectory()) {
				logger.debug("Found : " + file.getAbsolutePath());
				outputFiles.add(file);
			}
		}
		if(outputFiles.size()==0) {
			throw new EnoGenerationException("DDI Splitting produced no file.");
		}

		logger.debug("DDI splitting preprocessing : END");
		return outputFiles;
	}

	public String toString() {
		return "DDI splitting preprocessor";
	}


}

@@ -539,22 +539,22 @@
  Logger 
-logger
+LOGGER
  = Logge
@@ -754,22 +754,22 @@
 ion %7B%0A%09%09
-logger
+LOGGER
 .info(%22D
@@ -899,30 +899,30 @@
 ferencing%0A%09%09
-logger
+LOGGER
 .debug(%22Dere
@@ -1800,22 +1800,22 @@
 aning%0A%09%09
-logger
+LOGGER
 .debug(%22
@@ -2116,48 +2116,17 @@
 ();%0A
-%09%09String cleaningInput = null;%0A%0A%09%09logger
+%0A%09%09LOGGER
 .deb
@@ -2255,22 +2255,22 @@
 ) %7B%0A%09%09%09%09
-logger
+LOGGER
 .debug(%22
@@ -2455,14 +2455,14 @@
 %0A%0A%09%09
-logger
+LOGGER
 .deb

Update DDISplittingPreprocessor.java