Skip to content

Commit

Permalink
Disable XSD validation when xsi:schemaLocation doesn't declare the
Browse files Browse the repository at this point in the history
namespace for the document element root.

See #951

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Dec 31, 2020
1 parent 200a7aa commit e770ac1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMDocumentType;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.SchemaLocationHint;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSyntaxErrorCode;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
Expand Down Expand Up @@ -82,8 +83,8 @@ public static void doDiagnostics(DOMDocument document, XMLEntityResolver entityR
// Add LSP content handler to stop XML parsing if monitor is canceled.
parser.setContentHandler(new LSPContentHandler(monitor));

boolean hasSchemaGrammar = document.hasSchemaLocation() || document.hasNoNamespaceSchemaLocation()
|| hasExternalSchemaGrammar(document);
boolean hasSchemaGrammar = isValidSchemaLocationForDocumentElement(document)
|| document.hasNoNamespaceSchemaLocation() || hasExternalSchemaGrammar(document);
boolean hasGrammar = document.hasDTD() || hasSchemaGrammar || document.hasExternalGrammar();
// If diagnostics for Schema preference is enabled
if ((validationSettings == null) || validationSettings.isSchema()) {
Expand Down Expand Up @@ -114,6 +115,26 @@ public static void doDiagnostics(DOMDocument document, XMLEntityResolver entityR
}
}

/**
* Returns true if the given DOM document declares a xsi:schemaLocation hint for
* the document element and false otherwise.
*
* @param document the DOM document.
* @return true if the given DOM document declares a xsi:schemaLocation hint for
* the document element and false otherwise.
*/
private static boolean isValidSchemaLocationForDocumentElement(DOMDocument document) {
if (!document.hasSchemaLocation()) {
return false;
}
String namespaceURI = document.getNamespaceURI();
SchemaLocationHint hint = document.getSchemaLocation().getLocationHint(namespaceURI);
if (hint == null) {
return false;
}
return true;
}

private static boolean hasExternalSchemaGrammar(DOMDocument document) {
if (document.getExternalGrammarFromNamespaceURI() != null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,32 @@ public void diagnosticsWithCatalogAndXSDInclude() throws BadLocationException {
Diagnostic diagnostic = d(1, 2, 1, 6, XMLSchemaErrorCode.cvc_complex_type_2_4_b);
XMLAssert.testDiagnosticsFor(xml, "src/test/resources/catalogs/include/catalog-include.xml", diagnostic);
}

@Test
public void noHintSchemaLocationForRootElement() {
// Here the xsi:schemaLocation doens't declare the hint for
// http://www.eclipse.org/oomph/setup/1.0 (used in the root element)
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<setup:Configuration \r\n" + //
" xmi:version=\"2.0\"\r\n" + //
" xmlns:xmi=\"http://www.omg.org/XMI\"\r\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" + //
" xmlns:setup=\"http://www.eclipse.org/oomph/setup/1.0\"\r\n" + //
" xmlns:setup.p2=\"http://www.eclipse.org/oomph/setup/p2/1.0\"\r\n" + //
" xmlns:workbench=\"http://www.eclipse.org/oomph/setup/workbench/1.0\"\r\n" + //
" xsi:schemaLocation=\"http://www.eclipse.org/oomph/setup/workbench/1.0 http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/models/Workbench.ecore\"\r\n"
+ //
" label=\"Gael Eclipse Installation\"> \r\n" + //
" <installation name=\"com.github.glhez.eclipse.install\" label=\"Gael Eclipse Installation Installation\">\r\n"
+ //
" <setupTask xsi:type=\"setup.p2:P2Task\" label=\"Oomph Setup Task\">\r\n" + //
" <requirement name=\"org.eclipse.oomph.setup.feature.group\"/>\r\n" + //
" <repository url=\"${oomph.update.url}\"/>\r\n" + //
" </setupTask> \r\n" + //
" </installation>\r\n" + //
"</setup:Configuration>";
XMLAssert.testDiagnosticsFor(xml);
}

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

0 comments on commit e770ac1

Please sign in to comment.