Skip to content

Commit

Permalink
Re-adjusted error range for bad schema location, need to filter it ou…
Browse files Browse the repository at this point in the history
…t duplicates

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Apr 26, 2019
1 parent 3168b2c commit 5aa582d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
import java.util.Map;

import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMNode;
import org.eclipse.lsp4xml.dom.NoNamespaceSchemaLocation;
import org.eclipse.lsp4xml.dom.SchemaLocation;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.cvc_attribute_3CodeAction;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.cvc_complex_type_2_1CodeAction;
import org.eclipse.lsp4xml.extensions.contentmodel.participants.codeactions.cvc_complex_type_2_3CodeAction;
Expand Down Expand Up @@ -46,7 +51,7 @@ public enum XMLSchemaErrorCode implements IXMLErrorCode {
cvc_datatype_valid_1_2_1("cvc-datatype-valid.1.2.1"), // https://wiki.xmldation.com/Support/Validator/cvc-datatype-valid-1-2-1
cvc_elt_1_a("cvc-elt.1.a"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-1
cvc_elt_3_1("cvc-elt.3.1"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-3-1
cvc_elt_3_2_1("cvc-elt.3.2.1"), //https://wiki.xmldation.com/Support/Validator/cvc-elt-3-2-1
cvc_elt_3_2_1("cvc-elt.3.2.1"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-3-2-1
cvc_elt_4_2("cvc-elt.4.2"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-4-2
cvc_type_3_1_1("cvc-type.3.1.1"), // https://wiki.xmldation.com/Support/Validator/cvc-type-3-1-1
cvc_type_3_1_2("cvc-type.3.1.2"), // https://wiki.xmldation.com/Support/Validator/cvc-type-3-1-2
Expand All @@ -59,8 +64,8 @@ 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"),
schema_reference_4("schema_reference.4"); //
TargetNamespace_2("TargetNamespace.2"),
schema_reference_4("schema_reference.4"); //

private final String code;

Expand Down Expand Up @@ -104,7 +109,7 @@ public static XMLSchemaErrorCode get(String name) {
* @param location
* @param key
* @param arguments
* @param document.ge
* @param document
* @return the LSP range from the SAX error.
*/
public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Object[] arguments,
Expand All @@ -127,7 +132,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case cvc_elt_3_1: {
String namespaceAntAttrName = (String) arguments[1]; // http://www.w3.org/2001/XMLSchema-instance,nil
String namespaceAntAttrName = (String) arguments[1]; // http://www.w3.org/2001/XMLSchema-instance,nil
String attrName = namespaceAntAttrName;
int index = namespaceAntAttrName.indexOf(",");
if (index != -1) {
Expand All @@ -140,6 +145,24 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
}
return XMLPositionUtility.selectAttributeFromGivenNameAt(attrName, offset, document);
}
case schema_reference_4: {
NoNamespaceSchemaLocation noNamespaceSchemaLocation = document.getNoNamespaceSchemaLocation();
DOMNode attrValue = noNamespaceSchemaLocation.getAttr().getNodeAttrValue();
if (attrValue != null) {
int startOffset = attrValue.getStart();
int endOffset = attrValue.getEnd();
Range range;
try {
Position startPosition = document.positionAt(startOffset);
Position endPosition = document.positionAt(endOffset);
return new Range(startPosition, endPosition);

} catch (BadLocationException e) {
return null;
}
}
return null;
}
case cvc_attribute_3:
case cvc_complex_type_3_1:
case cvc_elt_4_2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.eclipse.lsp4xml.utils.XMLPositionUtility.toLSPPosition;

import java.util.HashSet;
import java.util.List;

import org.apache.xerces.impl.XMLErrorReporter;
Expand Down Expand Up @@ -39,13 +40,15 @@ public abstract class AbstractLSPErrorReporter extends XMLErrorReporter {

private final DOMDocument xmlDocument;
private final List<Diagnostic> diagnostics;
private final HashSet<Diagnostic> diagnosticSet;

private final String source;

public AbstractLSPErrorReporter(String source, DOMDocument xmlDocument, List<Diagnostic> diagnostics) {
this.source = source;
this.xmlDocument = xmlDocument;
this.diagnostics = diagnostics;
this.diagnosticSet = new HashSet<Diagnostic>();
XMLMessageFormatter xmft = new XMLMessageFormatter();
super.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
super.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
Expand Down Expand Up @@ -82,9 +85,14 @@ public String reportError(XMLLocator location, String domain, String key, Object
if(adjustedRange == null) {
return null;
}

Diagnostic d = new Diagnostic(adjustedRange, message, toLSPSeverity(severity), source, key);
if(diagnosticSet.contains(d)) {
return null;
}
diagnosticSet.add(d);
// Fill diagnostic
diagnostics.add(new Diagnostic(adjustedRange, message,
toLSPSeverity(severity), source, key));
diagnostics.add(d);

if (severity == SEVERITY_FATAL_ERROR && !fContinueAfterFatalError) {
XMLParseException parseException = (exception != null) ? new XMLParseException(location, message, exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,38 @@ public void schemaWithUrlWithoutCache() throws Exception {
"</invoice> \r\n" + //
"";
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, configuration, pd(fileURI, //
new Diagnostic(r(2, 52, 2, 52),
new Diagnostic(r(2, 31, 2, 51),
"schema_reference.4: Failed to read schema document 'http://invoice.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.",
DiagnosticSeverity.Warning, "xml", "schema_reference.4"), //
new Diagnostic(r(1, 1, 1, 8), "cvc-elt.1.a: Cannot find the declaration of element 'invoice'.",
DiagnosticSeverity.Error, "xml", "cvc-elt.1.a")));
}

@Test
public void schemaWithUrlWithoutCacheNoDuplicateWarning() throws Exception {
// Here we test the following context:
// - XML which have xsi:noNamespaceSchemaLocation="http://invoice.xsd"
// - XMLCacheResolverExtension which is disabled
// Result of test is to have one published diagnostics with several Xerces
// errors (schema)

Consumer<XMLLanguageService> configuration = ls -> {
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
// Use cache on file system
contentModelManager.setUseCache(false);
};

String fileURI = "test.xml";
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<invoice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:noNamespaceSchemaLocation=\"http://invoice.xsd\">\r\n" + //
" <a></a>\r\n" + //
" <b></b>\r\n" + //
" <c></c>\r\n" + //
"</invoice> \r\n" + //
"";
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, configuration, pd(fileURI, //
new Diagnostic(r(2, 31, 2, 51),
"schema_reference.4: Failed to read schema document 'http://invoice.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.",
DiagnosticSeverity.Warning, "xml", "schema_reference.4"), //
new Diagnostic(r(1, 1, 1, 8), "cvc-elt.1.a: Cannot find the declaration of element 'invoice'.",
Expand Down

0 comments on commit 5aa582d

Please sign in to comment.