Bug 560724 - NPE parsing ANT files imported or included via classpath

ANT files that are imported or included using a javaresource and with a
classpath result in a AntXMLContext that does not have a build file but
a build file URL.

Change-Id: I5a545f56aee016cec0d0b38e4e052937a6e48446
Signed-off-by: Michael Schnell <niks@gmx.li>
diff --git a/ant/org.eclipse.ant.tests.ui/Ant Editor Tests/org/eclipse/ant/tests/ui/editor/AntEditorTests.java b/ant/org.eclipse.ant.tests.ui/Ant Editor Tests/org/eclipse/ant/tests/ui/editor/AntEditorTests.java
index b7a0f58..6f62dcd 100644
--- a/ant/org.eclipse.ant.tests.ui/Ant Editor Tests/org/eclipse/ant/tests/ui/editor/AntEditorTests.java
+++ b/ant/org.eclipse.ant.tests.ui/Ant Editor Tests/org/eclipse/ant/tests/ui/editor/AntEditorTests.java
@@ -312,6 +312,28 @@
 		assertNotNull("Should have a document", doc); //$NON-NLS-1$
 	}
 
+	public void testOpenImportViaClasspath() throws PartInitException {
+		IFile file = getIFile("importViaClasspath.xml"); //$NON-NLS-1$
+		AntEditor editor = (AntEditor) EditorTestHelper.openInEditor(file, "org.eclipse.ant.ui.internal.editor.AntEditor", true); //$NON-NLS-1$
+		IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+		assertNotNull("Should have a document", doc); //$NON-NLS-1$
+
+		file = getIFile("BaseBaseBuild.xml"); //$NON-NLS-1$
+		editor = (AntEditor) EditorTestHelper.openInEditor(file, "org.eclipse.ant.ui.internal.editor.AntEditor", true); //$NON-NLS-1$
+		doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+		assertNotNull("Should have a document", doc); //$NON-NLS-1$
+
+		file = getIFile("BaseBuild.xml"); //$NON-NLS-1$
+		editor = (AntEditor) EditorTestHelper.openInEditor(file, "org.eclipse.ant.ui.internal.editor.AntEditor", true); //$NON-NLS-1$
+		doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+		assertNotNull("Should have a document", doc); //$NON-NLS-1$
+
+		file = getIFile("Base.xml"); //$NON-NLS-1$
+		editor = (AntEditor) EditorTestHelper.openInEditor(file, "org.eclipse.ant.ui.internal.editor.AntEditor", true); //$NON-NLS-1$
+		doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
+		assertNotNull("Should have a document", doc); //$NON-NLS-1$
+	}
+
 	/**
 	 * bug 195840 Import a XML file with BOM character in ant editor fails Runs on 1.5 vms or newer.
 	 */
diff --git a/ant/org.eclipse.ant.tests.ui/testbuildfiles/Base.xml b/ant/org.eclipse.ant.tests.ui/testbuildfiles/Base.xml
new file mode 100644
index 0000000..d48026e
--- /dev/null
+++ b/ant/org.eclipse.ant.tests.ui/testbuildfiles/Base.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>

+<!--

+     Copyright (c) 2020 Michael Schnell and others.

+

+     This program and the accompanying materials

+     are made available under the terms of the Eclipse Public License 2.0

+     which accompanies this distribution, and is available at

+     https://www.eclipse.org/legal/epl-2.0/

+

+     SPDX-License-Identifier: EPL-2.0

+    

+     Contributors:

+         Michael Schnell - initial API and implementation

+ -->

+<project name="TOP" default="Middle.Base.DoBase">

+	<include>

+		<javaresource name="BaseBuild.xml" classpath="." />

+	</include>

+</project>
\ No newline at end of file
diff --git a/ant/org.eclipse.ant.tests.ui/testbuildfiles/BaseBaseBuild.xml b/ant/org.eclipse.ant.tests.ui/testbuildfiles/BaseBaseBuild.xml
new file mode 100644
index 0000000..15d5887
--- /dev/null
+++ b/ant/org.eclipse.ant.tests.ui/testbuildfiles/BaseBaseBuild.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>

+<!--

+     Copyright (c) 2020 Michael Schnell and others.

+

+     This program and the accompanying materials

+     are made available under the terms of the Eclipse Public License 2.0

+     which accompanies this distribution, and is available at

+     https://www.eclipse.org/legal/epl-2.0/

+

+     SPDX-License-Identifier: EPL-2.0

+    

+     Contributors:

+         Michael Schnell - initial API and implementation

+ -->

+<project name="Base">

+	<target name="DoBase">

+		<echo message="Hello ANT" />

+	</target>

+</project>
\ No newline at end of file
diff --git a/ant/org.eclipse.ant.tests.ui/testbuildfiles/BaseBuild.xml b/ant/org.eclipse.ant.tests.ui/testbuildfiles/BaseBuild.xml
new file mode 100644
index 0000000..ead8228
--- /dev/null
+++ b/ant/org.eclipse.ant.tests.ui/testbuildfiles/BaseBuild.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>

+<!--

+     Copyright (c) 2020 Michael Schnell and others.

+

+     This program and the accompanying materials

+     are made available under the terms of the Eclipse Public License 2.0

+     which accompanies this distribution, and is available at

+     https://www.eclipse.org/legal/epl-2.0/

+

+     SPDX-License-Identifier: EPL-2.0

+    

+     Contributors:

+         Michael Schnell - initial API and implementation

+ -->

+<project name="Middle">

+	<include>

+		<javaresource name="BaseBaseBuild.xml" classpath="." />

+	</include>

+

+	<target name="DoMore">

+		<echo message="Hello ANT" />

+	</target>

+</project>
\ No newline at end of file
diff --git a/ant/org.eclipse.ant.tests.ui/testbuildfiles/importViaClasspath.xml b/ant/org.eclipse.ant.tests.ui/testbuildfiles/importViaClasspath.xml
new file mode 100644
index 0000000..72c55f9
--- /dev/null
+++ b/ant/org.eclipse.ant.tests.ui/testbuildfiles/importViaClasspath.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!--
+     Copyright (c) 2020 Andrey Loskutov <loskutov@gmx.de> and others.
+
+     This program and the accompanying materials
+     are made available under the terms of the Eclipse Public License 2.0
+     which accompanies this distribution, and is available at
+     https://www.eclipse.org/legal/epl-2.0/
+
+     SPDX-License-Identifier: EPL-2.0
+    
+     Contributors:
+         Andrey Loskutov <loskutov@gmx.de> - initial API and implementation
+ -->
+
+<project name="importViaClasspath" default="import-default">
+	<import>
+		<javaresource name="toBeImported.xml" classpath="." />
+	</import>
+</project>
+
diff --git a/ant/org.eclipse.ant.ui/Ant Editor/org/eclipse/ant/internal/ui/editor/utils/ProjectHelper.java b/ant/org.eclipse.ant.ui/Ant Editor/org/eclipse/ant/internal/ui/editor/utils/ProjectHelper.java
index 4045ffe..d5143e9 100644
--- a/ant/org.eclipse.ant.ui/Ant Editor/org/eclipse/ant/internal/ui/editor/utils/ProjectHelper.java
+++ b/ant/org.eclipse.ant.ui/Ant Editor/org/eclipse/ant/internal/ui/editor/utils/ProjectHelper.java
@@ -278,6 +278,17 @@
 	}
 
 	/**
+	 * Gets the associated project name by the absolute build-file path
+	 *
+	 * @param buildFile
+	 *            The file
+	 * @return The project name
+	 */
+	public static String getProjectNameOfBuildFile(String absolutePath) {
+		return parsedProjectNames.get(absolutePath);
+	}
+
+	/**
 	 * Builds the hash-map's build-file key
 	 *
 	 * @param buildFile
@@ -345,8 +356,11 @@
 				String currentProjectName = context.getCurrentProjectName();
 				// just an additional check if the name is non-empty
 				if (this.isCurrentProjectNameValid(currentProjectName)) {
-					String buildFilePath = context.getBuildFile().getAbsolutePath();
-					storeParsedProjectName(buildFilePath, currentProjectName);
+					if (context.getBuildFile() != null) {
+						storeParsedProjectName(context.getBuildFile().getAbsolutePath(), currentProjectName);
+					} else if (context.getBuildFileURL() != null) {
+						storeParsedProjectName(new File(context.getBuildFileURL().getPath()).getAbsolutePath(), currentProjectName);
+					}
 				}
 			}
 			catch (SAXParseException e) {
diff --git a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntIncludeNode.java b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntIncludeNode.java
index 906d54c..dfda703 100644
--- a/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntIncludeNode.java
+++ b/ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/model/AntIncludeNode.java
@@ -141,7 +141,10 @@
 		 * of any subsequent operation on the AntIncludeNode object.
 		 */
 		IFile projectSpecificBuildFile = this.handleCorrectBuildFile(importedFromNode);
-		return org.eclipse.ant.internal.ui.editor.utils.ProjectHelper.getProjectNameOfBuildFile(projectSpecificBuildFile);
+		if (projectSpecificBuildFile != null) {
+			return org.eclipse.ant.internal.ui.editor.utils.ProjectHelper.getProjectNameOfBuildFile(projectSpecificBuildFile);
+		}
+		return org.eclipse.ant.internal.ui.editor.utils.ProjectHelper.getProjectNameOfBuildFile(this.getFilePath());
 	}
 
 	/**