Bug 529828 - [sonar] Resolve "Possible null pointer dereference"

Change-Id: Iddfb24c63e3454c3de9e539db25c379c7775f06c
Signed-off-by: René Purrio <rpurrio@itemis.de>
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java b/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java
index f254fc6..d050bf8 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/internal/base/IndexToolApplication.java
@@ -100,6 +100,9 @@
 	private static void delete(File file) throws IOException {
 		if (file.isDirectory()) {
 			File files[] = file.listFiles();
+			if(files == null) {
+				throw new IOException("Content from directory '" + file.getAbsolutePath() + "' can not be listed."); //$NON-NLS-1$ //$NON-NLS-2$
+			}
 			for (int i = 0; i < files.length; i++) {
 				delete(files[i]);
 			}
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java b/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java
index 0903ed4..c8fd3e5 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/internal/search/SearchIndex.java
@@ -210,6 +210,9 @@
 
 	private void deleteDir(File indexDir) {
 		File[] files = indexDir.listFiles();
+		if(files == null) {
+			files = new File[0];
+		}
 		for (File file : files) {
 			if (file.isDirectory())
 				deleteDir(file);
diff --git a/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java b/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java
index 53d2b83..4416eca 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/internal/standalone/Eclipse.java
@@ -10,7 +10,11 @@
  *******************************************************************************/
 package org.eclipse.help.internal.standalone;
 
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.List;
 
 /**
@@ -176,6 +180,9 @@
 			throw new Exception("Plugins directory " + pluginsDir.getAbsolutePath() //$NON-NLS-1$
 				+ " does not exists.  Pass a correct -eclipsehome option"); //$NON-NLS-1$
 		File[] plugins = pluginsDir.listFiles();
+		if(plugins == null) {
+			throw new IOException("Content from plugins directory '" + pluginsDir.getAbsolutePath() + "' can not be listed."); //$NON-NLS-1$ //$NON-NLS-2$
+		}
 		for (int i = 0; i < plugins.length; i++) {
 			String file = plugins[i].getName();
 			if (file.startsWith("org.eclipse.equinox.launcher_") && file.endsWith(".jar") && !plugins[i].isDirectory()) //$NON-NLS-1$ //$NON-NLS-2$
diff --git a/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java b/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java
index 776ddb4..0ab6701 100644
--- a/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java
+++ b/org.eclipse.help.base/src/org/eclipse/help/search/HelpIndexBuilder.java
@@ -352,7 +352,7 @@
 	 * nl/language/country/ locale dirs that contain files. We will
 	 * produce an index for each one.
 	 */
-	private void computeLocaleDirs(boolean fragment) {
+	private void computeLocaleDirs(boolean fragment) throws CoreException {
 		if (!fragment) {
 			LocaleDir dir = new LocaleDir(null, "/"); //$NON-NLS-1$
 			dir.addDirectory(destination);
@@ -366,7 +366,7 @@
 		File nl = new File(destination, "nl"); //$NON-NLS-1$
 		if (!nl.exists() || !nl.isDirectory())
 			return;
-		File [] languages = nl.listFiles();
+		File [] languages = listFiles(nl);
 		HashSet<String> locales = new HashSet<>();
 		for (int i=0; i<languages.length; i++) {
 			File language = languages[i];
@@ -374,7 +374,7 @@
 				continue;
 			if (!isValidLanguage(language.getName()))
 				continue;
-			File [] countries = language.listFiles();
+			File [] countries = listFiles(language);
 			for (int j=0; j<countries.length; j++) {
 				File country = countries[j];
 				String locale;
@@ -403,10 +403,10 @@
 		}
 	}
 
-	private void computeSystem(File systemRoot, String [] values) {
+	private void computeSystem(File systemRoot, String [] values) throws CoreException {
 		if (systemRoot.exists() && systemRoot.isDirectory()) {
 			// check
-			File [] files = systemRoot.listFiles();
+			File [] files = listFiles(systemRoot);
 			for (int i=0; i<files.length; i++) {
 				File sdir = files[i];
 				if (!sdir.isDirectory())
@@ -643,7 +643,7 @@
 
 	private void prepareDirectory(File indexDirectory) throws CoreException {
 		if (indexDirectory.exists()) {
-			File[] files = indexDirectory.listFiles();
+			File[] files = listFiles(indexDirectory);
 			for (int i = 0; i < files.length; i++) {
 				File file = files[i];
 				boolean result = file.delete();
@@ -766,4 +766,12 @@
 				IStatus.OK, message, t);
 		throw new CoreException(status);
 	}
+	
+	private File[] listFiles(File file) throws CoreException {
+		File[] fileList = file.listFiles();
+		if(fileList == null) {
+			throwCoreException("Content from directory '" + file.getAbsolutePath() + "' can not be listed.", null); //$NON-NLS-1$ //$NON-NLS-2$
+		}
+		return fileList;
+	}
 }
diff --git a/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/ScopeSetManager.java b/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/ScopeSetManager.java
index 6a018e9..276ba4f 100644
--- a/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/ScopeSetManager.java
+++ b/org.eclipse.help.ui/src/org/eclipse/help/ui/internal/views/ScopeSetManager.java
@@ -110,6 +110,9 @@
 		if (dir.exists() && dir.isDirectory()) {
 			File[] files = dir.listFiles((FilenameFilter) (dir1, name) -> name.endsWith(ScopeSet.EXT)
 					|| name.endsWith(HistoryScopeSet.EXT));
+			if(files == null) {
+				files = new File[0];
+			}
 			for (int i = 0; i < files.length; i++) {
 				File file = files[i];
 				String name = file.getName();
diff --git a/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java b/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java
index d18c161..9097980 100644
--- a/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java
+++ b/org.eclipse.help/src/org/eclipse/help/internal/HelpData.java
@@ -152,7 +152,13 @@
 	 * Allow unit tests to override for providing test data.
 	 */
 	public InputStream getHelpDataFile(String filePath) throws IOException {
-		return Platform.getProduct().getDefiningBundle().getEntry(filePath).openStream();
+		IProduct product = Platform.getProduct();
+		Bundle definingBundle = product != null ? product.getDefiningBundle() : null;
+		URL entry = definingBundle != null ? definingBundle.getEntry(filePath) : null;
+		if(entry == null) {
+			throw new IOException("No entry to '"+filePath+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$
+		}
+		return entry.openStream();
 	}
 
 	/*
diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java
index 8baec7e..176f6bb 100644
--- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java
+++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/DynamicXHTMLProcessorTest.java
@@ -15,6 +15,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.URL;
 import java.nio.charset.StandardCharsets;
 
 import javax.xml.parsers.ParserConfigurationException;
@@ -48,7 +49,11 @@
 	protected InputStream getProcessedInput(String path, Bundle bundle)
 			throws IOException, SAXException, ParserConfigurationException,
 			TransformerException, TransformerConfigurationException {
-		try (InputStream in = bundle.getEntry(path).openStream()) {
+		URL url = bundle.getEntry(path);
+		if(url == null ) {
+			throw new IOException("No entry to '"+path+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$
+		}
+		try (InputStream in = url.openStream()) {
 			String href = '/' + bundle.getBundleId() + path;
 			return DynamicXHTMLProcessor.process(href, in, "en", true);
 		}
diff --git a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java
index c146999..0628d70 100644
--- a/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java
+++ b/org.eclipse.ua.tests/help/org/eclipse/ua/tests/help/dynamic/XMLProcessorTest.java
@@ -10,7 +10,9 @@
  *******************************************************************************/
 package org.eclipse.ua.tests.help.dynamic;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.help.internal.base.HelpEvaluationContext;
@@ -45,8 +47,16 @@
 		};
 		XMLProcessor processor = new XMLProcessor(handlers);
 		Bundle bundle = UserAssistanceTestPlugin.getDefault().getBundle();
-		try (InputStream in = bundle.getEntry(FileUtil.getResultFile(path)).openStream();
-				InputStream in2 = processor.process(bundle.getEntry(path).openStream(),
+		URL url1 = bundle.getEntry(FileUtil.getResultFile(path));
+		if(url1 == null) {
+			throw new IOException("No entry to '"+FileUtil.getResultFile(path)+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$
+		}
+		URL url2 = bundle.getEntry(path);
+		if(url2 == null) {
+			throw new IOException("No entry to '"+path+"' could be found or caller does not have the appropriate permissions.");//$NON-NLS-1$ //$NON-NLS-2$
+		}
+		try (InputStream in = url1.openStream();
+				InputStream in2 = processor.process(url2.openStream(),
 						'/' + bundle.getSymbolicName() + '/' + path, "UTF-8")) {
 			XMLUtil.assertXMLEquals("XML content was not processed correctly: " + path, in, in2);
 		}