Bug 566490 - NPE in DesktopFileWriter.escapeSpaces

Avoid using raw values from system properties, they could be null.

Also some smaller issues fixed
- don't use != to compare strings
- don't call same methods in a loop
- don't use pattern matching just to remove "file:" prefix

Change-Id: Ia6c0578cfb571ee54a260a2c5bb1a08b75473dd2
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/DesktopFileWriter.java b/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/DesktopFileWriter.java
index e584319..34dbdfd 100644
--- a/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/DesktopFileWriter.java
+++ b/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/DesktopFileWriter.java
@@ -16,6 +16,7 @@
 import static java.util.stream.Collectors.joining;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -172,6 +173,9 @@
 	 * @return The minimal file content as list (one entry = one file line)
 	 */
 	public static List<String> getMinimalDesktopFileContent(String eclipseExecutableLocation, String productName) {
+		if (eclipseExecutableLocation == null || eclipseExecutableLocation.isEmpty()) {
+			return Collections.emptyList();
+		}
 		String executable = escapeSpaces(eclipseExecutableLocation);
 		return Arrays.asList(//
 				"[Desktop Entry]", //$NON-NLS-1$
diff --git a/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationLinux.java b/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationLinux.java
index c0085fa..bb63549 100644
--- a/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationLinux.java
+++ b/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationLinux.java
@@ -70,12 +70,13 @@
 	public List<ISchemeInformation> getSchemesInformation(Collection<IScheme> schemes) throws Exception {
 		List<ISchemeInformation> returnList = new ArrayList<>();
 
+		String eclipseLauncher = getEclipseLauncher();
 		for (IScheme scheme : schemes) {
 			SchemeInformation schemeInfo = new SchemeInformation(scheme.getName(),
 					scheme.getDescription());
 
 			String location = determineHandlerLocation(scheme.getName());
-			if (location.equals(getEclipseLauncher())) {
+			if (location.equals(eclipseLauncher)) {
 				schemeInfo.setHandled(true);
 			}
 			schemeInfo.setHandlerLocation(location);
@@ -96,6 +97,9 @@
 			String desktopFilePath) throws IOException {
 
 		List<String> lines = readFileOrGetInitialContent(desktopFilePath);
+		if (lines.isEmpty()) {
+			return;
+		}
 
 		DesktopFileWriter writer = new DesktopFileWriter(lines);
 		for (IScheme scheme : toAdd) {
@@ -152,12 +156,19 @@
 
 	@Override
 	public String getEclipseLauncher() {
-		return System.getProperty("eclipse.launcher"); //$NON-NLS-1$
+		String property = System.getProperty("eclipse.launcher"); //$NON-NLS-1$
+		if (property == null) {
+			return getEclipseHomeLocation() + "eclipse"; //$NON-NLS-1$
+		}
+		return property;
 	}
 
 	private String getEclipseHomeLocation() {
-		String homeLocationProperty = System.getProperty("eclipse.home.location"); //$NON-NLS-1$
-		return homeLocationProperty.replaceAll("file:(.*)", "$1"); //$NON-NLS-1$ //$NON-NLS-2$
+		String homeLocationProperty = System.getProperty("eclipse.home.location", ""); //$NON-NLS-1$ //$NON-NLS-2$
+		if (homeLocationProperty.startsWith("file:")) { //$NON-NLS-1$
+			return homeLocationProperty.substring("file:".length()); //$NON-NLS-1$
+		}
+		return homeLocationProperty;
 	}
 
 	/**
diff --git a/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationMacOsX.java b/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationMacOsX.java
index 0ad7a26..25bcae1 100644
--- a/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationMacOsX.java
+++ b/bundles/org.eclipse.urischeme/src/org/eclipse/urischeme/internal/registration/RegistrationMacOsX.java
@@ -61,12 +61,13 @@
 	public List<ISchemeInformation> getSchemesInformation(Collection<IScheme> schemes) throws Exception {
 		List<ISchemeInformation> returnList = new ArrayList<>();
 
+		String eclipseLauncher = getEclipseLauncher();
 		for (IScheme scheme : schemes) {
 
 			SchemeInformation schemeInfo = new SchemeInformation(scheme.getName(), scheme.getDescription());
 
 			String location = determineHandlerLocation(getLsRegisterOutput(), scheme.getName());
-			if (location != "" && getEclipseLauncher().startsWith(location)) { //$NON-NLS-1$
+			if (!location.isEmpty() && eclipseLauncher.startsWith(location)) {
 				schemeInfo.setHandled(true);
 			}
 			schemeInfo.setHandlerLocation(location);