From 7bfb0d95eeb42bf18d223f13c4234ca35e97e8ae 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 | 37 ++++++++++++------- .../XSDDocumentLinkParticipant.java | 2 +- .../extensions/xsd/utils/XSDUtils.java | 23 ++++++------ 3 files changed, 36 insertions(+), 26 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..69fd85c0b7 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 @@ -235,7 +235,7 @@ public LocationLink findTypeLocation(DOMNode originNode) { originAttribute = (DOMAttr) originNode; originElement = originAttribute.getOwnerElement(); } - if (originElement == null) { + if (originElement == null || originElement.getLocalName() == null) { return null; } // Try to retrieve XSD element declaration from the given element. @@ -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 (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); diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/participants/XSDDocumentLinkParticipant.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/participants/XSDDocumentLinkParticipant.java index 984d9e1aa1..fcdb11855b 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/participants/XSDDocumentLinkParticipant.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/participants/XSDDocumentLinkParticipant.java @@ -45,7 +45,7 @@ public class XSDDocumentLinkParticipant implements IDocumentLinkParticipant { @Override public void findDocumentLinks(DOMDocument document, List links) { DOMElement root = document.getDocumentElement(); - if (root == null || !XSDUtils.isXSSchema(root)) { + if (!XSDUtils.isXSSchema(root)) { return; } String xmlSchemaPrefix = root.getPrefix(); 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 6b4171c065..911bc5b279 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 @@ -19,8 +19,6 @@ import java.util.Vector; import java.util.function.BiConsumer; -import com.google.common.base.Objects; - import org.apache.xerces.impl.xs.SchemaGrammar; import org.apache.xerces.xs.StringList; import org.eclipse.lemminx.dom.DOMAttr; @@ -39,6 +37,8 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import com.google.common.base.Objects; + /** * XSD utilities. * @@ -384,28 +384,27 @@ private static void searchXSOriginAttributes(NodeList nodes, List targe } public static boolean isXSComplexType(Element element) { - return "complexType".equals(element.getLocalName()); + return element != null && "complexType".equals(element.getLocalName()); } public static boolean isXSSimpleType(Element element) { - return "simpleType".equals(element.getLocalName()); + return element != null && "simpleType".equals(element.getLocalName()); } public static boolean isXSElement(Element element) { - return "element".equals(element.getLocalName()); + return element != null && "element".equals(element.getLocalName()); } public static boolean isXSGroup(Element element) { - return "group".equals(element.getLocalName()); + return element != null && "group".equals(element.getLocalName()); } public static boolean isXSInclude(Element element) { - return "include".equals(element.getLocalName()); + return element != null && "include".equals(element.getLocalName()); } - - public static boolean isXSImport(Element child) { - return "import".equals(child.getLocalName()); + public static boolean isXSImport(Element element) { + return element != null && "import".equals(element.getLocalName()); } public static boolean isXSTargetElement(Element element) { @@ -413,11 +412,11 @@ public static boolean isXSTargetElement(Element element) { } public static boolean isXSAttribute(DOMElement element) { - return "attribute".equals(element.getLocalName()); + return element != null && "attribute".equals(element.getLocalName()); } public static boolean isXSSchema(Element element) { - return "schema".equals(element.getLocalName()); + return element != null && "schema".equals(element.getLocalName()); } public static FilesChangedTracker createFilesChangedTracker(SchemaGrammar grammar) {