-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jessica He <jhe@redhat.com>
- Loading branch information
1 parent
9827ac2
commit 39eefb7
Showing
7 changed files
with
211 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...rc/main/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogDiagnosticsParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.lemminx.extensions.catalog; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings; | ||
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant; | ||
import org.eclipse.lemminx.utils.DOMUtils; | ||
import org.eclipse.lemminx.utils.FilesUtils; | ||
import org.eclipse.lemminx.utils.URIUtils; | ||
import org.eclipse.lemminx.utils.XMLPositionUtility; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.DiagnosticSeverity; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
|
||
/** | ||
* Validate XML catalog. | ||
* | ||
*/ | ||
public class XMLCatalogDiagnosticsParticipant implements IDiagnosticsParticipant { | ||
|
||
private static final String ERROR_STRING = "The file ''{0}'' cannot be found."; | ||
|
||
@Override | ||
public void doDiagnostics(DOMDocument xmlDocument, List<Diagnostic> diagnostics, | ||
XMLValidationSettings validationSettings, CancelChecker monitor) { | ||
if (!DOMUtils.isCatalog(xmlDocument)) { | ||
return; | ||
} | ||
|
||
for (CatalogEntry catalogEntry : CatalogUtils.getCatalogEntries(xmlDocument)) { | ||
// CatalogUtils.getResolvedLocation() always returns path with 'file://' scheme, | ||
// appending it in the case when original URI does not start with 'file://'. | ||
// Ex: originalURI ="foo/bar.xsd" -> path ="file://foo/bar.xsd" | ||
String path = CatalogUtils.getResolvedLocation(xmlDocument, catalogEntry); | ||
if (!FilesUtils.isValidPath(FilesUtils.getPath(path)) && URIUtils.isFileResource(path)) { | ||
Range range = XMLPositionUtility.selectValueWithoutQuote(catalogEntry.getLinkRange()); | ||
String msg = MessageFormat.format(ERROR_STRING, catalogEntry.getResolvedURI()); | ||
|
||
diagnostics | ||
.add(new Diagnostic(range, msg, DiagnosticSeverity.Error, xmlDocument.getDocumentURI(), | ||
XMLCatalogErrorCode.catalog_uri.name())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...pse.lemminx/src/main/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.lemminx.extensions.catalog; | ||
|
||
import org.eclipse.lemminx.services.extensions.diagnostics.IXMLErrorCode; | ||
|
||
/** | ||
* XML Catalog error code. | ||
* | ||
*/ | ||
public enum XMLCatalogErrorCode implements IXMLErrorCode { | ||
catalog_uri("catalog_uri"); | ||
|
||
private final String code; | ||
|
||
private XMLCatalogErrorCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
@Override | ||
public String getCode() { | ||
if (code == null) { | ||
return name(); | ||
} | ||
return code; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...mminx/src/test/java/org/eclipse/lemminx/extensions/catalog/XMLCatalogDiagnosticsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.eclipse.lemminx.extensions.catalog; | ||
|
||
import static org.eclipse.lemminx.XMLAssert.d; | ||
|
||
import org.eclipse.lemminx.XMLAssert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class XMLCatalogDiagnosticsTest { | ||
@Test | ||
public void testCatalogWithValidFile() { | ||
String xml = "<?xml version=\"1.0\"?>\n" + // | ||
"<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + // | ||
"<system systemId=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + // | ||
"uri=\"src/test/resources/xsd/spring-beans-3.0.xsd\" />" + // | ||
"</catalog>"; | ||
XMLAssert.testDiagnosticsFor(xml); | ||
} | ||
|
||
@Test | ||
public void testCatalogWithInvalidFile() { | ||
String xml = "<?xml version=\"1.0\"?>\n" + // | ||
"<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + // | ||
"<system systemId=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + // | ||
"uri=\"src/test/resources/xsd/spring-beans-3.0ABCDE.xsd\" />" + // | ||
"</catalog>"; | ||
XMLAssert.testDiagnosticsFor(xml, d(3, 5, 3, 53, XMLCatalogErrorCode.catalog_uri)); | ||
} | ||
|
||
@Test | ||
public void testCatalogWithHttpLink() { | ||
String xml = "<?xml version=\"1.0\"?>\n" + // | ||
"<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + // | ||
"<system systemId=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + // | ||
"uri=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\" />" + // | ||
"</catalog>"; | ||
XMLAssert.testDiagnosticsFor(xml); | ||
} | ||
|
||
@Test | ||
public void testCatalogEntryWithXMLBaseGroupValidFile() { | ||
String xml = "<?xml version=\"1.0\"?>\n" + // | ||
" <catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\" xml:base= \"src\">\n" + // | ||
" <group xml:base=\"test/resources/xsd/\">\n" + // | ||
" <system systemId=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + // | ||
" uri=\"spring-beans-3.0.xsd\" />" + // | ||
" </group>" + // | ||
" </catalog>"; | ||
XMLAssert.testDiagnosticsFor(xml); | ||
} | ||
|
||
@Test | ||
public void testCatalogEntryWithXMLBaseGroupInvalidFile() { | ||
String xml = "<?xml version=\"1.0\"?>\n" + // | ||
" <catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\" xml:base= \"src\">\n" + // | ||
" <group xml:base=\"test/resources/xsd/\">\n" + // | ||
" <system systemId=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + // | ||
" uri=\"spring-beans-3.0ABCDE.xsd\" />" + // | ||
" </group>" + // | ||
" </catalog>"; | ||
XMLAssert.testDiagnosticsFor(xml, d(4, 11, 4, 36, XMLCatalogErrorCode.catalog_uri)); | ||
} | ||
|
||
@Test | ||
public void testCatalogEntryWithXMLBaseGroupInvalidBase() { | ||
String xml = "<?xml version=\"1.0\"?>\n" + // | ||
" <catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\" xml:base= \"srcABCDE\">\n" + // | ||
" <group xml:base=\"test/resources/xsd/\">\n" + // | ||
" <system systemId=\"http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"\n" + // | ||
" uri=\"spring-beans-3.0.xsd\" />" + // | ||
" </group>" + // | ||
" </catalog>"; | ||
XMLAssert.testDiagnosticsFor(xml, d(4, 11, 4, 31, XMLCatalogErrorCode.catalog_uri)); | ||
} | ||
} |