Skip to content

Commit 8ae6fc6

Browse files
committed
Add XMLProblems (see #218)
1 parent a43a43a commit 8ae6fc6

File tree

4 files changed

+81
-27
lines changed

4 files changed

+81
-27
lines changed

org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/diagnostics/XMLValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.eclipse.lsp4xml.dom.Element;
3232
import org.eclipse.lsp4xml.dom.XMLDocument;
3333
import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings;
34+
import org.eclipse.lsp4xml.extensions.contentmodel.settings.XMLProblems;
3435
import org.eclipse.lsp4xml.services.extensions.diagnostics.LSPContentHandler;
3536
import org.eclipse.lsp4xml.uriresolver.CacheResourceDownloadingException;
3637
import org.eclipse.lsp4xml.uriresolver.IExternalSchemaLocationProvider;
@@ -103,19 +104,18 @@ public static void doDiagnostics(XMLDocument document, XMLEntityResolver entityR
103104
/**
104105
* Warn if XML document is not bound to a grammar according the settings
105106
*
106-
* @param document the XML document
107-
* @param diagnostics the diagnostics list to populate
108-
* @param contentModelSettings the settings to use to know the severity of warn.
107+
* @param document the XML document
108+
* @param diagnostics the diagnostics list to populate
109+
* @param settings the settings to use to know the severity of warn.
109110
*/
110111
private static void warnNoGrammar(XMLDocument document, List<Diagnostic> diagnostics,
111-
ContentModelSettings contentModelSettings) {
112+
ContentModelSettings settings) {
112113
boolean hasGrammar = document.hasGrammar();
113114
if (hasGrammar) {
114115
return;
115116
}
116117
// By default "hint" settings.
117-
DiagnosticSeverity severity = contentModelSettings != null ? contentModelSettings.getNoGrammarSeverity()
118-
: DiagnosticSeverity.Hint;
118+
DiagnosticSeverity severity = XMLProblems.getNoGrammarSeverity(settings);
119119
if (severity == null) {
120120
// "ignore" settings
121121
return;

org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/settings/ContentModelSettings.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
package org.eclipse.lsp4xml.extensions.contentmodel.settings;
1212

13-
import org.eclipse.lsp4j.DiagnosticSeverity;
1413
import org.eclipse.lsp4xml.utils.JSONUtility;
1514

1615
/**
@@ -23,7 +22,7 @@ public class ContentModelSettings {
2322

2423
private String[] catalogs;
2524

26-
private String warnNoGrammar;
25+
private XMLProblems problems;
2726

2827
private XMLFileAssociation[] fileAssociations;
2928

@@ -81,28 +80,16 @@ public XMLFileAssociation[] getFileAssociations() {
8180
return fileAssociations;
8281
}
8382

84-
public static ContentModelSettings getSettings(Object initializationOptionsSettings) {
85-
return JSONUtility.toModel(initializationOptionsSettings, ContentModelSettings.class);
83+
public XMLProblems getProblems() {
84+
return problems;
8685
}
8786

88-
public void setWarnNoGrammar(String warnNoGrammar) {
89-
this.warnNoGrammar = warnNoGrammar;
87+
public void setProblems(XMLProblems problems) {
88+
this.problems = problems;
9089
}
9190

92-
public String getWarnNoGrammar() {
93-
return warnNoGrammar;
91+
public static ContentModelSettings getSettings(Object initializationOptionsSettings) {
92+
return JSONUtility.toModel(initializationOptionsSettings, ContentModelSettings.class);
9493
}
9594

96-
public DiagnosticSeverity getNoGrammarSeverity() {
97-
if ("ignore".equalsIgnoreCase(warnNoGrammar)) {
98-
return null;
99-
} else if ("info".equalsIgnoreCase(warnNoGrammar)) {
100-
return DiagnosticSeverity.Information;
101-
} else if ("warning".equalsIgnoreCase(warnNoGrammar)) {
102-
return DiagnosticSeverity.Warning;
103-
} else if ("error".equalsIgnoreCase(warnNoGrammar)) {
104-
return DiagnosticSeverity.Error;
105-
}
106-
return DiagnosticSeverity.Hint;
107-
}
10895
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2018 Angelo ZERR
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
10+
*/
11+
package org.eclipse.lsp4xml.extensions.contentmodel.settings;
12+
13+
import org.eclipse.lsp4j.DiagnosticSeverity;
14+
15+
/**
16+
* XML problems settings.
17+
*
18+
*/
19+
public class XMLProblems {
20+
21+
/**
22+
* This severity preference to mark the root element of XML document which is
23+
* not bound to a XML Schema/DTD.
24+
*
25+
* Values are {ignore, hint, info, warning, error}
26+
*/
27+
private String noGrammar;
28+
29+
public void setNoGrammar(String noGrammar) {
30+
this.noGrammar = noGrammar;
31+
}
32+
33+
public String getNoGrammar() {
34+
return noGrammar;
35+
}
36+
37+
/**
38+
* Returns the <code>noGrammar</code> severity according the given settings and
39+
* {@link DiagnosticSeverity#Hint} otherwise.
40+
*
41+
* @param settings the settings
42+
* @return the <code>noGrammar</code> severity according the given settings and
43+
* {@link DiagnosticSeverity#Hint} otherwise.
44+
*/
45+
public static DiagnosticSeverity getNoGrammarSeverity(ContentModelSettings settings) {
46+
DiagnosticSeverity defaultSeverity = DiagnosticSeverity.Hint;
47+
if (settings == null || settings.getProblems() == null) {
48+
return defaultSeverity;
49+
}
50+
XMLProblems problems = settings.getProblems();
51+
String noGrammar = problems.getNoGrammar();
52+
if ("ignore".equalsIgnoreCase(noGrammar)) {
53+
// Ignore "noGrammar", return null.
54+
return null;
55+
} else if ("info".equalsIgnoreCase(noGrammar)) {
56+
return DiagnosticSeverity.Information;
57+
} else if ("warning".equalsIgnoreCase(noGrammar)) {
58+
return DiagnosticSeverity.Warning;
59+
} else if ("error".equalsIgnoreCase(noGrammar)) {
60+
return DiagnosticSeverity.Error;
61+
}
62+
return defaultSeverity;
63+
}
64+
}

org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/XMLAssert.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.eclipse.lsp4xml.dom.XMLDocument;
4343
import org.eclipse.lsp4xml.dom.XMLParser;
4444
import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings;
45+
import org.eclipse.lsp4xml.extensions.contentmodel.settings.XMLProblems;
4546
import org.eclipse.lsp4xml.services.XMLLanguageService;
4647
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
4748
import org.eclipse.lsp4xml.services.extensions.diagnostics.IXMLErrorCode;
@@ -234,7 +235,9 @@ public static void testDiagnosticsFor(String xml, String catalogPath, Consumer<X
234235

235236
ContentModelSettings settings = new ContentModelSettings();
236237
settings.setUseCache(false);
237-
settings.setWarnNoGrammar("ignore");
238+
XMLProblems problems = new XMLProblems();
239+
problems.setNoGrammar("ignore");
240+
settings.setProblems(problems);
238241
if (catalogPath != null) {
239242
// Configure XML catalog for XML schema
240243
settings.setCatalogs(new String[] { catalogPath });

0 commit comments

Comments
 (0)