Skip to content

Commit

Permalink
Missing xml-model reference generates multiple similar warnings
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#795

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jun 22, 2020
1 parent c9fa0cc commit 742fe52
Showing 1 changed file with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
import static org.eclipse.lemminx.utils.StringUtils.getString;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMRange;
import org.eclipse.lemminx.dom.NoNamespaceSchemaLocation;
import org.eclipse.lemminx.dom.SchemaLocation;
import org.eclipse.lemminx.dom.XMLModel;
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.TargetNamespace_1CodeAction;
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.TargetNamespace_2CodeAction;
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.cvc_attribute_3CodeAction;
Expand Down Expand Up @@ -174,25 +178,43 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
}
case SchemaLocation:
case schema_reference_4: {
DOMNode attrValueNode = null;
DOMRange locationRange = null;
if (code.equals(SchemaLocation)) {
SchemaLocation schemaLocation = document.getSchemaLocation();
attrValueNode = schemaLocation.getAttr().getNodeAttrValue();
locationRange = schemaLocation.getAttr().getNodeAttrValue();
} else {
NoNamespaceSchemaLocation noNamespaceSchemaLocation = document.getNoNamespaceSchemaLocation();
if (noNamespaceSchemaLocation != null) {
attrValueNode = noNamespaceSchemaLocation.getAttr().getNodeAttrValue();
} else {
SchemaLocation schemaLocation = document.getSchemaLocation();
if (schemaLocation != null) {
attrValueNode = schemaLocation.getAttr().getNodeAttrValue();
// Check if location comes from a xml-model/@href
List<XMLModel> xmlModels = document.getXMLModels();
if (!xmlModels.isEmpty()) {
String errorLocation = arguments.length == 1 ? (String) arguments[0] : null;
if (errorLocation != null) {
String documentURI = document.getDocumentURI();
for (XMLModel xmlModel : xmlModels) {
String href = xmlModel.getHref();
String xmlModelLocation = getResolvedLocation(documentURI, href);
if (errorLocation.equals(xmlModelLocation)) {
locationRange = xmlModel.getHrefNode();
break;
}
}
}
}
if (locationRange == null) {
NoNamespaceSchemaLocation noNamespaceSchemaLocation = document.getNoNamespaceSchemaLocation();
if (noNamespaceSchemaLocation != null) {
locationRange = noNamespaceSchemaLocation.getAttr().getNodeAttrValue();
} else {
SchemaLocation schemaLocation = document.getSchemaLocation();
if (schemaLocation != null) {
locationRange = schemaLocation.getAttr().getNodeAttrValue();
}
}
}
}

if (attrValueNode != null) {
int startOffset = attrValueNode.getStart();
int endOffset = attrValueNode.getEnd();
if (locationRange != null) {
int startOffset = locationRange.getStart();
int endOffset = locationRange.getEnd();
try {
Position startPosition = document.positionAt(startOffset);
Position endPosition = document.positionAt(endOffset);
Expand Down Expand Up @@ -272,9 +294,25 @@ public static void registerCodeActionParticipants(Map<String, ICodeActionPartici
codeActions.put(cvc_enumeration_valid.getCode(), new cvc_enumeration_validCodeAction());
codeActions.put(cvc_complex_type_2_1.getCode(), new cvc_complex_type_2_1CodeAction());
codeActions.put(TargetNamespace_1.getCode(), new TargetNamespace_1CodeAction());
codeActions.put(TargetNamespace_2.getCode(), new TargetNamespace_2CodeAction());
codeActions.put(TargetNamespace_2.getCode(), new TargetNamespace_2CodeAction());
if (sharedSettings.getWorkspaceSettings().isResourceOperationSupported(ResourceOperationKind.Create)) {
codeActions.put(schema_reference_4.getCode(), new schema_reference_4CodeAction());
}
}

/**
* Returns the expanded system location
*
* @return the expanded system location
*/
private static String getResolvedLocation(String documentURI, String location) {
if (location == null) {
return null;
}
try {
return XMLEntityManager.expandSystemId(location, documentURI, false);
} catch (MalformedURIException e) {
return location;
}
}
}

0 comments on commit 742fe52

Please sign in to comment.