From bce9fe013a8838393971f990e13f8fc38404a629 Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Mon, 24 Apr 2006 14:15:52 +0000 Subject: [PATCH] Bugzilla Report: 39295 Reporter: Waldek Herka extract the fileToURL code use it for tools jar as well as the other paths git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@396563 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + WHATSNEW | 3 ++ .../org/apache/tools/ant/launch/Launcher.java | 8 +--- .../org/apache/tools/ant/launch/Locator.java | 44 ++++++++++--------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 1f4e7a3dc..12fcf633b 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -231,6 +231,7 @@ Tom Ball Tom Dimock Tom Eugelink Ulrich Schmidt +Waldek Herka Will Wang William Ferguson Wolfgang Werner diff --git a/WHATSNEW b/WHATSNEW index e985c7466..b07033ccc 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -397,6 +397,9 @@ Other changes: attribute will read default values from the properties ant.build.javac.source and ant.build.javac.target. +* Handling of ' ', '#' in CLASSPATH and '#' in -lib (cannot use ' ' + in -lib on UNIX at the moment). Bugzilla Report 39295. + Changes from Ant 1.6.4 to Ant 1.6.5 =================================== diff --git a/src/main/org/apache/tools/ant/launch/Launcher.java b/src/main/org/apache/tools/ant/launch/Launcher.java index 0d554a10a..4d50e3bd5 100644 --- a/src/main/org/apache/tools/ant/launch/Launcher.java +++ b/src/main/org/apache/tools/ant/launch/Launcher.java @@ -130,11 +130,7 @@ public class Launcher { } } - try { - libPathURLs.add(new URL(Locator.encodeURI(element.toURL().toString()))); - } catch (UnsupportedEncodingException ex) { - throw new MalformedURLException(ex.toString()); - } + libPathURLs.add(Locator.fileToURL(element)); } } @@ -255,7 +251,7 @@ public class Launcher { systemJars.length); if (toolsJar != null) { - jars[jars.length - 1] = toolsJar.toURL(); + jars[jars.length - 1] = Locator.fileToURL(toolsJar); } diff --git a/src/main/org/apache/tools/ant/launch/Locator.java b/src/main/org/apache/tools/ant/launch/Locator.java index 27810ceeb..635f60f8f 100644 --- a/src/main/org/apache/tools/ant/launch/Locator.java +++ b/src/main/org/apache/tools/ant/launch/Locator.java @@ -310,6 +310,28 @@ public final class Locator { return sb == null ? path : sb.toString(); } + /** + * Convert a File to a URL. + * File.toURL() does not encode characters like #. + * File.toURI() has been introduced in java 1.4, so + * ANT cannot use it (except by reflection) + * FileUtils.toURI() cannot be used by Locator.java + * Implemented this way. + * File.toURL() adds file: and changes '\' to '/' for dos OSes + * encodeURI converts characters like ' ' and '#' to %DD + * @param file the file to convert + * @return URL the converted File + * @throws MalformedURLException on error + */ + public static URL fileToURL(File file) + throws MalformedURLException { + try { + return new URL(encodeURI(file.toURL().toString())); + } catch (UnsupportedEncodingException ex) { + throw new MalformedURLException(ex.toString()); + } + } + /** * Get the File necessary to load the Sun compiler tools. If the classes * are available to this class, then no additional URL is required and @@ -401,20 +423,7 @@ public final class Locator { String path = location.getPath(); for (int i = 0; i < extensions.length; ++i) { if (path.toLowerCase().endsWith(extensions[i])) { - try { - /** - * File.toURL() does not encode characters like #. - * File.toURI() has been introduced in java 1.4, so - * ANT cannot use it (except by reflection) - * FileUtils.toURI() cannot be used by Locator.java - * Implemented this way. - * File.toURL() adds file: and changes '\' to '/' for dos OSes - * encodeURI converts characters like ' ' and '#' to %DD - */ - urls[0] = new URL(encodeURI(location.toURL().toString())); - } catch (UnsupportedEncodingException ex) { - throw new MalformedURLException(ex.toString()); - } + urls[0] = fileToURL(location); break; } } @@ -433,12 +442,7 @@ public final class Locator { }); urls = new URL[matches.length]; for (int i = 0; i < matches.length; ++i) { - try { - // See comments above. - urls[i] = new URL(encodeURI(matches[i].toURL().toString())); - } catch (UnsupportedEncodingException ex) { - throw new MalformedURLException(ex.toString()); - } + urls[i] = fileToURL(matches[i]); } return urls; }