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..f99abd38118 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 (0 < dot && 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..f8e5a000bb9 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; @@ -229,7 +236,7 @@ void parseDocTypeWithoutExternalId() { """ - + WARN @@ -372,4 +379,28 @@ void preserveWhitespaceOnEntities() { ) ); } + + @DisabledOnOs(OS.WINDOWS) + @ParameterizedTest + @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", + "foo.xml.", + "file.cpp", + "/foo/bar/baz.xml.txt" + }) + void acceptWithInvalidPaths(String path) { + assertThat(new XmlParser().accept(Paths.get(path))).isFalse(); + } }