Skip to content

Commit

Permalink
Merge pull request #1810 from usethesource/canonical-file-paths
Browse files Browse the repository at this point in the history
Make a canonical file path if we are asked to build a new file path
  • Loading branch information
jurgenvinju authored Jun 19, 2023
2 parents 2331fec + 00d2dda commit 7c92455
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.List;

import org.rascalmpl.interpreter.utils.RascalManifest;
import org.rascalmpl.uri.URIUtil;

import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValueFactory;
import org.rascalmpl.values.ValueFactoryFactory;
Expand Down Expand Up @@ -54,7 +56,7 @@ public void contributePaths(List<ISourceLocation> l) {
}
}
else {
l.add(vf.sourceLocation("file","", path));
l.add(URIUtil.createFileLocation(path));
}
} catch (URISyntaxException e) {
}
Expand Down
38 changes: 21 additions & 17 deletions src/org/rascalmpl/library/lang/json/internal/JsonValueReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,26 +269,30 @@ private IValue sourceLocationObject() throws IOException {
}

in.endObject();

if (path != null && offset != -1 && length != -1 && beginLine != -1 && endLine != -1 && beginColumn != -1 && endColumn != -1) {
return vf.sourceLocation(path, offset, length, beginLine, endLine, beginColumn, endColumn);
}
try {
if (scheme != null && authority != null && query != null && fragment != null) {
return vf.sourceLocation(scheme, authority, path, query, fragment);
}
if (scheme != null) {
return vf.sourceLocation(scheme, authority == null ? "" : authority, path);
}
ISourceLocation root;
if (scheme != null && authority != null && query != null && fragment != null) {
root = vf.sourceLocation(scheme, authority, path, query, fragment);
}
else if (scheme != null) {
root = vf.sourceLocation(scheme, authority == null ? "" : authority, path);
}
else if (path != null) {
root = URIUtil.createFileLocation(path);
}
else {
throw new IOException("Could not parse complete source location: " + in.getPath());
}
if (offset != -1 && length != -1 && beginLine != -1 && endLine != -1 && beginColumn != -1 && endColumn != -1) {
return vf.sourceLocation(root, offset, length, beginLine, endLine, beginColumn, endColumn);
}
if (offset != -1 && length != -1) {
return vf.sourceLocation(root, offset, length);
}
return root;
} catch (URISyntaxException e) {
throw new IOException(e);
}

if (path != null) {
return vf.sourceLocation(path);
}

throw new IOException("Could not parse complete source location: " + in.getPath());
}

@Override
Expand Down Expand Up @@ -331,7 +335,7 @@ else if (val.contains("://")) {
}
else {
// will be simple interpreted as an absolute file name
return vf.sourceLocation(val);
return URIUtil.createFileLocation(val);
}
} catch (URISyntaxException e) {
throw new IOException("could not parse URI:" + in.getPath() + " due to " + e.getMessage(), e);
Expand Down
11 changes: 9 additions & 2 deletions src/org/rascalmpl/uri/URIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.rascalmpl.uri;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -55,8 +56,14 @@ public static URI create(String scheme, String userInformation, String host, int
* @throws URISyntaxException
*/
public static URI createFile(String path) throws URISyntaxException {
path = fixWindowsPath(path);
return fixUnicode(new File(path).toURI());
File file = new File(fixWindowsPath(path));
try {
file = file.getCanonicalFile();
}
catch (IOException e) {
// swallow, let's keep the old file
}
return fixUnicode(file.toURI());
}

/**
Expand Down

0 comments on commit 7c92455

Please sign in to comment.