下面列出了怎么用org.eclipse.lsp4j.DiagnosticSeverity的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the <code>noGrammar</code> severity according the given settings and
* {@link DiagnosticSeverity#Hint} otherwise.
*
* @param settings the settings
* @return the <code>noGrammar</code> severity according the given settings and
* {@link DiagnosticSeverity#Hint} otherwise.
*/
public static DiagnosticSeverity getNoGrammarSeverity(ContentModelSettings settings) {
DiagnosticSeverity defaultSeverity = DiagnosticSeverity.Hint;
if (settings == null || settings.getValidation() == null) {
return defaultSeverity;
}
XMLValidationSettings problems = settings.getValidation();
String noGrammar = problems.getNoGrammar();
if ("ignore".equalsIgnoreCase(noGrammar)) {
// Ignore "noGrammar", return null.
return null;
} else if ("info".equalsIgnoreCase(noGrammar)) {
return DiagnosticSeverity.Information;
} else if ("warning".equalsIgnoreCase(noGrammar)) {
return DiagnosticSeverity.Warning;
} else if ("error".equalsIgnoreCase(noGrammar)) {
return DiagnosticSeverity.Error;
}
return defaultSeverity;
}
public Collection<Diagnostic> converToLSPDiagnostics(Map<String, ConfigurationPropertiesValidationResult> configurationPropertiesErrors) {
List<Diagnostic> lspDiagnostics = new ArrayList<>();
for (Map.Entry<String, ConfigurationPropertiesValidationResult> errorEntry : configurationPropertiesErrors.entrySet()) {
ConfigurationPropertiesValidationResult validationResult = errorEntry.getValue();
String lineContentInError = errorEntry.getKey();
List<Diagnostic> unknownParameterDiagnostics = computeUnknowParameters(validationResult, lineContentInError);
lspDiagnostics.addAll(unknownParameterDiagnostics);
List<Diagnostic> invalidEnumDiagnostics = computeInvalidEnumsDiagnostic(validationResult, lineContentInError);
lspDiagnostics.addAll(invalidEnumDiagnostics);
if (invalidEnumDiagnostics.size() + unknownParameterDiagnostics.size() < validationResult.getNumberOfErrors()) {
lspDiagnostics.add(new Diagnostic(
computeRange(validationResult, lineContentInError, lineContentInError),
computeErrorMessage(validationResult),
DiagnosticSeverity.Error,
APACHE_CAMEL_VALIDATION,
null));
}
}
return lspDiagnostics;
}
private List<Diagnostic> computeUnknowParameters(ConfigurationPropertiesValidationResult validationResult, String lineContentInError) {
List<Diagnostic> lspDiagnostics = new ArrayList<>();
Set<String> unknownParameters = validationResult.getUnknown();
if (unknownParameters != null) {
for (String unknownParameter : unknownParameters) {
int lastIndexOf = unknownParameter.lastIndexOf('.');
String realValueOfUnknowparameter;
if(lastIndexOf != -1) {
realValueOfUnknowparameter = unknownParameter.substring(lastIndexOf + 1);
} else {
realValueOfUnknowparameter = unknownParameter;
}
lspDiagnostics.add(new Diagnostic(
computeRange(validationResult, lineContentInError, realValueOfUnknowparameter),
new UnknownErrorMsg().getErrorMessage(unknownParameter),
DiagnosticSeverity.Error,
APACHE_CAMEL_VALIDATION,
ERROR_CODE_UNKNOWN_PROPERTIES));
}
}
return lspDiagnostics;
}
public List<Diagnostic> converToLSPDiagnostics(String fullCamelText, Map<CamelEndpointDetails, EndpointValidationResult> endpointErrors, TextDocumentItem textDocumentItem) {
List<Diagnostic> lspDiagnostics = new ArrayList<>();
for (Map.Entry<CamelEndpointDetails, EndpointValidationResult> endpointError : endpointErrors.entrySet()) {
EndpointValidationResult validationResult = endpointError.getValue();
CamelEndpointDetails camelEndpointDetails = endpointError.getKey();
List<Diagnostic> unknownParameterDiagnostics = computeUnknowParameters(fullCamelText, textDocumentItem, validationResult, camelEndpointDetails);
lspDiagnostics.addAll(unknownParameterDiagnostics);
List<Diagnostic> invalidEnumDiagnostics = computeInvalidEnumsDiagnostic(fullCamelText, textDocumentItem, validationResult, camelEndpointDetails);
lspDiagnostics.addAll(invalidEnumDiagnostics);
if (invalidEnumDiagnostics.size() + unknownParameterDiagnostics.size() < validationResult.getNumberOfErrors()) {
lspDiagnostics.add(new Diagnostic(
computeRange(fullCamelText, textDocumentItem, camelEndpointDetails),
computeErrorMessage(validationResult),
DiagnosticSeverity.Error,
APACHE_CAMEL_VALIDATION,
null));
}
}
return lspDiagnostics;
}
@Test
public void testMavenMarkers() throws Exception {
String msg1 = "Some dependency is missing";
IMarker m1 = createMavenMarker(IMarker.SEVERITY_ERROR, msg1, 2, 95, 100);
IDocument d = mock(IDocument.class);
when(d.getLineOffset(1)).thenReturn(90);
List<Diagnostic> diags = WorkspaceDiagnosticsHandler.toDiagnosticsArray(d, new IMarker[] { m1, null }, true);
assertEquals(1, diags.size());
Range r;
Diagnostic d1 = diags.get(0);
assertEquals(msg1, d1.getMessage());
assertEquals(DiagnosticSeverity.Error, d1.getSeverity());
r = d1.getRange();
assertEquals(1, r.getStart().getLine());
assertEquals(95, r.getStart().getCharacter());
assertEquals(1, r.getEnd().getLine());
assertEquals(100, r.getEnd().getCharacter());
}
private void testDiagnostic(List<PublishDiagnosticsParams> allCalls) {
List<Diagnostic> projectDiags = new ArrayList<>();
List<Diagnostic> pomDiags = new ArrayList<>();
for (PublishDiagnosticsParams diag : allCalls) {
if (diag.getUri().endsWith("maven/salut")) {
projectDiags.addAll(diag.getDiagnostics());
} else if (diag.getUri().endsWith("pom.xml")) {
pomDiags.addAll(diag.getDiagnostics());
}
}
assertTrue("No maven/salut errors were found", projectDiags.size() > 0);
Optional<Diagnostic> projectDiag = projectDiags.stream().filter(p -> p.getMessage().contains("references non existing library")).findFirst();
assertTrue("No 'references non existing library' diagnostic", projectDiag.isPresent());
assertEquals(projectDiag.get().getSeverity(), DiagnosticSeverity.Error);
assertTrue("No pom.xml errors were found", pomDiags.size() > 0);
Optional<Diagnostic> pomDiag = pomDiags.stream().filter(p -> p.getMessage().startsWith("Missing artifact")).findFirst();
assertTrue("No 'missing artifact' diagnostic", pomDiag.isPresent());
assertTrue(pomDiag.get().getMessage().startsWith("Missing artifact"));
assertEquals(pomDiag.get().getRange().getStart().getLine(), 19);
assertEquals(pomDiag.get().getRange().getStart().getCharacter(), 3);
assertEquals(pomDiag.get().getRange().getEnd().getLine(), 19);
assertEquals(pomDiag.get().getRange().getEnd().getCharacter(), 14);
assertEquals(pomDiag.get().getSeverity(), DiagnosticSeverity.Error);
}
private ProblemHighlightType getHighlighType(DiagnosticSeverity severity) {
switch (severity) {
case Error:
return ProblemHighlightType.ERROR;
case Hint:
case Information:
return ProblemHighlightType.INFORMATION;
case Warning:
return ProblemHighlightType.WARNING;
}
return ProblemHighlightType.INFORMATION;
}
private Color getColor(DiagnosticSeverity severity) {
switch (severity) {
case Hint:
return Color.GRAY;
case Error:
return Color.RED;
case Information:
return Color.GRAY;
case Warning:
return Color.YELLOW;
}
return Color.GRAY;
}
public Diagnostic createDiagnostic(String uri, String message, Range range, String source, IJavaErrorCode code) {
Diagnostic diagnostic = new Diagnostic();
diagnostic.setSource(source);
diagnostic.setMessage(message);
diagnostic.setSeverity(DiagnosticSeverity.Warning);
diagnostic.setRange(range);
if (code != null) {
diagnostic.setCode(code.getCode());
}
return diagnostic;
}
@Test
public void testRestClientAnnotationMissingForInterface() throws Exception {
ApplicationManager.getApplication().invokeAndWait(() -> {
Module module = getModule("rest-client-quickstart.main");
MicroProfileJavaDiagnosticsParams params = new MicroProfileJavaDiagnosticsParams();
VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) + "/src/main/java/org/acme/restclient/MyService.java");
String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString();
params.setUris(Arrays.asList(uri));
params.setDocumentFormat(DocumentFormat.Markdown);
Diagnostic d = d(2, 17, 26,
"The interface `MyService` does not have the @RegisterRestClient annotation. The 1 fields references will not be injected as CDI beans.",
DiagnosticSeverity.Warning, MicroProfileRestClientConstants.DIAGNOSTIC_SOURCE,
MicroProfileRestClientErrorCode.RegisterRestClientAnnotationMissing);
assertJavaDiagnostics(params, utils, //
d);
/*String uri = javaFile.getLocation().toFile().toURI().toString();
MicroProfileJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
assertJavaCodeAction(codeActionParams, utils, //
ca(uri, "Insert @RegisterRestClient", d, //
te(0, 28, 2, 0,
"\r\n\r\nimport org.eclipse.microprofile.rest.client.inject.RegisterRestClient;\r\n\r\[email protected]\r\n")));*/
});
}
@Test
public void testImplementHealthCheck() throws Exception {
Module module = getModule("microprofile-health-quickstart.main");
MicroProfileJavaDiagnosticsParams diagnosticsParams = new MicroProfileJavaDiagnosticsParams();
VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) + "/src/main/java/org/acme/health/DontImplementHealthCheck.java");
String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString();
diagnosticsParams.setUris(Arrays.asList(uri));
diagnosticsParams.setDocumentFormat(DocumentFormat.Markdown);
Diagnostic d = d(9, 13, 37,
"The class `org.acme.health.DontImplementHealthCheck` using the @Liveness, @Readiness, or @Health annotation should implement the HealthCheck interface.",
DiagnosticSeverity.Warning, MicroProfileHealthConstants.DIAGNOSTIC_SOURCE,
MicroProfileHealthErrorCode.ImplementHealthCheck);
assertJavaDiagnostics(diagnosticsParams, utils, //
d);
/*String uri = javaFile.getUrl();
MicroProfileJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
assertJavaCodeAction(codeActionParams, utils, //
ca(uri, "Let 'DontImplementHealthCheck' implement 'org.eclipse.microprofile.health.HealthCheck'", d, //
te(2, 50, 9, 37, "\r\n\r\n" + //
"import org.eclipse.microprofile.health.HealthCheck;\r\n" + //
"import org.eclipse.microprofile.health.HealthCheckResponse;\r\n" + //
"import org.eclipse.microprofile.health.Liveness;\r\n\r\[email protected]\r\n" + //
"@ApplicationScoped\r\n" + //
"public class DontImplementHealthCheck implements HealthCheck")));*/
}
@Test
public void testHealthAnnotationMissing() throws Exception {
Module module = getModule("microprofile-health-quickstart.main");
MicroProfileJavaDiagnosticsParams diagnosticsParams = new MicroProfileJavaDiagnosticsParams();
VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) + "/src/main/java/org/acme/health/ImplementHealthCheck.java");
String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString();
diagnosticsParams.setUris(Arrays.asList(uri));
diagnosticsParams.setDocumentFormat(DocumentFormat.Markdown);
Diagnostic d = d(5, 13, 33,
"The class `org.acme.health.ImplementHealthCheck` implementing the HealthCheck interface should use the @Liveness, @Readiness, or @Health annotation.",
DiagnosticSeverity.Warning, MicroProfileHealthConstants.DIAGNOSTIC_SOURCE,
MicroProfileHealthErrorCode.HealthAnnotationMissing);
assertJavaDiagnostics(diagnosticsParams, utils, //
d);
/*MicroProfileJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
assertJavaCodeAction(codeActionParams, utils, //
ca(uri, "Insert @Health", d, //
te(2, 0, 5, 0, "import org.eclipse.microprofile.health.Health;\r\n" + //
"import org.eclipse.microprofile.health.HealthCheck;\r\n" + //
"import org.eclipse.microprofile.health.HealthCheckResponse;\r\n\r\n" + //
"@Health\r\n")),
ca(uri, "Insert @Liveness", d, //
te(3, 59, 5, 0, "\r\n" + //
"import org.eclipse.microprofile.health.Liveness;\r\n\r\n" + //
"@Liveness\r\n")), //
ca(uri, "Insert @Readiness", d, //
te(3, 59, 5, 0, "\r\n" + //
"import org.eclipse.microprofile.health.Readiness;\r\n\r\n" + //
"@Readiness\r\n")) //
);*/
}
@Test
public void testImplementHealthCheck() throws Exception {
Module module = createMavenModule("microprofile-health-quickstart", new File("projects/maven/microprofile-health-quickstart"));
MicroProfileJavaDiagnosticsParams diagnosticsParams = new MicroProfileJavaDiagnosticsParams();
VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) + "/src/main/java/org/acme/health/DontImplementHealthCheck.java");
String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString();
diagnosticsParams.setUris(Arrays.asList(uri));
diagnosticsParams.setDocumentFormat(DocumentFormat.Markdown);
Diagnostic d = d(9, 13, 37,
"The class `org.acme.health.DontImplementHealthCheck` using the @Liveness, @Readiness, or @Health annotation should implement the HealthCheck interface.",
DiagnosticSeverity.Warning, MicroProfileHealthConstants.DIAGNOSTIC_SOURCE,
MicroProfileHealthErrorCode.ImplementHealthCheck);
assertJavaDiagnostics(diagnosticsParams, utils, //
d);
/*String uri = javaFile.getUrl();
MicroProfileJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
assertJavaCodeAction(codeActionParams, utils, //
ca(uri, "Let 'DontImplementHealthCheck' implement 'org.eclipse.microprofile.health.HealthCheck'", d, //
te(2, 50, 9, 37, "\r\n\r\n" + //
"import org.eclipse.microprofile.health.HealthCheck;\r\n" + //
"import org.eclipse.microprofile.health.HealthCheckResponse;\r\n" + //
"import org.eclipse.microprofile.health.Liveness;\r\n\r\[email protected]\r\n" + //
"@ApplicationScoped\r\n" + //
"public class DontImplementHealthCheck implements HealthCheck")));*/
}
@Test
public void testHealthAnnotationMissing() throws Exception {
Module module = createMavenModule("microprofile-health-quickstart", new File("projects/maven/microprofile-health-quickstart"));
MicroProfileJavaDiagnosticsParams diagnosticsParams = new MicroProfileJavaDiagnosticsParams();
VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) + "/src/main/java/org/acme/health/ImplementHealthCheck.java");
String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString();
diagnosticsParams.setUris(Arrays.asList(uri));
diagnosticsParams.setDocumentFormat(DocumentFormat.Markdown);
Diagnostic d = d(5, 13, 33,
"The class `org.acme.health.ImplementHealthCheck` implementing the HealthCheck interface should use the @Liveness, @Readiness, or @Health annotation.",
DiagnosticSeverity.Warning, MicroProfileHealthConstants.DIAGNOSTIC_SOURCE,
MicroProfileHealthErrorCode.HealthAnnotationMissing);
assertJavaDiagnostics(diagnosticsParams, utils, //
d);
/*MicroProfileJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
assertJavaCodeAction(codeActionParams, utils, //
ca(uri, "Insert @Health", d, //
te(2, 0, 5, 0, "import org.eclipse.microprofile.health.Health;\r\n" + //
"import org.eclipse.microprofile.health.HealthCheck;\r\n" + //
"import org.eclipse.microprofile.health.HealthCheckResponse;\r\n\r\n" + //
"@Health\r\n")),
ca(uri, "Insert @Liveness", d, //
te(3, 59, 5, 0, "\r\n" + //
"import org.eclipse.microprofile.health.Liveness;\r\n\r\n" + //
"@Liveness\r\n")), //
ca(uri, "Insert @Readiness", d, //
te(3, 59, 5, 0, "\r\n" + //
"import org.eclipse.microprofile.health.Readiness;\r\n\r\n" + //
"@Readiness\r\n")) //
);*/
}
@Test
public void testRestClientAnnotationMissingForInterface() throws Exception {
Module module = createMavenModule("rest-client-quickstart", new File("projects/maven/rest-client-quickstart"));
IPsiUtils utils = PsiUtilsImpl.getInstance();
MicroProfileJavaDiagnosticsParams params = new MicroProfileJavaDiagnosticsParams();
VirtualFile javaFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(ModuleUtilCore.getModuleDirPath(module) + "/src/main/java/org/acme/restclient/MyService.java");
String uri = VfsUtilCore.virtualToIoFile(javaFile).toURI().toString();
params.setUris(Arrays.asList(uri));
params.setDocumentFormat(DocumentFormat.Markdown);
Diagnostic d = d(2, 17, 26,
"The interface `MyService` does not have the @RegisterRestClient annotation. The 1 fields references will not be injected as CDI beans.",
DiagnosticSeverity.Warning, MicroProfileRestClientConstants.DIAGNOSTIC_SOURCE,
MicroProfileRestClientErrorCode.RegisterRestClientAnnotationMissing);
assertJavaDiagnostics(params, utils, //
d);
/*String uri = javaFile.getLocation().toFile().toURI().toString();
MicroProfileJavaCodeActionParams codeActionParams = createCodeActionParams(uri, d);
assertJavaCodeAction(codeActionParams, utils, //
ca(uri, "Insert @RegisterRestClient", d, //
te(0, 28, 2, 0,
"\r\n\r\nimport org.eclipse.microprofile.rest.client.inject.RegisterRestClient;\r\n\r\[email protected]\r\n")));*/
}
private static void publishOneDiagnosticInRoot(DOMDocument document, String message, DiagnosticSeverity severity,
Consumer<PublishDiagnosticsParams> publishDiagnostics) {
String uri = document.getDocumentURI();
DOMElement documentElement = document.getDocumentElement();
Range range = XMLPositionUtility.selectStartTagName(documentElement);
List<Diagnostic> diagnostics = new ArrayList<>();
diagnostics.add(new Diagnostic(range, message, severity, "XML"));
publishDiagnostics.accept(new PublishDiagnosticsParams(uri, diagnostics));
}
public boolean addDiagnostic(Range adjustedRange, String message, DiagnosticSeverity severity, String key) {
Diagnostic d = new Diagnostic(adjustedRange, message, severity, source, key);
if (diagnostics.contains(d)) {
return false;
}
// Fill diagnostic
diagnostics.add(d);
return true;
}
/**
* Returns the LSP diagnostic severity according the SAX severity.
*
* @param severity the SAX severity
* @return the LSP diagnostic severity according the SAX severity.
*/
private static DiagnosticSeverity toLSPSeverity(int severity) {
switch (severity) {
case SEVERITY_WARNING:
return DiagnosticSeverity.Warning;
default:
return DiagnosticSeverity.Error;
}
}
@Test
public void schemaWithUrlWithoutCache() 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" + //
"</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'.",
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'.",
DiagnosticSeverity.Error, "xml", "cvc-elt.1.a")));
}
@Test
public void schemaWithUrlInvalidPathWithNamespace() 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:schemaLocation=\"http://invoice.xsd\">\r\n" + //
"</invoice> \r\n" + //
"";
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, configuration, pd(fileURI, //
new Diagnostic(r(2, 20, 2, 40),
"SchemaLocation: schemaLocation value = 'http://invoice.xsd' must have even number of URI's.",
DiagnosticSeverity.Warning, "xml", "SchemaLocation"), //
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 schemaWithUrlWithCache() throws Exception {
// Here we test the following context:
// - XML which have xsi:noNamespaceSchemaLocation="http://invoice.xsd"
// - XMLCacheResolverExtension which is enabled
// Result of test is to have 2 published diagnostics (resource downloading as
// info and error downloading).
Consumer<XMLLanguageService> configuration = ls -> {
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
// Use cache on file system
contentModelManager.setUseCache(true);
};
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" + //
"</invoice> \r\n" + //
"";
String expectedLocation = TEST_WORK_DIRECTORY.resolve("cache/http/invoice.xsd").toString();
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, configuration,
pd(fileURI,
new Diagnostic(r(1, 1, 1, 8), "The resource 'http://invoice.xsd' is downloading.",
DiagnosticSeverity.Information, "XML")),
pd(fileURI, new Diagnostic(r(1, 1, 1, 8), "Error while downloading 'http://invoice.xsd' to "+expectedLocation+".",
DiagnosticSeverity.Error, "XML")));
}
private List<Diagnostic> computeInvalidEnumsDiagnostic(ConfigurationPropertiesValidationResult validationResult, String lineContentInError) {
List<Diagnostic> lspDiagnostics = new ArrayList<>();
Map<String, String> invalidEnums = validationResult.getInvalidEnum();
if (invalidEnums != null) {
for (Entry<String, String> invalidEnum : invalidEnums.entrySet()) {
lspDiagnostics.add(new Diagnostic(
computeRange(validationResult, lineContentInError, invalidEnum.getKey()),
new EnumErrorMsg().getErrorMessage(validationResult, invalidEnum),
DiagnosticSeverity.Error,
APACHE_CAMEL_VALIDATION,
ERROR_CODE_INVALID_ENUM));
}
}
return lspDiagnostics;
}
private List<Diagnostic> computeInvalidEnumsDiagnostic(String fullCamelText, TextDocumentItem textDocumentItem, EndpointValidationResult validationResult, CamelEndpointDetails camelEndpointDetails) {
List<Diagnostic> lspDiagnostics = new ArrayList<>();
Map<String, String> invalidEnums = validationResult.getInvalidEnum();
if (invalidEnums != null) {
for (Entry<String, String> invalidEnum : invalidEnums.entrySet()) {
lspDiagnostics.add(new Diagnostic(
computeRange(fullCamelText, textDocumentItem, camelEndpointDetails, invalidEnum),
new EnumErrorMsg().getErrorMessage(validationResult, invalidEnum),
DiagnosticSeverity.Error,
APACHE_CAMEL_VALIDATION,
ERROR_CODE_INVALID_ENUM));
}
}
return lspDiagnostics;
}
private List<Diagnostic> computeUnknowParameters(String fullCamelText, TextDocumentItem textDocumentItem, EndpointValidationResult validationResult, CamelEndpointDetails camelEndpointDetails) {
List<Diagnostic> lspDiagnostics = new ArrayList<>();
Set<String> unknownParameters = validationResult.getUnknown();
if (unknownParameters != null) {
for (String unknownParameter : unknownParameters) {
lspDiagnostics.add(new Diagnostic(
computeRange(fullCamelText, textDocumentItem, camelEndpointDetails, unknownParameter),
new UnknownErrorMsg().getErrorMessage(unknownParameter),
DiagnosticSeverity.Error,
APACHE_CAMEL_VALIDATION,
ERROR_CODE_UNKNOWN_PROPERTIES));
}
}
return lspDiagnostics;
}
private DiagnosticSeverity convertLintProblemLevel2DiagnosticSeverity(ErrorLevel lv) {
DiagnosticSeverity severity;
switch (lv) {
case ERROR:
severity = DiagnosticSeverity.Error;
break;
case WARN:
severity = DiagnosticSeverity.Warning;
break;
default:
severity = DiagnosticSeverity.Information;
}
return severity;
}
private String getShortSeverity(DiagnosticSeverity severity) {
switch (severity) {
case Error:
return "ERR";
case Warning:
return "WRN";
case Information:
return "INF";
case Hint:
return "HNT";
}
return "???";
}
/**
* Convert the {@link Severity} to a lsp {@link DiagnosticSeverity}.
*
* Defaults to severity {@link DiagnosticSeverity#Hint}.
*/
protected DiagnosticSeverity toSeverity(Severity severity) {
switch (severity) {
case ERROR:
return DiagnosticSeverity.Error;
case IGNORE:
return DiagnosticSeverity.Hint;
case INFO:
return DiagnosticSeverity.Information;
case WARNING:
return DiagnosticSeverity.Warning;
default:
return DiagnosticSeverity.Hint;
}
}
/**
* Convert the lsp {@link DiagnosticSeverity} to an Xtext {@link Severity}.
*
* Defaults to severity {@link Severity#IGNORE}.
*/
protected Severity toSeverity(DiagnosticSeverity severity) {
switch (severity) {
case Error:
return Severity.ERROR;
case Hint:
return Severity.IGNORE;
case Information:
return Severity.INFO;
case Warning:
return Severity.WARNING;
default:
return Severity.IGNORE;
}
}
public void addException(Token startToken, Token endToken, String errorMessage) {
Position startPosition = new Position(startToken.beginLine, startToken.beginColumn);
Position endPosition = new Position(endToken.endLine, endToken.endColumn + 1);
DdlAnalyzerException exception = new DdlAnalyzerException(DiagnosticSeverity.Error, errorMessage,
new Range(startPosition, endPosition)); // $NON-NLS-1$);
this.addException(exception);
}