Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce robust URL to java.nio.file.Path conversion #691

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HannesWell
Copy link
Member

Converting a URL into a Path or File is not a trivial task, especially since URLs are often malformed and therefore the straight forward way Path.of(url.toURI()) or new File(url.toURI()) lead to a URISyntaxException being thrown. Therefore multiple different workarounds are used throughout the SDK, e.g. Path.of(url.getPath()) or Path.of(url.getFile()), but they are again have their problems.

This PR suggest a new utility method in the URIUtil that is capable of all the different conversion methods and workarounds and has the goal to reliably convert file-URLs into java.nio.file.Paths regardless of if the URL is malformed or perfectly complying to the standard.

This could also help for eclipse-platform/eclipse.platform#35 because one does not have to rely on malformed URLs anymore if they are just passed around internally (URLs displayed to the user are another topic).

*
* @since 3.20
*/
public static Path toFilePath(URL url) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That the name contains File and Path seems to be a duplication at first, but since any kind of URL can have a path I think it should be made clear that it's a file-system path (although nio-Path can also handle non-classical file-systems).
On the other hand toFileSystemPath() seemed to be a bit verbose to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only food for thought, this suggestion isn't common convention. Could use Path fromFileUrl(URL url).
Cannot say I like that, but it seems to make it clear.

Comment on lines +342 to +344
if (!SCHEME_FILE.equals(url.getProtocol())) {
throw new IllegalArgumentException("Not a 'file' url: " + url); //$NON-NLS-1$
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively this method could return an empty optional if the URL is not a file-URL.
But since callers that are able to handle other protocols can simple check the precondition before calling this method, I think it's fine to keep it simple for those cases where a file-URL is used with certainty.

Copy link

Test Results

0 files   -   660  0 suites   - 660   0s ⏱️ - 1h 16m 40s
0 tests  - 2 201  0 ✅  - 2 154  0 💤  -  47  0 ❌ ±0 
0 runs   - 6 747  0 ✅  - 6 604  0 💤  - 143  0 ❌ ±0 

Results for commit d9b3e1d. ± Comparison against base commit 9ee49aa.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants