Skip to content

Commit

Permalink
Fix NPE on empty .xsd document
Browse files Browse the repository at this point in the history
Added some null checks to prevent the NPE occuring.

Fixes eclipse-lemminx#684

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Jun 4, 2020
1 parent 35ac5a1 commit b60aa90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ public static void searchXSOriginAttributes(DOMNode targetNode, BiConsumer<DOMAt
* @return the referenced attributes list from the given referenced node.
*/
private static List<DOMAttr> getTargetAttrs(DOMNode referencedNode) {
if (referencedNode == null) {
return Collections.emptyList();
}
List<DOMAttr> referencedNodes = new ArrayList<>();
Document document = referencedNode.getOwnerDocument();
switch (referencedNode.getNodeType()) {
Expand All @@ -302,7 +305,11 @@ private static List<DOMAttr> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit b60aa90

Please sign in to comment.