Skip to content

Commit

Permalink
NPE with TypeDefinition
Browse files Browse the repository at this point in the history
Fixes #629

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jun 9, 2020
1 parent 5082892 commit 7bfb0d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -274,7 +274,6 @@ public LocationLink findTypeLocation(DOMNode originNode) {
if (attributeDecl.getScope() == XSConstants.SCOPE_LOCAL) {
return findLocalXSAttribute(originAttribute, targetSchema,
attributeDecl.getEnclosingCTDefinition(), schemaGrammar);

}
}
} else {
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class XSDDocumentLinkParticipant implements IDocumentLinkParticipant {
@Override
public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) {
DOMElement root = document.getDocumentElement();
if (root == null || !XSDUtils.isXSSchema(root)) {
if (!XSDUtils.isXSSchema(root)) {
return;
}
String xmlSchemaPrefix = root.getPrefix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +37,8 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.google.common.base.Objects;

/**
* XSD utilities.
*
Expand Down Expand Up @@ -384,40 +384,39 @@ private static void searchXSOriginAttributes(NodeList nodes, List<DOMAttr> 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) {
return isXSComplexType(element) || isXSSimpleType(element) || isXSElement(element) || isXSGroup(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) {
Expand Down

0 comments on commit 7bfb0d9

Please sign in to comment.