Bug 389564 - Fix jar path urls for shared eclipse installations

Change-Id: I4977c0133a50016e1d2733f830b3e6056c837ceb
Signed-off-by: Julian Enoch <julian.enoch@ericsson.com>
diff --git a/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java b/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java
index 35191b2..7c1997d 100644
--- a/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java
+++ b/ant/org.eclipse.ant.core/src/org/eclipse/ant/core/AntCorePreferences.java
@@ -8,6 +8,8 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *     Thierry Lach (thierry.lach@bbdodetroit.com) - bug 40502
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
+ *     Ericsson AB, Julian Enoch - Bug 389564
  *******************************************************************************/
 package org.eclipse.ant.core;
 
@@ -15,6 +17,7 @@
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -42,6 +45,7 @@
 import org.eclipse.core.runtime.Preferences;
 import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
@@ -677,14 +681,22 @@
 	 * Create a "file:" URL for the specified File making sure the URL ends with a slash if the File denotes a directory.
 	 */
 	private URL getClasspathEntryURL(Bundle bundle, String library) throws IOException {
-		File urlFile;
+		File urlFile = null;
 		if (library.equals("/")) { //$NON-NLS-1$
 			urlFile = FileLocator.getBundleFile(bundle);
 		} else {
-			urlFile = new File(FileLocator.toFileURL(bundle.getEntry(library)).getPath());
+			try {
+				URL fileURL = FileLocator.toFileURL(bundle.getEntry(library));
+				urlFile = URIUtil.toFile(URIUtil.toURI(fileURL));
+			}
+			catch (URISyntaxException e) {
+				AntCorePlugin.log(e);
+			}
 		}
-		if (!urlFile.exists())
+
+		if (urlFile == null || !urlFile.exists())
 			return null;
+
 		String path = urlFile.getAbsolutePath();
 		return new URL(IAntCoreConstants.FILE_PROTOCOL + (urlFile.isDirectory() ? path + "/" : path)); //$NON-NLS-1$
 	}
@@ -1908,4 +1920,4 @@
 	public IAntClasspathEntry[] getContributedClasspathEntries() {
 		return extraClasspathURLs.toArray(new IAntClasspathEntry[extraClasspathURLs.size()]);
 	}
-}
\ No newline at end of file
+}
diff --git a/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntClasspathEntry.java b/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntClasspathEntry.java
index 3156502..3dc8940 100644
--- a/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntClasspathEntry.java
+++ b/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntClasspathEntry.java
@@ -7,16 +7,22 @@
  * 
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
+ *     Ericsson AB, Julian Enoch - Bug 389564
  *******************************************************************************/
 
 package org.eclipse.ant.internal.core;
 
-import java.io.File;
+import java.io.IOException;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.net.URL;
+
 import org.eclipse.ant.core.AntCorePlugin;
 import org.eclipse.ant.core.IAntClasspathEntry;
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.core.variables.VariablesPlugin;
 
 public class AntClasspathEntry implements IAntClasspathEntry {
@@ -70,7 +76,13 @@
 
 	public AntClasspathEntry(URL url) {
 		this.url = url;
-		this.entryString = new File(url.getPath()).getAbsolutePath();
+		try {
+			URL fileURL = FileLocator.toFileURL(url);
+			this.entryString = (URIUtil.toFile(URIUtil.toURI(fileURL))).getAbsolutePath();
+		}
+		catch (URISyntaxException | IOException e) {
+			AntCorePlugin.log(e);
+		}
 	}
 
 	/*
@@ -121,4 +133,4 @@
 	public void setEclipseRuntimeRequired(boolean eclipseRequired) {
 		this.eclipseRequired = eclipseRequired;
 	}
-}
\ No newline at end of file
+}
diff --git a/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntPropertyValueProvider.java b/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntPropertyValueProvider.java
index 5d0100c..97901ce 100644
--- a/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntPropertyValueProvider.java
+++ b/ant/org.eclipse.ant.core/src/org/eclipse/ant/internal/core/AntPropertyValueProvider.java
@@ -8,15 +8,17 @@
  * Contributors:
  *     Thierry Lach (thierry.lach@bbdodetroit.com) - initial API and implementation for bug 40502
  *     IBM Corporation - added eclipse.running property, bug 65655
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
+ *     Ericsson AB, Julian Enoch - Bug 389564
  *******************************************************************************/
 package org.eclipse.ant.internal.core;
 
-import java.io.File;
 import java.net.URL;
 
 import org.eclipse.ant.core.AntCorePlugin;
 import org.eclipse.ant.core.IAntPropertyValueProvider;
 import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.URIUtil;
 
 /**
  * Dynamic provider for Ant properties.
@@ -49,7 +51,8 @@
 			return "true"; //$NON-NLS-1$
 		} else if ("eclipse.home".equals(propertyName)) { //$NON-NLS-1$
 			try {
-				value = new File(FileLocator.resolve(new URL("platform:/base/")).getPath()).getAbsolutePath(); //$NON-NLS-1$
+				URL fileURL = FileLocator.toFileURL(new URL("platform:/base/")); //$NON-NLS-1$
+				value = (URIUtil.toFile(URIUtil.toURI(fileURL))).getAbsolutePath();
 				if (value.endsWith("/")) { //$NON-NLS-1$
 					value = value.substring(0, value.length() - 1);
 				}
@@ -60,4 +63,4 @@
 		}
 		return value;
 	}
-}
\ No newline at end of file
+}
diff --git a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
index c701553..6ff9686 100644
--- a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
+++ b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/AntLaunchDelegate.java
@@ -9,11 +9,13 @@
  *     IBM Corporation - initial API and implementation
  *     Juan A. Hernandez - bug 89926
  *     dakshinamurthy.karra@gmail.com - bug 165371
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
  *******************************************************************************/
 package org.eclipse.ant.internal.launching.launchConfigurations;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.Collections;
 import java.util.HashMap;
@@ -48,6 +50,7 @@
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.debug.core.DebugEvent;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.IBreakpointManager;
@@ -740,9 +743,14 @@
 			Bundle fragBundle = fragmentWires.get(0).getRequirer().getBundle();
 			try {
 				URL url = FileLocator.toFileURL(fragBundle.getEntry("/")); //$NON-NLS-1$
-				IPath path = new Path(url.getPath());
-				path = path.removeTrailingSeparator();
-				fgSWTLibraryLocation = path.toOSString();
+				try {
+					IPath path = new Path(URIUtil.toURL(URIUtil.toURI(url)).getPath());
+					path = path.removeTrailingSeparator();
+					fgSWTLibraryLocation = path.toOSString();
+				}
+				catch (URISyntaxException e) {
+					AntLaunching.log(e);
+				}
 			}
 			catch (IOException e) {
 				// do nothing
diff --git a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java
index ec7a0f5..5d02a60 100644
--- a/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java
+++ b/ant/org.eclipse.ant.launching/src/org/eclipse/ant/internal/launching/launchConfigurations/ContributedClasspathEntriesEntry.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
  *******************************************************************************/
 package org.eclipse.ant.internal.launching.launchConfigurations;
 
@@ -14,6 +15,7 @@
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -30,6 +32,7 @@
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.jdt.internal.launching.AbstractRuntimeClasspathEntry;
 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
@@ -148,7 +151,18 @@
 				if (install != null) {
 					IAntClasspathEntry entry = AntCorePlugin.getPlugin().getPreferences().getToolsJarEntry(new Path(install.getInstallLocation().getAbsolutePath()));
 					if (entry != null) {
-						rtes.add(JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(entry.getEntryURL().getPath())));
+						String pathString = null;
+						try {
+							pathString = URIUtil.toURL(URIUtil.toURI(entry.getEntryURL())).getPath();
+							rtes.add(JavaRuntime.newArchiveRuntimeClasspathEntry(new Path(pathString)));
+						}
+						catch (MalformedURLException e) {
+							AntLaunching.log(e);
+
+						}
+						catch (URISyntaxException e) {
+							AntLaunching.log(e);
+						}
 					}
 				}
 			}
@@ -177,7 +191,12 @@
 				String urlFileName = bundleURL.getFile();
 				if (urlFileName.startsWith(IAntCoreConstants.FILE_PROTOCOL)) {
 					try {
-						urlFileName = new URL(urlFileName).getFile();
+						try {
+							urlFileName = URIUtil.toURL(URIUtil.toURI(new URL(urlFileName))).getFile();
+						}
+						catch (URISyntaxException e) {
+							AntLaunching.log(e);
+						}
 						if (urlFileName.endsWith("!/")) { //$NON-NLS-1$
 							urlFileName = urlFileName.substring(0, urlFileName.length() - 2);
 						}
diff --git a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntDefiningTaskNode.java b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntDefiningTaskNode.java
index 1ee42d2..d283033 100644
--- a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntDefiningTaskNode.java
+++ b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntDefiningTaskNode.java
@@ -7,12 +7,14 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
  *******************************************************************************/
 
 package org.eclipse.ant.internal.ui.model;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Hashtable;
@@ -33,6 +35,7 @@
 import org.eclipse.ant.internal.ui.IAntUIConstants;
 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
 import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.xml.sax.Attributes;
@@ -154,7 +157,15 @@
 		File file = null;
 		for (int i = 0; i < antClasspath.length; i++) {
 			try {
-				file = new File(FileLocator.toFileURL(antClasspath[i]).getPath());
+				try {
+					URL thisURL = URIUtil.toURI(antClasspath[i]).toURL();
+					file = URIUtil.toFile(FileLocator.toFileURL(thisURL).toURI());
+				}
+				catch (URISyntaxException e) {
+					file = new File(FileLocator.toFileURL(antClasspath[i]).getPath());
+					AntUIPlugin.log(e);
+					e.printStackTrace();
+				}
 			}
 			catch (IOException e) {
 				continue;
diff --git a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntElementNode.java b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntElementNode.java
index b4d0a7d..930ce9a 100644
--- a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntElementNode.java
+++ b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntElementNode.java
@@ -11,12 +11,15 @@
  *     GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial API and implementation
  * 	   IBM Corporation - bug fixes
  *     John-Mason P. Shackelford (john-mason.shackelford@pearson.com) - bug 49445
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
+ *     Ericsson AB, Julian Enoch - Bug 389564
  *******************************************************************************/
 
 package org.eclipse.ant.internal.ui.model;
 
-import java.io.File;
+import java.io.IOException;
 import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
@@ -24,12 +27,15 @@
 import org.eclipse.ant.internal.core.IAntCoreConstants;
 import org.eclipse.ant.internal.ui.AntImageDescriptor;
 import org.eclipse.ant.internal.ui.AntUIImages;
+import org.eclipse.ant.internal.ui.AntUIPlugin;
 import org.eclipse.ant.internal.ui.AntUtil;
 import org.eclipse.ant.internal.ui.IAntUIConstants;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.text.IRegion;
 import org.eclipse.swt.graphics.Image;
@@ -232,7 +238,17 @@
 			fFilePath = path;
 			return;
 		}
-		fFilePath = new Path(new File(url.getPath()).getAbsolutePath()).toString();
+
+		try {
+			URL fileURL = FileLocator.toFileURL(url);
+			fFilePath = new Path((URIUtil.toFile(URIUtil.toURI(fileURL))).getAbsolutePath()).toString();
+		}
+		catch (URISyntaxException e) {
+			AntUIPlugin.log(e);
+		}
+		catch (IOException e) {
+			AntUIPlugin.log(e);
+		}
 	}
 
 	/**
@@ -720,4 +736,4 @@
 		}
 		return false;
 	}
-}
\ No newline at end of file
+}
diff --git a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/preferences/AddCustomDialog.java b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/preferences/AddCustomDialog.java
index be5ed25..ccbf0c1 100644
--- a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/preferences/AddCustomDialog.java
+++ b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/preferences/AddCustomDialog.java
@@ -7,11 +7,14 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Ericsson AB, Hamdan Msheik - Bug 389564
  *******************************************************************************/
 package org.eclipse.ant.internal.ui.preferences;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -25,6 +28,7 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.URIUtil;
 import org.eclipse.core.variables.VariablesPlugin;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.dialogs.StatusDialog;
@@ -579,7 +583,17 @@
 		} else {
 			className = ((File) file).getAbsolutePath();
 			IPath classPath = new Path(className);
-			IPath libraryPath = new Path(library.getEntryURL().getPath());
+			IPath libraryPath = null;
+			try {
+				libraryPath = new Path(URIUtil.toURL(URIUtil.toURI(library.getEntryURL())).getPath());
+			}
+			catch (MalformedURLException e) {
+				AntUIPlugin.log(e);
+			}
+			catch (URISyntaxException e) {
+				AntUIPlugin.log(e);
+			}
+
 			int matching = classPath.matchingFirstSegments(libraryPath);
 			classPath = classPath.removeFirstSegments(matching);
 			classPath = classPath.setDevice(null);