Skip to content

Commit

Permalink
Drop XSLT options from ChangePluginConfiguration (openrewrite#4328)
Browse files Browse the repository at this point in the history
* Drop XSLT options from ChangePluginConfiguration after internal review

> xslt will clobber formatting, should at best be a more general purpose rewrite-xml utility, etc.
> this is going to accrue in features and be hard to revert later

* Add Incubating annotations and add note on formatting

* Drop now unused test resource
  • Loading branch information
timtebeek authored Jul 15, 2024
1 parent c24ec1c commit 0fb7071
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,6 @@ public class ChangePluginConfiguration extends Recipe {
@Nullable
String configuration;

@Nullable
@Language("xml")
@Option(displayName = "XSLT Configuration transformation",
description = "The transformation to be applied on the <configuration> element.",
example = "<xsl:stylesheet ...>...</xsl:stylesheet>",
required = false)
String xslt;

@Nullable
@Option(displayName = "XSLT Configuration transformation classpath resource",
description = "The transformation to be applied on the <configuration> element provided as a classpath resource.",
example = "/changePlugin.xslt",
required = false)
String xsltResource;

@Override
public String getDisplayName() {
return "Change Maven plugin configuration";
Expand All @@ -89,12 +74,6 @@ public String getDescription() {
return "Apply the specified configuration to a Maven plugin. Will not add the plugin if it does not already exist in the pom.";
}

@Override
public Validated<Object> validate() {
return super.validate().and(Validated.test("configuration", "Configuration set at most once", configuration,
cfg -> Stream.of(configuration, xslt, xsltResource).filter(StringUtils::isBlank).count() >= 2));
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenVisitor<ExecutionContext>() {
Expand All @@ -111,39 +90,18 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
.findAny();
if (maybePlugin.isPresent()) {
Xml.Tag plugin = maybePlugin.get();
if (configuration == null && xslt == null && xsltResource == null) {
if (configuration == null) {
plugins = filterChildren(plugins, plugin,
child -> !(child instanceof Xml.Tag && "configuration".equals(((Xml.Tag) child).getName())));
} else if (configuration != null) {
} else {
plugins = addOrUpdateChild(plugins, plugin,
Xml.Tag.build("<configuration>\n" + configuration + "\n</configuration>"),
getCursor().getParentOrThrow());
} else {
// Implied that xslt or xsltResource is not null
Optional<Xml.Tag> configurationTag = plugin.getChild("configuration");
if (configurationTag.isPresent()) {
String xsltTransformation = loadResource(xslt, xsltResource);
plugins = addOrUpdateChild(plugins, plugin,
XsltTransformationVisitor.transformTag(configurationTag.get().printTrimmed(getCursor()), xsltTransformation),
getCursor().getParentOrThrow());
}
}
}
}
return plugins;
}
};
}

private static String loadResource(@Nullable String xslt, @Nullable String xsltResource) {
if (StringUtils.isBlank(xsltResource)) {
return requireNonNull(xslt);
}
try (InputStream is = XsltTransformation.class.getResourceAsStream(xsltResource)) {
assert is != null;
return StringUtils.readFully(is, Charset.defaultCharset());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,18 @@
*/
package org.openrewrite.maven;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.test.RewriteTest;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.openrewrite.maven.Assertions.pomXml;

class ChangePluginConfigurationTest implements RewriteTest {

@Language("xml")
private static String xslt;

@BeforeAll
static void setup() {
xslt = StringUtils.readFully(ChangePluginConfigurationTest.class
.getResourceAsStream("/changePlugin.xslt"));

assertFalse(StringUtils.isBlank(xslt));
}

@DocumentExample
@Test
void removeConfiguration() {
rewriteRun(
spec -> spec.recipe(new ChangePluginConfiguration("org.openrewrite.maven", "rewrite-maven-plugin", null, null, null)),
spec -> spec.recipe(new ChangePluginConfiguration("org.openrewrite.maven", "rewrite-maven-plugin", null)),
pomXml(
"""
<project>
Expand Down Expand Up @@ -93,9 +77,7 @@ void addConfiguration() {
spec -> spec.recipe(new ChangePluginConfiguration(
"org.openrewrite.maven",
"rewrite-maven-plugin",
"<activeRecipes>\n<recipe>org.openrewrite.java.cleanup.UnnecessaryThrows</recipe>\n</activeRecipes>",
null,
null)),
"<activeRecipes>\n<recipe>org.openrewrite.java.cleanup.UnnecessaryThrows</recipe>\n</activeRecipes>")),
pomXml(
"""
<project>
Expand Down Expand Up @@ -146,9 +128,7 @@ void replaceConfiguration() {
spec -> spec.recipe(new ChangePluginConfiguration(
"org.openrewrite.maven",
"rewrite-maven-plugin",
"<activeRecipes>\n<recipe>org.openrewrite.java.cleanup.UnnecessaryThrows</recipe>\n</activeRecipes>",
null,
null)),
"<activeRecipes>\n<recipe>org.openrewrite.java.cleanup.UnnecessaryThrows</recipe>\n</activeRecipes>")),
pomXml(
"""
<project>
Expand Down Expand Up @@ -194,130 +174,12 @@ void replaceConfiguration() {
);
}

@Test
void transformConfigurationFromInlineTransformation() {
rewriteRun(
spec -> spec.recipe(new ChangePluginConfiguration(
"org.openrewrite.maven",
"rewrite-maven-plugin",
null,
xslt,
null)),
pomXml(
"""
<project>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.1.5</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.cleanup.UnnecessaryThrows</recipe>
</activeRecipes>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
"""
<project>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.1.5</version>
<configuration>
<activeRecipes>
<activeRecipe>org.openrewrite.java.cleanup.UnnecessaryThrows</activeRecipe>
</activeRecipes>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}

@Test
void transformConfigurationFromClasspathResource() {
rewriteRun(
spec -> spec.recipe(new ChangePluginConfiguration(
"org.openrewrite.maven",
"rewrite-maven-plugin",
null,
null,
"/changePlugin.xslt")),
pomXml(
"""
<project>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.1.5</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.cleanup.UnnecessaryThrows</recipe>
</activeRecipes>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
"""
<project>
<groupId>org.example</groupId>
<artifactId>foo</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>4.1.5</version>
<configuration>
<activeRecipes>
<activeRecipe>org.openrewrite.java.cleanup.UnnecessaryThrows</activeRecipe>
</activeRecipes>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
);
}

@Test
void transformConfigurationNoOpWhenConfigurationMissing() {
rewriteRun(
spec -> spec.recipe(new ChangePluginConfiguration(
"org.openrewrite.maven",
"rewrite-maven-plugin",
null,
null,
null)),
pomXml(
"""
Expand Down
15 changes: 0 additions & 15 deletions rewrite-maven/src/test/resources/changePlugin.xslt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

@Value
@EqualsAndHashCode(callSuper = false)
@Incubating(since = "8.30.0")
public class XsltTransformation extends Recipe {

@Nullable
Expand Down Expand Up @@ -62,7 +63,8 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "Apply the specified XSLT transformation on matching files.";
return "Apply the specified XSLT transformation on matching files. " +
"Note that there are no format matching guarantees when running this recipe.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.openrewrite.xml;

import lombok.RequiredArgsConstructor;
import org.intellij.lang.annotations.Language;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Incubating;
import org.openrewrite.marker.AlreadyReplaced;
import org.openrewrite.marker.Marker;
import org.openrewrite.xml.tree.Xml;
Expand All @@ -35,6 +37,7 @@
import static org.openrewrite.Tree.randomId;

@RequiredArgsConstructor
@Incubating(since = "8.30.0")
public class XsltTransformationVisitor extends XmlVisitor<ExecutionContext> {

private final String xslt;
Expand Down Expand Up @@ -71,12 +74,20 @@ private static Xml.Document transform(Xml.Document document, String xslt) {
}
}

public static Xml.Tag transformTag(String sourceConfiguration, String xslt) {
/**
* Transform XML content using XSLT transformation. There are no formatting guarantees with this transformation.
*
* @param xmlContent XML content to transform
* @param xslt XSLT transformation
* @return Transformed XML content
*/
@SuppressWarnings("unused")
public static Xml.Tag transformTag(@Language("xml") String xmlContent, @Language("xslt") String xslt) {
try {
Source xsltSource = new StreamSource(new ByteArrayInputStream(xslt.getBytes()));
Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource);

Source text = new StreamSource(new ByteArrayInputStream(sourceConfiguration.getBytes()));
Source text = new StreamSource(new ByteArrayInputStream(xmlContent.getBytes()));
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
transformer.transform(text, new StreamResult(baos));
return Xml.Tag.build(baos.toString());
Expand Down

0 comments on commit 0fb7071

Please sign in to comment.