Skip to content

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm torn here as well. Currently the URIUtil class has both kind of methods: toXYZ() and fromXYZ().
On one hand, it's clear from the method signature that a file-system Path is returned so we could reflect the file-URL requirement in the name. On the other hand, since this class is named URIUtil, just the method name fromFileUrl() could imply that a URI is created (which is indeed the case for the fromString() method).

A 'full' name could be filePathFromFileURL() or just pathFromFileURL() but I fear the latter misses the 'file-system' aspect and both are quite lengthy.

Maybe a different style like fileURLToPath() would also fit?

Comment on lines +342 to +364
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
Member

Choose a reason for hiding this comment

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

Optional seem much nicer, calles can throw exception if the want anyways with Optional.orElseThrow(...)

Then we can easily handel other error conditions here as well for example if invalid characters are in the path.

Copy link
Member Author

Choose a reason for hiding this comment

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

In some cases it's indeed nicer, but there are many cases where the callers have already checked or are sure that it's a file-URL and then it's more cumbersome.
Furthermore, if we check more error cases where using an optional erases detailed error information as one just gets an empty optional without more context. Using exceptions instead allows us to add more details.

@github-actions
Copy link

github-actions bot commented Oct 19, 2024

Test Results

0 files   -   669  0 suites   - 669   0s ⏱️ - 1h 13m 21s
0 tests  - 2 223  0 ✅  - 2 175  0 💤  -  47  0 ❌  - 1 
0 runs   - 6 813  0 ✅  - 6 669  0 💤  - 143  0 ❌  - 1 

Results for commit b823f1d. ± Comparison against base commit 7bdfa2a.

♻️ This comment has been updated with latest results.

@laeubi
Copy link
Member

laeubi commented Mar 31, 2025

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.

Why not have a new PathUtil then if its not about URI...? Or put it into FileLocator, as we already have public static URL toFileURL(URL url) throws IOException there...

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.

3 participants