package com.semmle.util.trap.pathtransformers;

import java.io.File;

import com.semmle.util.files.FileUtil;
import com.semmle.util.process.Env;
import com.semmle.util.process.Env.Var;

public abstract class PathTransformer {
	public abstract String transform(String input);

	/**
	 * Convert a file to its path in the (code) database. Turns file paths into
	 * canonical, absolute, strings and normalises away Unix/Windows differences.
	 */
	public String fileAsDatabaseString(File file) {
		String path;
		if (Boolean.valueOf(Env.systemEnv().get(Var.SEMMLE_PRESERVE_SYMLINKS)))
			path = FileUtil.simplifyPath(file);
		else
			path = FileUtil.tryMakeCanonical(file).getPath();
		return transform(FileUtil.normalisePath(path));
	}

	/**
	 * Utility method for extractors: Canonicalise the given path as required
	 * for the current extraction. Unlike {@link FileUtil#tryMakeCanonical(File)},
	 * this method is consistent with {@link #fileAsDatabaseString(File)}.
	 */
	public File canonicalFile(String path) {
		return new File(fileAsDatabaseString(new File(path)));
	}

	private static final PathTransformer DEFAULT_TRANSFORMER;
	static {
		String layout = Env.systemEnv().get(Var.SEMMLE_PATH_TRANSFORMER);
		if (layout == null)
			DEFAULT_TRANSFORMER = new NoopTransformer();
		else
			DEFAULT_TRANSFORMER = new ProjectLayoutTransformer(new File(layout));
	}

	public static PathTransformer std() {
		return DEFAULT_TRANSFORMER;
	}
}
@@ -497,17 +497,395 @@
 ing path
-;
+ = file.getPath();%0A%09%09// For a /!unknown-binary-location/... path, on Windows%0A%09%09// the standard code wants to normalise it to%0A%09%09// C:/!unknown-binary-location/...%0A%09%09// which is particularly annoying for cross-platform test%0A%09%09// output. We therefore handle it specially here.%0A%09%09if (path.matches(%22%5E%5B/%5C%5C%5C%5C%5D!unknown-binary-location%5B/%5C%5C%5C%5C%5D.*%22)) %7B%0A%09%09%09return path.replace('%5C%5C', '/');%0A%09%09%7D
 %0A%09%09if (B
@@ -1808,8 +1808,9 @@
 ER;%0A%09%7D%0A%7D
+%0A

Handle /!unknown-binary-location/... paths specially on Windows