-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate element in plugin <configuration> and plugin goals
Fix #9 Signed-off-by: Andrew Obuchowicz <aobuchow@redhat.com>
- Loading branch information
Showing
9 changed files
with
245 additions
and
26 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
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; | ||
|
||
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()) { | ||
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> |