Skip to content

Commit

Permalink
Adjust range for cvc-elt.3.1 (see #241)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Nov 30, 2018
1 parent 921459b commit d779bf8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum XMLSchemaErrorCode implements IXMLErrorCode {
cvc_complex_type_4("cvc-complex-type.4"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-4
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_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_3("cvc-type.3.1.3"), // https://wiki.xmldation.com/Support/Validator/cvc-type-3-1-3,
Expand Down Expand Up @@ -121,6 +122,20 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
String attrName = (String) arguments[1];
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 attrName = namespaceAntAttrName;
int index = namespaceAntAttrName.indexOf(",");
if (index != -1) {
String namespaceURI = namespaceAntAttrName.substring(0, index);
String prefix = document.getDocumentElement().getPrefix(namespaceURI);
attrName = namespaceAntAttrName.substring(index + 1, namespaceAntAttrName.length());
if (prefix != null && !prefix.isEmpty()) {
attrName = prefix + ":" + attrName;
}
}
return XMLPositionUtility.selectAttributeFromGivenNameAt(attrName, offset, document);
}
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 @@ -123,6 +123,23 @@ private static Range createAttrNameRange(DOMAttr attr, DOMDocument document) {
return createRange(startOffset, endOffset, document);
}

public static Range selectAttributeFromGivenNameAt(String attrName, int offset, DOMDocument document) {
DOMNode element = document.findNodeAt(offset);
if (element != null && element.hasAttributes()) {
DOMAttr attr = element.getAttributeNode(attrName);
if (attr != null) {
return createAttrRange(attr, document);
}
}
return null;
}

private static Range createAttrRange(DOMAttr attr, DOMDocument document) {
int startOffset = attr.getStart();
int endOffset = attr.getEnd();
return createRange(startOffset, endOffset, document);
}

private static int adjustOffsetForAttribute(int offset, DOMDocument document) {
// For attribute value, Xerces report the error offset after the spaces which
// are after " of the attribute value
Expand Down Expand Up @@ -333,5 +350,14 @@ public static Range selectText(int offset, DOMDocument document) {
}
return null;
}

public static Range selectDTDElementDeclAt(int offset, DOMDocument document) {
DOMNode node = document.findNodeAt(offset);
if (node != null && node.isDTDElementDecl()) {
return createRange(node.getStart(), node.getEnd(), document);
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void cvc_complex_type_2_1WithLinefeed() throws Exception {
testDiagnosticsFor(xml, d);
testCodeActionsFor(xml, d, ca(d, te(5, 25, 7, 8, "/>")));
}

/**
* @see https://github.com/angelozerr/lsp4xml/issues/217
*/
Expand All @@ -285,6 +285,21 @@ public void issue217() {
testDiagnosticsFor(xml, d);
}

@Test
public void cvc_elt_3_1() throws Exception {
String xml = "<?xml version=\"1.0\"?>\r\n" + //
"<xs:schema\r\n" + //
" elementFormDefault=\"qualified\"\r\n" + //
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\r\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n" + //
" <xs:complexType\r\n" + //
" name=\"property\"\r\n" + //
" xsi:nil=\"true\"></xs:complexType>\r\n" + //
"</xs:schema>";
XMLAssert.testDiagnosticsFor(xml, d(7, 3, 7, 17, XMLSchemaErrorCode.cvc_elt_3_1));

}

private static void testDiagnosticsFor(String xml, Diagnostic... expected) {
XMLAssert.testDiagnosticsFor(xml, "src/test/resources/catalogs/catalog.xml", expected);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<xs:schema
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:complexType
name="property"
xsi:nil="true"></xs:complexType>
</xs:schema>

0 comments on commit d779bf8

Please sign in to comment.