From ebb96b69fd136b478854e864bdc0b5df766c815a Mon Sep 17 00:00:00 2001 From: Bryce Tompkins Date: Mon, 23 Sep 2024 18:36:36 -0400 Subject: [PATCH 1/2] Parse .vbproj and .fsproj as XML files --- .../java/org/openrewrite/xml/XmlParser.java | 69 +++++++++++-------- .../org/openrewrite/xml/XmlParserTest.java | 21 ++++++ 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java b/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java index cca7af905b5..d0d9528cc39 100755 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java @@ -32,9 +32,43 @@ import org.openrewrite.xml.tree.Xml; import java.nio.file.Path; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import java.util.stream.Stream; public class XmlParser implements Parser { + private static final Set ACCEPTED_FILE_EXTENSIONS = new HashSet<>(Arrays.asList( + "xml", + "wsdl", + "xhtml", + "xsd", + "xsl", + "xslt", + "xmi", + "tld", + "xjb", + "jsp", + // Datastage file formats that are all xml under the hood + "det", + "pjb", + "qjb", + "sjb", + "prt", + "srt", + "psc", + "ssc", + "tbd", + "tfm", + "dqs", + "stp", + "dcn", + "pst", + // .NET project files + "csproj", + "vbproj", + "fsproj")); + @Override public Stream parseInputs(Iterable sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) { ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener(); @@ -76,34 +110,13 @@ public Stream parse(@Language("xml") String... sources) { @Override public boolean accept(Path path) { String p = path.toString(); - return p.endsWith(".xml") || - p.endsWith(".wsdl") || - p.endsWith(".xhtml") || - p.endsWith(".xsd") || - p.endsWith(".xsl") || - p.endsWith(".xslt") || - p.endsWith(".xmi") || - p.endsWith(".tld") || - p.endsWith(".xjb") || - p.endsWith(".jsp") || - // Datastage file formats that are all xml under the hood - p.endsWith(".det") || - p.endsWith(".pjb") || - p.endsWith(".qjb") || - p.endsWith(".sjb") || - p.endsWith(".prt") || - p.endsWith(".srt") || - p.endsWith(".psc") || - p.endsWith(".ssc") || - p.endsWith(".tbd") || - p.endsWith(".tfm") || - p.endsWith(".dqs") || - p.endsWith(".stp") || - p.endsWith(".dcn") || - p.endsWith(".pst") || - // C# project files - p.endsWith(".csproj") || - path.endsWith("packages.config"); + int dot = p.lastIndexOf('.'); + if (dot > 0 && dot < (p.length() - 1)) { + if (ACCEPTED_FILE_EXTENSIONS.contains(p.substring(dot + 1))) { + return true; + } + } + return path.endsWith("packages.config"); } @Override diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java index da793ec5f16..98d6e533be1 100755 --- a/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java @@ -16,12 +16,19 @@ package org.openrewrite.xml; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.ExecutionContext; import org.openrewrite.Issue; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import org.openrewrite.xml.tree.Xml; +import java.nio.file.Paths; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.openrewrite.test.RewriteTest.toRecipe; import static org.openrewrite.xml.Assertions.xml; @@ -372,4 +379,18 @@ void preserveWhitespaceOnEntities() { ) ); } + + @DisabledOnOs(OS.WINDOWS) + @ParameterizedTest + @ValueSource(strings={"foo.xml", "proj.csproj", "/foo/bar/baz.jsp"}) + void acceptWithValidPaths(String path) { + assertThat(new XmlParser().accept(Paths.get(path))).isTrue(); + } + + @DisabledOnOs(OS.WINDOWS) + @ParameterizedTest + @ValueSource(strings={".xml", "file.cpp", "/foo/bar/baz.xml.txt"}) + void acceptWithInvalidPaths(String path) { + assertThat(new XmlParser().accept(Paths.get(path))).isFalse(); + } } From 952b62f79847075f3ea5aa8212e4dc99b0cbaabb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 24 Sep 2024 13:32:28 +0200 Subject: [PATCH 2/2] Test two more variants to guard against regression --- .../main/java/org/openrewrite/xml/XmlParser.java | 2 +- .../java/org/openrewrite/xml/XmlParserTest.java | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java b/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java index d0d9528cc39..f99abd38118 100755 --- a/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java +++ b/rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java @@ -111,7 +111,7 @@ public Stream parse(@Language("xml") String... sources) { public boolean accept(Path path) { String p = path.toString(); int dot = p.lastIndexOf('.'); - if (dot > 0 && dot < (p.length() - 1)) { + if (0 < dot && dot < (p.length() - 1)) { if (ACCEPTED_FILE_EXTENSIONS.contains(p.substring(dot + 1))) { return true; } diff --git a/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java b/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java index 98d6e533be1..f8e5a000bb9 100755 --- a/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java +++ b/rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java @@ -236,7 +236,7 @@ void parseDocTypeWithoutExternalId() { """ - + WARN @@ -382,14 +382,24 @@ void preserveWhitespaceOnEntities() { @DisabledOnOs(OS.WINDOWS) @ParameterizedTest - @ValueSource(strings={"foo.xml", "proj.csproj", "/foo/bar/baz.jsp"}) + @ValueSource(strings = { + "foo.xml", + "proj.csproj", + "/foo/bar/baz.jsp", + "packages.config" + }) void acceptWithValidPaths(String path) { assertThat(new XmlParser().accept(Paths.get(path))).isTrue(); } @DisabledOnOs(OS.WINDOWS) @ParameterizedTest - @ValueSource(strings={".xml", "file.cpp", "/foo/bar/baz.xml.txt"}) + @ValueSource(strings = { + ".xml", + "foo.xml.", + "file.cpp", + "/foo/bar/baz.xml.txt" + }) void acceptWithInvalidPaths(String path) { assertThat(new XmlParser().accept(Paths.get(path))).isFalse(); }