Skip to content

Commit

Permalink
Handle namespaced attribute element with conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
evie-lau committed Jun 28, 2024
1 parent 4088233 commit 3ac7c10
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class XPathMatcher {

// Regular expression to support conditional tags like `plugin[artifactId='maven-compiler-plugin']` or foo[@bar='baz']
private static final Pattern PATTERN = Pattern.compile("(@)?([-\\w]+|\\*)\\[((local-name|namespace-uri)\\(\\)|(@)?([-\\w]+|\\*))='(.*)']");
private static final Pattern PATTERN = Pattern.compile("(@)?([-:\\w]+|\\*)\\[((local-name|namespace-uri)\\(\\)|(@)?([-\\w]+|\\*))='(.*)']");

private final String expression;
private final boolean startsWithSlash;
Expand Down Expand Up @@ -197,7 +197,8 @@ public boolean matches(Cursor cursor) {
for (int i = 0; i < parts.length; i++) {
String part = parts[i];

Xml.Tag tag = i < path.size() ? path.get(i) : null;
int isAttr = part.startsWith("@") ? 1 : 0;
Xml.Tag tag = i - isAttr < path.size() ? path.get(i - isAttr) : null;
String partName;
boolean matchedCondition = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class XPathMatcherTest {
http://www.example.com/namespace3 http://www.example.com/namespace3.xsd">
<element1 ns3:attribute1="content3">content1</element1>
<ns2:element2>content2</ns2:element2>
<parent>
<element3 ns3:attr='test'>content3</element3>
</parent>
</root>
"""
).toList().get(0);
Expand Down Expand Up @@ -159,6 +162,7 @@ void matchPom() {
pomXml1)).isTrue();
assertThat(match("/project/build//plugins/plugin/configuration/source",
pomXml2)).isTrue();
// assertThat(match("/project/build//plugin/configuration/source", pomXml2)).isTrue(); // TODO: seems parser only handles // up to 1 level
// assertThat(match("/project//configuration/source", pomXml2)).isTrue(); // TODO: was already failing previously
}

Expand Down Expand Up @@ -314,6 +318,11 @@ void matchAttributeElement() {
assertThat(match("//ns2:element2/@*", namespacedXml)).isFalse();
assertThat(match("/root/ns2:element2/@*", namespacedXml)).isFalse();

assertThat(match("/root/parent/element3/@attr", namespacedXml)).isFalse();
assertThat(match("/root/parent/element3/@ns3:attr", namespacedXml)).isTrue();
assertThat(match("/root/parent/element3/@ns3:attr[namespace-uri()='http://www.example.com/namespace3']", namespacedXml)).isTrue();
assertThat(match("//element3/@ns3:attr[namespace-uri()='http://www.example.com/namespace3']", namespacedXml)).isTrue();

// TODO: fix mid-path // match with attribute element
// assertThat(match("/root//element1/@*", namespacedXml)).isTrue();
// assertThat(match("/root//element1/@*[namespace-uri()='http://www.example.com/namespace3']", namespacedXml)).isTrue();
Expand Down

0 comments on commit 3ac7c10

Please sign in to comment.