Skip to content

Commit

Permalink
Prevent from NPE validation with schemaLocaton and "schema.reference.4"
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Oct 7, 2019
1 parent d9e3e13 commit 9d9974f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public enum XMLSchemaErrorCode implements IXMLErrorCode {
cvc_maxInclusive_valid("cvc-maxInclusive-valid"), // https://wiki.xmldation.com/Support/validator/cvc-maxinclusive-valid
cvc_minExclusive_valid("cvc-minExclusive-valid"), // https://wiki.xmldation.com/Support/validator/cvc-minexclusive-valid
cvc_minInclusive_valid("cvc-minInclusive-valid"), // https://wiki.xmldation.com/Support/validator/cvc-mininclusive-valid
TargetNamespace_2("TargetNamespace.2"),
SchemaLocation("SchemaLocation"),
schema_reference_4("schema_reference.4"), //
TargetNamespace_2("TargetNamespace.2"), SchemaLocation("SchemaLocation"), schema_reference_4("schema_reference.4"), //
src_element_3("src-element.3");

private final String code;
Expand Down Expand Up @@ -162,23 +160,26 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case SchemaLocation:
case schema_reference_4: {
DOMNode attrValueNode;
if(code.equals(SchemaLocation)) {
if (code.equals(SchemaLocation)) {
SchemaLocation schemaLocation = document.getSchemaLocation();
attrValueNode = schemaLocation.getAttr().getNodeAttrValue();
}
else {
} else {
NoNamespaceSchemaLocation noNamespaceSchemaLocation = document.getNoNamespaceSchemaLocation();
attrValueNode = noNamespaceSchemaLocation.getAttr().getNodeAttrValue();
if (noNamespaceSchemaLocation != null) {
attrValueNode = noNamespaceSchemaLocation.getAttr().getNodeAttrValue();
} else {
SchemaLocation schemaLocation = document.getSchemaLocation();
attrValueNode = schemaLocation.getAttr().getNodeAttrValue();
}
}

if (attrValueNode != null) {
int startOffset = attrValueNode.getStart();
int endOffset = attrValueNode.getEnd();
try {
Position startPosition = document.positionAt(startOffset);
Position endPosition = document.positionAt(endOffset);
return new Range(startPosition, endPosition);

} catch (BadLocationException e) {
return null;
}
Expand All @@ -200,7 +201,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case cvc_datatype_valid_1_2_1: {
String attrValue = getString(arguments[0]);
Range range = XMLPositionUtility.selectAttributeValueFromGivenValue(attrValue, offset, document);

if (range != null) {
return range;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,15 @@ public void testSrcElement3() throws Exception {
testDiagnosticsFor(xml, d(0, 1, 0, 2, XMLSchemaErrorCode.src_element_3));
}

@Test
public void schema_reference_4_withSchemaLocation() {
String xml = "<IODevice xmlns=\"http://www.io-link.com/IODD/2010/10\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n"
+ " xsi:schemaLocation=\"http://www.io-link.com/IODD/2010/10 IODD1.1.xsd\">\r\n"
+ " </IODevice>";
testDiagnosticsFor(xml, d(1, 24, 1, 73, XMLSchemaErrorCode.schema_reference_4), //
d(0, 1, 0, 9, XMLSchemaErrorCode.cvc_elt_1_a));
}

private static void testDiagnosticsFor(String xml, Diagnostic... expected) {
XMLAssert.testDiagnosticsFor(xml, "src/test/resources/catalogs/catalog.xml", expected);
}
Expand Down

0 comments on commit 9d9974f

Please sign in to comment.