From b7be86b5ee8fb0a24f30094ae7f3b2dc53a2de81 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 4 Jun 2020 11:51:03 -0400 Subject: [PATCH] Fix NPE on empty .xsd document Added some null checks to prevent the NPE occuring. Fixes #684 Signed-off-by: David Thompson --- .../lemminx/extensions/xsd/utils/XSDUtils.java | 9 ++++++++- .../extensions/xsd/XSDCodeLensExtensionsTest.java | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/utils/XSDUtils.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/utils/XSDUtils.java index bcfbe83b5..6b4171c06 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/utils/XSDUtils.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/utils/XSDUtils.java @@ -288,6 +288,9 @@ public static void searchXSOriginAttributes(DOMNode targetNode, BiConsumer getTargetAttrs(DOMNode referencedNode) { + if (referencedNode == null) { + return Collections.emptyList(); + } List referencedNodes = new ArrayList<>(); Document document = referencedNode.getOwnerDocument(); switch (referencedNode.getNodeType()) { @@ -302,7 +305,11 @@ private static List getTargetAttrs(DOMNode referencedNode) { // The referenced node is the DOM document, collect all attributes // xs:complexType/@name, xs:simpleType/@name, xs:element/@name, xs:group/@name // which can be referenced - NodeList nodes = document.getDocumentElement().getChildNodes(); + Element documentElement = document.getDocumentElement(); + if (documentElement == null) { + break; + } + NodeList nodes = documentElement.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsd/XSDCodeLensExtensionsTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsd/XSDCodeLensExtensionsTest.java index 8f224b668..470edcb19 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsd/XSDCodeLensExtensionsTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsd/XSDCodeLensExtensionsTest.java @@ -75,4 +75,16 @@ public void codeLensOnComplexTypeAndSimpleType() throws BadLocationException { cl(r(15, 16, 15, 35), "1 reference", SHOW_REFERENCES)); } + @Test + public void codeLensEmptyDocument() throws BadLocationException { + String xml = ""; + XMLAssert.testCodeLensFor(xml); + } + + @Test + public void codeLensSpace() throws BadLocationException { + String xml = " "; + XMLAssert.testCodeLensFor(xml); + } + }