Skip to content

Commit

Permalink
More XPath matching fixes
Browse files Browse the repository at this point in the history
- paths with multiple occurrences of //
- // preceding @Attribute
  • Loading branch information
DidierLoiseau committed Sep 28, 2024
1 parent 9f796c5 commit d86f21c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
27 changes: 17 additions & 10 deletions rewrite-xml/src/main/java/org/openrewrite/xml/XPathMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ public class XPathMatcher {
private final boolean startsWithSlash;
private final boolean startsWithDoubleSlash;
private final String[] parts;
private final long tagMatchingParts;

public XPathMatcher(String expression) {
this.expression = expression;
startsWithSlash = expression.startsWith("/");
startsWithDoubleSlash = expression.startsWith("//");
parts = splitOnXPathSeparator(expression.substring(startsWithDoubleSlash ? 2 : startsWithSlash ? 1 : 0));
tagMatchingParts = Arrays.stream(parts).filter(part -> !part.isEmpty() && !part.startsWith("@")).count();
}

private String[] splitOnXPathSeparator(String input) {
Expand Down Expand Up @@ -165,36 +167,32 @@ public boolean matches(Cursor cursor) {
int blankPartIndex = Arrays.asList(parts).indexOf("");
int doubleSlashIndex = expression.indexOf("//");

if (path.size() > blankPartIndex && path.size() >= parts.length - (parts[parts.length - 1].startsWith("@") ? 2 : 1)) {
String newExpression;
if (path.size() > blankPartIndex && path.size() >= tagMatchingParts) {
Xml.Tag blankPartTag = path.get(blankPartIndex);
String part = parts[blankPartIndex + 1];
Matcher matcher = ELEMENT_WITH_CONDITION_PATTERN.matcher(part);
if (matcher.matches() ?
matchesElementWithConditionFunction(matcher, blankPartTag, cursor) != null :
Objects.equals(blankPartTag.getName(), part)) {
newExpression = String.format(
"%s/%s",
expression.substring(0, doubleSlashIndex),
expression.substring(doubleSlashIndex + 2)
);
if (new XPathMatcher(newExpression).matches(cursor)) {
if (matchesWithoutDoubleSlashesAt(cursor, doubleSlashIndex)) {
return true;
}
// fall-through: maybe we can skip this element and match further down
}
newExpression = String.format(
String newExpression = String.format(
// the // here allows to skip several levels of nested elements
"%s/%s//%s",
expression.substring(0, doubleSlashIndex),
blankPartTag.getName(),
expression.substring(doubleSlashIndex + 2)
);
return new XPathMatcher(newExpression).matches(cursor);
} else if (path.size() == tagMatchingParts) {
return matchesWithoutDoubleSlashesAt(cursor, doubleSlashIndex);
}
}

if (parts.length > path.size() + 1) {
if (tagMatchingParts > path.size()) {
return false;
}

Expand Down Expand Up @@ -236,6 +234,15 @@ public boolean matches(Cursor cursor) {
}
}

private boolean matchesWithoutDoubleSlashesAt(Cursor cursor, int doubleSlashIndex) {
String newExpression = String.format(
"%s/%s",
expression.substring(0, doubleSlashIndex),
expression.substring(doubleSlashIndex + 2)
);
return new XPathMatcher(newExpression).matches(cursor);
}

/**
* Checks that the given {@code tag} matches the XPath part represented by {@code matcher}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ void matchAbsolute() {
assertThat(match("/dependencies/dependency", xmlDoc)).isTrue();
assertThat(match("/dependencies/*/artifactId", xmlDoc)).isTrue();
assertThat(match("/dependencies/*", xmlDoc)).isTrue();
assertThat(match("/dependencies//dependency", xmlDoc)).isTrue();
assertThat(match("/dependencies//dependency//groupId", xmlDoc)).isTrue();

// negative matches
assertThat(match("/dependencies/dne", xmlDoc)).isFalse();
assertThat(match("/dependencies//dne", xmlDoc)).isFalse();
assertThat(match("/dependencies//dependency//dne", xmlDoc)).isFalse();
}

@Test
Expand All @@ -127,6 +133,17 @@ void matchAbsoluteAttribute() {
assertThat(match("/dependencies/dependency/artifactId/@scope", xmlDoc)).isTrue();
assertThat(match("/dependencies/dependency/artifactId/@*", xmlDoc)).isTrue();
assertThat(match("/dependencies/dependency/groupId/@*", xmlDoc)).isFalse();
assertThat(match("/dependencies//dependency//@scope", xmlDoc)).isTrue();
assertThat(match("/dependencies//dependency//artifactId//@scope", xmlDoc)).isTrue();
assertThat(match("/dependencies//dependency//@*", xmlDoc)).isTrue();
assertThat(match("/dependencies//dependency//artifactId//@*", xmlDoc)).isTrue();

// negative matches
assertThat(match("/dependencies/dependency/artifactId/@dne", xmlDoc)).isFalse();
assertThat(match("/dependencies/dependency/artifactId/@dne", xmlDoc)).isFalse();
assertThat(match("/dependencies//dependency//@dne", xmlDoc)).isFalse();
assertThat(match("/dependencies//dependency//artifactId//@dne", xmlDoc)).isFalse();

}

@Test
Expand All @@ -136,8 +153,6 @@ void matchRelative() {
assertThat(match("//dependency", xmlDoc)).isTrue();
assertThat(match("dependency/*", xmlDoc)).isTrue();
assertThat(match("dne", xmlDoc)).isFalse();
assertThat(match("/dependencies//dependency", xmlDoc)).isTrue();
assertThat(match("/dependencies//dependency/groupId", xmlDoc)).isTrue();
}

@Test
Expand Down Expand Up @@ -211,6 +226,7 @@ void matchPom() {
// skip 2+ levels with //
assertThat(match("/project/build//plugin/configuration/source", pomXml2)).isTrue();
assertThat(match("/project//configuration/source", pomXml2)).isTrue();
assertThat(match("/project//plugin//source", pomXml2)).isTrue();
}

private final SourceFile attributeXml = new XmlParser().parse(
Expand Down

0 comments on commit d86f21c

Please sign in to comment.