package org.pescuma.buildhealth.extractor;

import static org.apache.commons.io.FileUtils.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class PseudoFiles {
	
	private final List<File> files = new ArrayList<File>();
	private final InputStream stream;
	private final String streamFilename;
	
	public PseudoFiles(InputStream stream) {
		this(stream, null);
	}
	
	public PseudoFiles(InputStream stream, String streamFilename) {
		this.stream = stream;
		this.streamFilename = streamFilename;
	}
	
	public PseudoFiles(File file) {
		this.files.add(file);
		this.stream = null;
		this.streamFilename = null;
	}
	
	public PseudoFiles(List<File> files) {
		this.files.addAll(files);
		this.stream = null;
		this.streamFilename = null;
	}
	
	public boolean isStream() {
		return stream != null;
	}
	
	public InputStream getStream() {
		return stream;
	}
	
	public String getStreamFilename() {
		return streamFilename;
	}
	
	public Collection<File> getFiles(String... validExtensions) {
		if (files.isEmpty())
			return Collections.emptyList();
		
		List<File> result = new ArrayList<File>();
		
		for (File file : files) {
			if (file.isDirectory()) {
				if (validExtensions.length > 0)
					result.addAll(listFiles(file, validExtensions, true));
				else
					result.addAll(listFiles(file, null, true));
				
			} else {
				if (file.exists())
					result.add(file);
			}
		}
		
		// Make all canonical
		for (int i = 0; i < result.size(); i++)
			result.set(i, getCanonicalFile(result.get(i)));
		
		return result;
	}
	
	private File getCanonicalFile(File file) {
		try {
			return file.getCanonicalFile();
		} catch (IOException e) {
			return file.getAbsoluteFile();
		}
	}
}

@@ -715,26 +715,39 @@
 files.add(fi
-le
+xFileName(file)
 );%0D%0A%09%09this.s
@@ -836,24 +836,52 @@
 %3E files) %7B%0D%0A
+%09%09for (File file : files)%0D%0A%09
 %09%09this.files
@@ -888,17 +888,26 @@
 .add
-All
+(fixFileName
 (file
-s
+)
 );%0D%0A
@@ -959,32 +959,351 @@
 = null;%0D%0A%09%7D%0D%0A%09%0D%0A
+%09private File fixFileName(File file) %7B%0D%0A%09%09String name = file.getPath();%0D%0A%09%09if (name.length() %3E 2 //%0D%0A%09%09%09%09&& ((name.startsWith(%22'%22) && name.endsWith(%22'%22)) //%0D%0A%09%09%09%09%7C%7C (name.startsWith(%22%5C%22%22) && name.endsWith(%22%5C%22%22))))%0D%0A%09%09%09file = new File(name.substring(1, name.length() - 1));%0D%0A%09%09%0D%0A%09%09return file.getAbsoluteFile();%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public boolean 

Remove quotes from filenames (relative to #3)