-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
validate element in plugin <configuration> #30
Merged
AObuchow
merged 1 commit into
eclipse-lemminx:master
from
AObuchow:validate_plugin_configuration
Apr 28, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
95 changes: 95 additions & 0 deletions
95
lemminx-maven/src/main/java/org/eclipse/lemminx/maven/PluginValidator.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,95 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019-2020 Red Hat Inc. and others. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.maven; | ||
AObuchow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import java.util.List; | ||
|
||
import org.apache.maven.plugin.MavenPluginManager; | ||
import org.apache.maven.plugin.descriptor.Parameter; | ||
import org.eclipse.lemminx.dom.DOMNode; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.DiagnosticSeverity; | ||
|
||
public class PluginValidator { | ||
private final MavenProjectCache cache; | ||
private final MavenPluginManager pluginManager; | ||
|
||
public PluginValidator(MavenProjectCache cache, MavenPluginManager pluginManager) { | ||
this.cache = cache; | ||
this.pluginManager = pluginManager; | ||
} | ||
|
||
public Diagnostic validateConfiguration(DiagnosticRequest diagnosticRequest) { | ||
DOMNode node = diagnosticRequest.getNode(); | ||
if (node == null) { | ||
return null; | ||
} | ||
if (node.isElement() && node.hasChildNodes()) { | ||
List<Parameter> parameters = MavenPluginUtils.collectPluginConfigurationParameters(diagnosticRequest, cache, | ||
pluginManager); | ||
for (DOMNode childNode : node.getChildren()) { | ||
DiagnosticRequest childDiagnosticReq = new DiagnosticRequest(childNode, | ||
diagnosticRequest.getXMLDocument(), diagnosticRequest.getDiagnostics()); | ||
Diagnostic diag = internalValidateConfiguration(childDiagnosticReq, parameters); | ||
if (diag != null) { | ||
diagnosticRequest.getDiagnostics().add(diag); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public Diagnostic validateGoal(DiagnosticRequest diagnosticRequest) { | ||
DOMNode node = diagnosticRequest.getNode(); | ||
if (node == null) { | ||
return null; | ||
} | ||
if (node.isElement() && node.hasChildNodes()) { | ||
List<Parameter> parameters = MavenPluginUtils.collectPluginConfigurationParameters(diagnosticRequest, cache, | ||
pluginManager); | ||
Diagnostic diag = internalValidateGoal(diagnosticRequest, parameters); | ||
if (diag != null) { | ||
diagnosticRequest.getDiagnostics().add(diag); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static Diagnostic internalValidateGoal(DiagnosticRequest diagnosticReq, List<Parameter> parameters) { | ||
DOMNode node = diagnosticReq.getNode(); | ||
if (!node.hasChildNodes()) { | ||
AObuchow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
for (Parameter parameter : parameters) { | ||
if (node.getChild(0).getNodeValue().equals(parameter.getName())) { | ||
return null; | ||
} | ||
} | ||
return new Diagnostic(diagnosticReq.getRange(), "Invalid goal for this plugin", DiagnosticSeverity.Warning, | ||
diagnosticReq.getXMLDocument().getDocumentURI(), "XML"); | ||
|
||
} | ||
|
||
private static Diagnostic internalValidateConfiguration(DiagnosticRequest diagnosticReq, | ||
List<Parameter> parameters) { | ||
DOMNode node = diagnosticReq.getNode(); | ||
if (node.getLocalName() == null) { | ||
return null; | ||
} | ||
for (Parameter parameter : parameters) { | ||
if (node.getLocalName().equals(parameter.getName())) { | ||
return null; | ||
} | ||
} | ||
return new Diagnostic(diagnosticReq.getRange(), "Invalid plugin configuration: " + diagnosticReq.getCurrentTag(), DiagnosticSeverity.Warning, | ||
diagnosticReq.getXMLDocument().getDocumentURI(), "XML"); | ||
|
||
} | ||
|
||
} |
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
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
33 changes: 33 additions & 0 deletions
33
lemminx-maven/src/test/resources/pom-plugin-configuration-diagnostic.xml
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,33 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.test</groupId> | ||
<artifactId>test</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.2</version> <!-- Same version as in actual pom.xml that builds project --> | ||
<configuration> | ||
<dependenciesToScan> | ||
<dependency>org.acme:project-a</dependency> | ||
</dependenciesToScan> | ||
<notValidConfig><!-- This tag should cause a warning --></notValidConfig> | ||
<anotherNotValidConfig><!-- This tag should cause a warning --></anotherNotValidConfig> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<repositories> | ||
<repository> | ||
<id>soffid</id> | ||
<layout>m2</layout> | ||
<url>http://www.soffid.com/maven/</url> | ||
</repository> | ||
</repositories> | ||
|
||
</project> |
37 changes: 37 additions & 0 deletions
37
lemminx-maven/src/test/resources/pom-plugin-goal-diagnostic.xml
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,37 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.test</groupId> | ||
<artifactId>test</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>pom</packaging> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.2</version> <!-- Need to be same as in lemminx-maven pom so we're sure version in in local repo --> | ||
<executions> | ||
<execution> | ||
<id>execution1</id> | ||
<goals><goal>test</goal></goals> | ||
</execution> | ||
<execution> | ||
<id>execution2</id> | ||
<goals><goal>invalid-goal</goal></goals> | ||
<configuration> | ||
|
||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>execution3</id> | ||
<goals><goal>another-invalid-goal</goal></goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mickaelistria I think this patch is good, except I dislike the fact that I left some methods unimplemented. It should be ok for now, but calling these functions could lead to problems for DiagnosticRequest's.