Bug 59311 - Add FileUtil methods to get URLs for archives and folders
Add FileUtil methods to get URLs for archives and folders
Status: RESOLVED FIXED
Product: platform
Classification: Unclassified
Component: Filesystems
5.x
All All
: P1 (vote)
: 6.x
Assigned To: apireviews
issues@platform
: API, API_REVIEW_FAST
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2005-05-26 21:50 UTC by Jesse Glick
Modified: 2008-12-22 11:41 UTC (History)
4 users (show)

See Also:
Issue Type: ENHANCEMENT
:


Attachments
Proposed patch (49.22 KB, patch)
2008-03-10 23:32 UTC, Jesse Glick
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jesse Glick 2005-05-26 21:50:45 UTC
It is quite common in project system code to have a java.io.File which
represents either an archive or a folder (a classpath entry) and wish to get a
URL representing this entry.

You can use existing FileUtil methods, but it is slightly tricky; you need to
check a few special conditions.

This code is duplicated all over the place. Just grep the code base for
File.toURI and you will find plenty of examples. See e.g.
apisupport/project/src/org/netbeans/modules/apisupport/project/Util.java and the
urlFor* methods.

Suggest new static convenience methods to do these things right, in one place.
Would mean going thru apireviews, adding the methods, and doing a search over
the whole code base for usages of these idioms that could be replaced with the
standard method.

There is sometimes the reverse operation: getting a File from a URL which might
be a file: or jar: URL. The thing which people writing this code usually forget
is to check for a jar: URL whether there is anything after "!/"; if so, you
generally *don't* want to convert it back to a *.jar File (because you will be
losing the suffix).

Might also want methods to convert classpaths between URL[] form (as used in
ClassPath) and string form (as e.g. "/path/dir:/path/archive.jar"). I think this
kind of operation is done in a few more places.

Not sure where all this belongs. FileUtil? PropertyUtils in ant/project?
ClassPathSupport in java/api?
Comment 1 Tomas Zezula 2005-05-27 08:08:14 UTC
I vote for the FileUtil, there are already the getArchiveRoot and the
getArchiveFile methods.
Comment 2 Jesse Glick 2005-10-31 18:09:34 UTC
Re. "convert classpaths between URL[] form and string form" - and/or convert
between File (as used in -classpath) and URL. Examples:

File("/tmp/dir") -> file:/tmp/dir/
File("/tmp/archive.jar") -> jar:file:/tmp/archive.jar!/
file:/tmp/dir/ -> File("/tmp/dir")
jar:file:/tmp/archive.jar!/ -> File("/tmp/archive.jar")

and the ones that people usually forget to check for, resulting in the
occasional error:

File("/tmp/nonexistent-dir") -> file:/tmp/nonexistent-dir/ [need final '/']
jar:file:/tmp/archive.jar -> null [missing JAR path; or assume "!/" anyway?]
jar:file:/tmp/archive.jar!/subpath/ -> null [not a root]
jar:jar:file:/tmp/archive.jar!/subjar.jar!/ -> null [nested not permitted]
http://nowhere.net/ -> null [remote]
jar:http://nowhere.net/archive.jar!/ -> null [remote]

Converting String -> URL[] would require PropertyUtils.tokenizePath or its
equivalent to correctly handle ':' as a drive letter marker on Windows rather
than a path separator (assuming the method permitted either ':' or ';' on any
platform, as Ant does).
Comment 3 Tomas Zezula 2005-11-01 09:58:06 UTC
There should be probably a method on the ClassPathSupport creating the classpath
from the string:
ClassPathSupport.fromJVMPath (String path);

and a method on the ClassPath creating the string representation of the classpath:
ClassPath.toJVMPath()
Comment 4 Jesse Glick 2006-07-15 16:07:25 UTC
This is important. People don't know how to do it right and wind up doing it
wrong. And similar but slightly different code is scattered all over the place.

For the ClassPath -> String conversion I would suggest a mode parameter which
would control behavior on untranslatable entries (e.g. jar:file:/tmp/src.zip!/src/):

enum Mode {
SKIP - such entries are silently dropped
WARN - a log warning is issued
FAIL - an IAE is thrown
PRINT - the URL.toString is included (useful for debugging and unit tests)
}
Comment 5 Jesse Glick 2008-03-07 23:49:46 UTC
I am working on a patch for this, will submit soon.
Comment 6 Jesse Glick 2008-03-10 23:32:04 UTC
Created attachment 58104 [details]
Proposed patch
Comment 7 Jesse Glick 2008-03-10 23:33:06 UTC
Please review. Patch contains API changes, and usage from modules I suspected needed the new APIs.
Comment 8 Tomas Zezula 2008-03-11 19:10:02 UTC
Both API (filesystems & java api) seems good.


Comment 9 Milos Kleint 2008-03-12 10:25:17 UTC
+1 from me, api looks fine.
Comment 10 Jesse Glick 2008-03-17 21:52:09 UTC
Also used it in java.freeform. #4c8b2e081042


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo