Skip to content

Commit

Permalink
[lang] Fixing invalid Windows path computation.
Browse files Browse the repository at this point in the history
see #504

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Oct 10, 2016
1 parent 6d97f79 commit 41fa6d3
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 34 deletions.
Expand Up @@ -75,6 +75,10 @@ public class Messages extends NLS {
public static String SarlBatchCompiler_29;
public static String SarlBatchCompiler_3;
public static String SarlBatchCompiler_30;
public static String SarlBatchCompiler_31;
public static String SarlBatchCompiler_32;
public static String SarlBatchCompiler_33;
public static String SarlBatchCompiler_34;
public static String SarlBatchCompiler_4;
public static String SarlBatchCompiler_5;
public static String SarlBatchCompiler_6;
Expand Down
Expand Up @@ -157,7 +157,7 @@ public boolean accept(File pathname) {

private boolean useCurrentClassLoaderAsParent;

private org.eclipse.emf.common.util.URI baseURI;
private org.eclipse.emf.common.util.URI baseUri;

private FileProjectConfig projectConfig;

Expand Down Expand Up @@ -403,7 +403,7 @@ public void setBasePath(String basePath) {
* @param basePath the base path.
*/
public void setBaseURI(org.eclipse.emf.common.util.URI basePath) {
this.baseURI = basePath;
this.baseUri = basePath;
}

/** Change the path where the Java files are generated.
Expand Down Expand Up @@ -1410,44 +1410,79 @@ private static boolean isContainedIn(File child, File possibleParent) {
return false;
}

private File determineCommonRoot(File outputFile, List<File> sourceFileList) {
if (this.baseURI != null && this.baseURI.isFile()) {
return new File(this.baseURI.toFileString());
private static LinkedList<String> splitFile(File file, CancelIndicator cancelIndicator) {
assert cancelIndicator != null;
final LinkedList<String> elements = new LinkedList<>();
File current = file;
do {
if (cancelIndicator.isCanceled()) {
return null;
}
elements.addFirst(current.getName());
current = current.getParentFile();
} while (current != null);
return elements;
}

@SuppressWarnings({"checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"})
private File determineCommonRoot(File outputFile, List<File> sourceFileList, CancelIndicator cancelIndicator) {
assert cancelIndicator != null;

if (this.baseUri != null) {
if (this.baseUri.isFile()) {
this.log.debug(MessageFormat.format(Messages.SarlBatchCompiler_32, this.baseUri));
return new File(this.baseUri.toFileString());
}
this.log.debug(MessageFormat.format(Messages.SarlBatchCompiler_33, this.baseUri));
}
final List<File> pathList = new ArrayList<>(sourceFileList);
pathList.add(outputFile);

final List<List<File>> pathParts = new ArrayList<>();
LinkedList<String> longuestPrefix = null;

for (final File path : pathList) {
final List<File> partsList = new ArrayList<>();
File subdir = path;
while (subdir != null) {
partsList.add(subdir);
subdir = subdir.getParentFile();
for (final File file : Iterables.concat(sourceFileList, Collections.singleton(outputFile))) {
if (cancelIndicator.isCanceled()) {
return null;
}
pathParts.add(partsList);
}
int i = 1;
File result = null;
while (true) {
File compareWith = null;
for (final List<File> parts : pathParts) {
if (parts.size() < i) {
return result;
final LinkedList<String> components = splitFile(file, cancelIndicator);
if (longuestPrefix == null) {
longuestPrefix = components;
} else {
int i = 0;
while (i < longuestPrefix.size() && i < components.size()
&& Strings.equal(longuestPrefix.get(i), components.get(i))) {
if (cancelIndicator.isCanceled()) {
return null;
}
++i;
}
final File part = parts.get(parts.size() - i);
if (compareWith == null) {
compareWith = part;
} else {
if (!compareWith.equals(part)) {
return result;
while (i < longuestPrefix.size()) {
if (cancelIndicator.isCanceled()) {
return null;
}
longuestPrefix.removeLast();
}
if (longuestPrefix.isEmpty()) {
return null;
}
}
result = compareWith;
i++;
}

if (longuestPrefix == null || cancelIndicator.isCanceled()) {
return null;
}

File prefix = null;
for (final String component : longuestPrefix) {
if (cancelIndicator.isCanceled()) {
return null;
}
if (prefix == null) {
prefix = new File(component);
} else {
prefix = new File(prefix, component);
}
}

return prefix;
}

@SuppressWarnings({"checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"})
Expand All @@ -1459,13 +1494,15 @@ private boolean configureWorkspace(ResourceSet resourceSet, CancelIndicator canc
return false;
}

final File commonRoot = determineCommonRoot(outputFile, sourceFolders);
this.log.debug(MessageFormat.format(Messages.SarlBatchCompiler_31, this.baseUri));

final File commonRoot = determineCommonRoot(outputFile, sourceFolders, cancelIndicator);
if (cancelIndicator.isCanceled()) {
return false;
}

// We don't want to use root ("/") as a workspace folder, didn't we?
if (commonRoot == null || commonRoot.getParent() == null || commonRoot.getParentFile().getParent() == null) {
this.log.debug(MessageFormat.format(Messages.SarlBatchCompiler_34, commonRoot));
if (commonRoot == null) {
this.log.error(Messages.SarlBatchCompiler_12);
for (final File sourceFile : sourceFolders) {
this.log.error(MessageFormat.format(Messages.SarlBatchCompiler_13, sourceFile));
Expand Down
Expand Up @@ -40,6 +40,10 @@ SarlBatchCompiler_28=Saving the Java models elements into {0}
SarlBatchCompiler_29=Source folders: {0}
SarlBatchCompiler_3=Compilation of Java code against stubs had errors. This is expected and usually is not a probblem.
SarlBatchCompiler_30=Classpath: {0}
SarlBatchCompiler_31=Base URI: {0}
SarlBatchCompiler_32=File Base URI detected: {0}
SarlBatchCompiler_33=Base URI ignored (not file URI): {0}
SarlBatchCompiler_34=Common folder: {0}
SarlBatchCompiler_4={0}: \t{1} - {2}\n{3}: {4}
SarlBatchCompiler_5={0}: \t{1}: {2}
SarlBatchCompiler_6=invoke batch compiler with ''{0}''
Expand Down

0 comments on commit 41fa6d3

Please sign in to comment.