From 5f4494228535214d52c2e02eeb5823e70dd93444 Mon Sep 17 00:00:00 2001 From: azerr Date: Fri, 5 Jun 2020 16:38:14 +0200 Subject: [PATCH] NPE with TypeDefinition Fixes #629 Signed-off-by: azerr --- .../xsd/contentmodel/CMXSDDocument.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDDocument.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDDocument.java index bd0fd09dce..0e4038fee2 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDDocument.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDDocument.java @@ -274,7 +274,6 @@ public LocationLink findTypeLocation(DOMNode originNode) { if (attributeDecl.getScope() == XSConstants.SCOPE_LOCAL) { return findLocalXSAttribute(originAttribute, targetSchema, attributeDecl.getEnclosingCTDefinition(), schemaGrammar); - } } } else { @@ -284,8 +283,12 @@ public LocationLink findTypeLocation(DOMNode originNode) { if (globalElement) { return findGlobalXSElement(originElement, targetSchema); } else { - return findLocalXSElement(originElement, targetSchema, - elementDeclaration.getElementDeclaration().getEnclosingCTDefinition(), schemaGrammar); + XSComplexTypeDefinition complexTypeDefinition = elementDeclaration.getElementDeclaration() + .getEnclosingCTDefinition(); + if (complexTypeDefinition != null) { + return findLocalXSElement(originElement, targetSchema, complexTypeDefinition, schemaGrammar); + } + return findXSElement(originElement, targetSchema.getChildNodes(), true); } } } @@ -409,7 +412,7 @@ private static LocationLink findGlobalXSElement(DOMElement originElement, DOMDoc private static LocationLink findLocalXSElement(DOMElement originElement, DOMDocument targetSchema, XSComplexTypeDefinition enclosingType, SchemaGrammar schemaGrammar) { // In local xs:element case, xs:element is declared inside a complex type - // (enclosing tye). + // (enclosing type). // Xerces stores in the SchemaGrammar the locator (offset) for each complex type // (XSComplexTypeDecl) // Here we get the offset of the local enclosing complex type xs:complexType. @@ -501,16 +504,12 @@ private static LocationLink findXSElement(DOMElement originElement, NodeList chi Node n = children.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element elt = (Element) n; - if (XSDUtils.isXSElement(elt)) { - if (originElement.getLocalName().equals(elt.getAttribute("name"))) { - DOMAttr targetAttr = (DOMAttr) elt.getAttributeNode("name"); - LocationLink location = XMLPositionUtility.createLocationLink(originElement, - targetAttr.getNodeAttrValue()); - return location; - } + LocationLink location = findXSElement(originElement, elt); + if (location != null) { + return location; } if (inAnyLevel && elt.hasChildNodes()) { - LocationLink location = findXSElement(originElement, elt.getChildNodes(), inAnyLevel); + location = findXSElement(originElement, elt.getChildNodes(), inAnyLevel); if (location != null) { return location; } @@ -520,6 +519,18 @@ private static LocationLink findXSElement(DOMElement originElement, NodeList chi return null; } + private static LocationLink findXSElement(DOMElement originElement, Element elt) { + if (elt != null && XSDUtils.isXSElement(elt)) { + if (originElement.getLocalName().equals(elt.getAttribute("name"))) { + DOMAttr targetAttr = (DOMAttr) elt.getAttributeNode("name"); + LocationLink location = XMLPositionUtility.createLocationLink(originElement, + targetAttr.getNodeAttrValue()); + return location; + } + } + return null; + } + private static LocationLink findLocalXSAttribute(DOMAttr originAttribute, DOMDocument targetSchema, XSComplexTypeDefinition enclosingType, SchemaGrammar schemaGrammar) { int complexTypeOffset = getComplexTypeOffset(enclosingType, schemaGrammar);