Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Oct 22, 2024
1 parent 4e3b4fa commit 6e34443
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.xml.marker;
package org.openrewrite.marker;

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

import java.util.UUID;

@Value
@With
public class JavaType implements Marker {
public class TypeReference implements Marker {
UUID id;
String language;
}
28 changes: 14 additions & 14 deletions rewrite-xml/src/main/java/org/openrewrite/xml/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
import org.openrewrite.Parser;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.marker.TypeReference;
import org.openrewrite.tree.ParseError;
import org.openrewrite.tree.ParsingEventListener;
import org.openrewrite.tree.ParsingExecutionContextView;
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.*;
import java.util.regex.Pattern;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

public class XmlParser implements Parser {
Expand Down Expand Up @@ -164,30 +166,28 @@ public String getDslName() {
}

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");
XPathMatcher classXPath = new XPathMatcher("//@class[contains(., '.')]");
XPathMatcher typeXPath = new XPathMatcher("//@type[contains(., '.')]");
XPathMatcher tags = new XPathMatcher("//value[contains(text(), '.')]");

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()))));
}
if (classXPath.matches(getCursor()) || typeXPath.matches(getCursor())) {
return attrib.withMarkers(attrib.getMarkers().withMarkers(Collections.singletonList(new TypeReference(attrib.getId(), "Java"))));

}
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 (tags.matches(getCursor())) {
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.withMarkers(tg.getMarkers().withMarkers(Collections.singletonList(new TypeReference(tg.getId(), "Java"))));
}
}
return tg;
Expand Down
10 changes: 5 additions & 5 deletions rewrite-xml/src/test/java/org/openrewrite/xml/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.marker.TypeReference;
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;
Expand Down Expand Up @@ -422,13 +422,13 @@ void springXmlMarkers() {
""",
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));
assertTrue(testBean.getAttributes().get(1).getMarkers().getMarkers().stream().anyMatch(TypeReference.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));
assertTrue(unnamedBeanInSiblingProperty.getAttributes().get(0).getMarkers().getMarkers().stream().anyMatch(TypeReference.class::isInstance));
Xml.Tag ageProperty = ((Xml.Tag) unnamedBeanInSiblingProperty.getContent().get(0));
assertTrue(ageProperty.getAttributes().get(2).getMarkers().getMarkers().stream().anyMatch(JavaType.class::isInstance));
assertTrue(ageProperty.getAttributes().get(2).getMarkers().getMarkers().stream().anyMatch(TypeReference.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));
assertTrue(someNameValue.getMarkers().getMarkers().stream().anyMatch(TypeReference.class::isInstance));
})
)
);
Expand Down

0 comments on commit 6e34443

Please sign in to comment.