package org.icij.extract;

import java.util.Iterator;
import java.util.logging.Logger;

import java.io.OutputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.file.Path;
import java.nio.charset.Charset;

import org.apache.tika.parser.ParsingReader;

import org.apache.commons.io.IOUtils;

/**
 * Extract
 *
 * @author Matthew Caruana Galizia <mcaruana@icij.org>
 * @version 1.0.0-beta
 * @since 1.0.0-beta
 */
public class FileSpewer extends Spewer {
	private final Logger logger;

	private Path outputDirectory;

	public FileSpewer(Logger logger) {
		this.logger = logger;
	}

	public void setOutputDirectory(Path outputDirectory) {
		this.outputDirectory = outputDirectory;
	}

	public void write(Path file, ParsingReader reader, Charset outputEncoding) throws IOException {
		int i = 0;
		Path outputFile = null;

		// Remove the common root of the file path.
		// If the file is at /path/to/some/random/file and the output directory is /path/to/output, then the resulting text is outputted to /path/to/output/some/random/file.
		if (outputDirectory.isAbsolute() && file.isAbsolute()) {
			final Iterator iterator = outputDirectory.iterator();

			while (iterator.hasNext()) {
				Object segment = iterator.next();

				if (!segment.equals(file.getName(i))) {
					break;
				}

				i++;
			}
		}

		if (i < 1) {
			i = 1;
		}

		outputFile = outputDirectory.resolve(file.subpath(i, file.getNameCount()));

		if (outputFile.equals(file)) {
			throw new IllegalArgumentException("Output file cannot be the same as the input file.");
		}

		logger.info("Outputting to file: " + outputFile + ".");

		// Make the required directories.
		boolean madeDirs = outputFile.getParent().toFile().mkdirs();
		if (true != madeDirs) {
			throw new RuntimeException("Unable to make directories for file: " + outputFile + ".");
		}

		final OutputStream outputStream = new FileOutputStream(outputFile.toFile());

		try {
			IOUtils.copy(reader, outputStream, outputEncoding);
		} catch (IOException e) {
			throw e;
		} finally {
			outputStream.close();
		}
	}
}

@@ -542,16 +542,57 @@
 rectory;
+%0A%09private String outputExtension = %22txt%22;
 %0A%0A%09publi
@@ -747,24 +747,116 @@
 ectory;%0A%09%7D%0A%0A
+%09public void setOutputExtension(String extension) %7B%0A%09%09this.outputExtension = extension;%0A%09%7D%0A%0A
 %09public void
@@ -1576,137 +1576,105 @@
 ));%0A
-%0A
 %09%09
-if (outputFile.equals(file)) %7B%0A%09%09%09throw new IllegalArgumentException(%22O
+outputFile = outputFile.getFileSystem().getPath(o
 utput
- file cannot be the same as the input file.%22);%0A%09%09%7D
+File.toString() + %22.%22 + outputExtension);
 %0A%0A%09%09

Add extension to extracted files