Skip to content

Commit

Permalink
Parse .NET .vbproj and .fsproj files as XML (#4518)
Browse files Browse the repository at this point in the history
* Parse .vbproj and .fsproj as XML files

* Test two more variants to guard against regression

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
bryceatmoderne and timtebeek authored Sep 24, 2024
1 parent 2f24d50 commit d2c825c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 29 deletions.
69 changes: 41 additions & 28 deletions rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
Expand Down Expand Up @@ -76,34 +110,13 @@ public Stream<SourceFile> 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
Expand Down
33 changes: 32 additions & 1 deletion rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -229,7 +236,7 @@ void parseDocTypeWithoutExternalId() {
"""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration >
<configuration scan="true">
<root>
<level>WARN</level>
Expand Down Expand Up @@ -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();
}
}

0 comments on commit d2c825c

Please sign in to comment.