From 0fb707186f69d4cd4518388f7180126385f7a14c Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 15 Jul 2024 04:34:49 +0200 Subject: [PATCH] Drop XSLT options from ChangePluginConfiguration (#4328) * 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 --- .../maven/ChangePluginConfiguration.java | 46 +----- .../maven/ChangePluginConfigurationTest.java | 144 +----------------- .../src/test/resources/changePlugin.xslt | 15 -- .../openrewrite/xml/XsltTransformation.java | 4 +- .../xml/XsltTransformationVisitor.java | 15 +- 5 files changed, 21 insertions(+), 203 deletions(-) delete mode 100644 rewrite-maven/src/test/resources/changePlugin.xslt diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangePluginConfiguration.java b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangePluginConfiguration.java index 64aac2bb1ff..7e38b951622 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/ChangePluginConfiguration.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/ChangePluginConfiguration.java @@ -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 element.", - example = "...", - required = false) - String xslt; - - @Nullable - @Option(displayName = "XSLT Configuration transformation classpath resource", - description = "The transformation to be applied on the element provided as a classpath resource.", - example = "/changePlugin.xslt", - required = false) - String xsltResource; - @Override public String getDisplayName() { return "Change Maven plugin configuration"; @@ -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 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 getVisitor() { return new MavenVisitor() { @@ -111,22 +90,13 @@ 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("\n" + configuration + "\n"), getCursor().getParentOrThrow()); - } else { - // Implied that xslt or xsltResource is not null - Optional 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()); - } } } } @@ -134,16 +104,4 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { } }; } - - 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); - } - } } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangePluginConfigurationTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangePluginConfigurationTest.java index a04c8a595a7..b8ed52c4d92 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/ChangePluginConfigurationTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/ChangePluginConfigurationTest.java @@ -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( """ @@ -93,9 +77,7 @@ void addConfiguration() { spec -> spec.recipe(new ChangePluginConfiguration( "org.openrewrite.maven", "rewrite-maven-plugin", - "\norg.openrewrite.java.cleanup.UnnecessaryThrows\n", - null, - null)), + "\norg.openrewrite.java.cleanup.UnnecessaryThrows\n")), pomXml( """ @@ -146,9 +128,7 @@ void replaceConfiguration() { spec -> spec.recipe(new ChangePluginConfiguration( "org.openrewrite.maven", "rewrite-maven-plugin", - "\norg.openrewrite.java.cleanup.UnnecessaryThrows\n", - null, - null)), + "\norg.openrewrite.java.cleanup.UnnecessaryThrows\n")), pomXml( """ @@ -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( - """ - - org.example - foo - 1.0 - - - - - org.openrewrite.maven - rewrite-maven-plugin - 4.1.5 - - - org.openrewrite.java.cleanup.UnnecessaryThrows - - - - - - - """, - """ - - org.example - foo - 1.0 - - - - - org.openrewrite.maven - rewrite-maven-plugin - 4.1.5 - - - org.openrewrite.java.cleanup.UnnecessaryThrows - - - - - - - """ - ) - ); - } - - @Test - void transformConfigurationFromClasspathResource() { - rewriteRun( - spec -> spec.recipe(new ChangePluginConfiguration( - "org.openrewrite.maven", - "rewrite-maven-plugin", - null, - null, - "/changePlugin.xslt")), - pomXml( - """ - - org.example - foo - 1.0 - - - - - org.openrewrite.maven - rewrite-maven-plugin - 4.1.5 - - - org.openrewrite.java.cleanup.UnnecessaryThrows - - - - - - - """, - """ - - org.example - foo - 1.0 - - - - - org.openrewrite.maven - rewrite-maven-plugin - 4.1.5 - - - org.openrewrite.java.cleanup.UnnecessaryThrows - - - - - - - """ - ) - ); - } - @Test void transformConfigurationNoOpWhenConfigurationMissing() { rewriteRun( spec -> spec.recipe(new ChangePluginConfiguration( "org.openrewrite.maven", "rewrite-maven-plugin", - null, - null, null)), pomXml( """ diff --git a/rewrite-maven/src/test/resources/changePlugin.xslt b/rewrite-maven/src/test/resources/changePlugin.xslt deleted file mode 100644 index ee4b5243104..00000000000 --- a/rewrite-maven/src/test/resources/changePlugin.xslt +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformation.java b/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformation.java index 917ba8bf247..8f987cd663d 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformation.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformation.java @@ -31,6 +31,7 @@ @Value @EqualsAndHashCode(callSuper = false) +@Incubating(since = "8.30.0") public class XsltTransformation extends Recipe { @Nullable @@ -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 diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformationVisitor.java b/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformationVisitor.java index d441761620c..796ce17841e 100644 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformationVisitor.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/XsltTransformationVisitor.java @@ -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; @@ -35,6 +37,7 @@ import static org.openrewrite.Tree.randomId; @RequiredArgsConstructor +@Incubating(since = "8.30.0") public class XsltTransformationVisitor extends XmlVisitor { private final String xslt; @@ -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());