Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup for adding JavaType marker after parsing XML #4595

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import org.openrewrite.xml.internal.XmlParserVisitor;
import org.openrewrite.xml.internal.grammar.XMLLexer;
import org.openrewrite.xml.internal.grammar.XMLParser;
import org.openrewrite.xml.marker.JavaType;
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.*;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class XmlParser implements Parser {
Expand Down Expand Up @@ -94,6 +94,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
is.isCharsetBomMarked()
).visitDocument(parser.document());
parsingListener.parsed(input, document);
document = (Xml.Document) addJavaTypeOrPackageMarkers().visitDocument(document, ctx);
return requirePrintEqualsInput(document, input, relativeTo, ctx);
} catch (Throwable t) {
ctx.getOnError().accept(t);
Expand Down Expand Up @@ -161,4 +162,36 @@ public String getDslName() {
return "xml";
}
}

private XmlVisitor<ExecutionContext> addJavaTypeOrPackageMarkers() {
final Pattern PACKAGE_OR_TYPE_REFERENCE = Pattern.compile("^([a-zA-Z_][a-zA-Z0-9_]*)(\\.[a-zA-Z_][a-zA-Z0-9_]*)*(\\.[A-Z][a-zA-Z0-9_]*|\\$[A-Z][a-zA-Z0-9_]*)*(\\.\\*)?$");
final List<String> ATTRIBUTES_THAT_REFERENCE_PACKAGE_OR_TYPE = Arrays.asList("class", "type");
final List<String> TAGS_THAT_REFERENCE_PACKAGE_OR_TYPE = Arrays.asList("value");

return new XmlVisitor<ExecutionContext>() {
@Override
public Xml visitAttribute(Xml.Attribute attribute, ExecutionContext ctx) {
Xml.Attribute attrib = (Xml.Attribute) super.visitAttribute(attribute, ctx);
if (ATTRIBUTES_THAT_REFERENCE_PACKAGE_OR_TYPE.contains(attrib.getKey().getName())) {
if (PACKAGE_OR_TYPE_REFERENCE.matcher(attrib.getValueAsString()).matches()) {
return attrib.withMarkers(attrib.getMarkers().withMarkers(Collections.singletonList(new JavaType(attrib.getId()))));
}
}
return attrib;
}

@Override
public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
Xml.Tag tg = (Xml.Tag) super.visitTag(tag, ctx);
if (TAGS_THAT_REFERENCE_PACKAGE_OR_TYPE.contains(tg.getName())) {
if (tg.getValue().isPresent()) {
if (PACKAGE_OR_TYPE_REFERENCE.matcher(tg.getValue().get()).matches()) {
return tg.withMarkers(tg.getMarkers().withMarkers(Collections.singletonList(new JavaType(tg.getId()))));
}
}
}
return tg;
}
};
}
}
13 changes: 13 additions & 0 deletions rewrite-xml/src/main/java/org/openrewrite/xml/marker/JavaType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openrewrite.xml.marker;
Laurens-W marked this conversation as resolved.
Show resolved Hide resolved

import lombok.Value;
import lombok.With;
import org.openrewrite.marker.Marker;

import java.util.UUID;

@Value
@With
public class JavaType implements Marker {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably move this marker to rewrite-core and name it something like TypeReference and then possibly add a language property to it. This way FindType, ChangeType and other recipes can support it and can at the same time be independent of the language that the markers occur in.

UUID id;
}
37 changes: 37 additions & 0 deletions rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.xml.marker.JavaType;
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.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.test.RewriteTest.toRecipe;
import static org.openrewrite.xml.Assertions.xml;

Expand Down Expand Up @@ -397,6 +399,41 @@ void preserveWhitespaceOnEntities() {
);
}

@Test
void springXmlMarkers() {
rewriteRun(
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="testBean" class="org.springframework.beans.TestBean" scope="prototype">
<property name="age" value="10"/>
<property name="sibling">
<bean class="org.springframework.beans.TestBean">
<property name="age" value="11" class="java.lang.Integer"/>
<property name="someName">
<value>java.lang.String</value>
</property>
</bean>
</property>
</bean>
</beans>
""",
spec -> spec.afterRecipe(after -> {
Xml.Tag testBean = ((Xml.Tag) after.getRoot().getContent().get(0));
assertTrue(testBean.getAttributes().get(1).getMarkers().getMarkers().stream().anyMatch(JavaType.class::isInstance));
Xml.Tag unnamedBeanInSiblingProperty = ((Xml.Tag) ((Xml.Tag) testBean.getContent().get(1)).getContent().get(0));
assertTrue(unnamedBeanInSiblingProperty.getAttributes().get(0).getMarkers().getMarkers().stream().anyMatch(JavaType.class::isInstance));
Xml.Tag ageProperty = ((Xml.Tag) unnamedBeanInSiblingProperty.getContent().get(0));
assertTrue(ageProperty.getAttributes().get(2).getMarkers().getMarkers().stream().anyMatch(JavaType.class::isInstance));
Xml.Tag someNameValue = (Xml.Tag) ((Xml.Tag) unnamedBeanInSiblingProperty.getContent().get(1)).getContent().get(0);
assertTrue(someNameValue.getMarkers().getMarkers().stream().anyMatch(JavaType.class::isInstance));
})
)
);
}

@DisabledOnOs(OS.WINDOWS)
@ParameterizedTest
@ValueSource(strings = {
Expand Down
Loading