From 9d9974f0fd035183e420f7b528cf769f323da233 Mon Sep 17 00:00:00 2001 From: azerr Date: Mon, 7 Oct 2019 14:59:46 +0200 Subject: [PATCH] Prevent from NPE validation with schemaLocaton and "schema.reference.4" error. See https://github.com/redhat-developer/vscode-xml/issues/183#issuecomment-535489425 Signed-off-by: azerr --- .../participants/XMLSchemaErrorCode.java | 21 ++++++++++--------- .../XMLSchemaDiagnosticsTest.java | 9 ++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java index 1bb63673f..d789fb733 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java @@ -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; @@ -162,15 +160,19 @@ 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(); @@ -178,7 +180,6 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj Position startPosition = document.positionAt(startOffset); Position endPosition = document.positionAt(endOffset); return new Range(startPosition, endPosition); - } catch (BadLocationException e) { return null; } @@ -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; } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java index 733dd9540..8e30099ee 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java @@ -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 = "\r\n" + + " "; + 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); }