Skip to content

Commit

Permalink
Merge pull request #26 from OP-TED/bugfix/TEDEFO-2356-contextualising…
Browse files Browse the repository at this point in the history
…-xpaths-with-predicates

Fixed an issue with the contextualisation of XPaths with multiple predicates
  • Loading branch information
rousso authored Dec 7, 2023
2 parents d4521b1 + 1fb0e61 commit 42bd78a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/main/java/eu/europa/ted/eforms/xpath/XPathProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ private static String getContextualizedXpath(Queue<XPathStep> contextQueue,
// At this point there are no more matching nodes in the two queues.

// We look at the first of the remaining steps in both queues and look if
// the context is more restrictive than the path. In this case we want to use a dot step
// with the predicate of the path.
// the context is the same as or less restrictive than the path. In this case
// we want to use a dot step with the predicate of the path.
if (!contextQueue.isEmpty() && !pathQueue.isEmpty()
&& pathQueue.peek().isSimilarTo(contextQueue.peek())) {
&& pathQueue.peek().isSameAsOrNarrowerThan(contextQueue.peek())) {
contextQueue.poll(); // consume the same step from the contextQueue
if (contextQueue.isEmpty()) {
// Since there are no more steps in the contextQueue, the relative xpath should
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/eu/europa/ted/eforms/xpath/XPathStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public boolean isTheSameAs(final XPathStep other) {
return pathPredicates.equals(contextPredicates);
}

public boolean isSimilarTo(final XPathStep other) {
public boolean isSameAsOrNarrowerThan(final XPathStep other) {

// First check the step texts are different.
if (!Objects.equals(other.stepText, this.stepText)) {
Expand All @@ -125,13 +125,15 @@ public boolean isSimilarTo(final XPathStep other) {

// If one of the two steps has more predicates that the other,
if (this.predicates.size() != other.predicates.size()) {
// then the steps are similar if either of them has no predicates
// or all the predicates of this step are also found in the specific step.
return this.predicates.isEmpty() || other.predicates.isEmpty()
|| other.predicates.containsAll(this.predicates);
// then this step is same as or narrower that the other, if either of them has
// no predicates or all the predicates of the other step are also found in this
// step. In this case this step has the same predicates as the other one, plus
// some more, which means it selects a subset of the nodes selected by the other
// step and therefore it is "narrower".
return other.predicates.isEmpty() || this.predicates.containsAll(other.predicates);
}

assert !this.isTheSameAs(other) : "You should not be calling isSimilarTo() without first checking isTheSameAs()";
assert !this.isTheSameAs(other) : "You should not be calling isSameAsOrNarrowerThan() without first checking isTheSameAs()";
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void testIdentical() {
assertEquals(".", contextualize("/a/b/c", "/a/b/c"));
}

@Test
void testIdentical_WithPredicates() {
assertEquals(".[d = e][f = g]", contextualize("/a/b/c[d = e]", "/a/b/c[d = e][f = g]"));
}

@Test
void testContextEmpty() {
assertEquals("/a/b/c", contextualize("", "/a/b/c"));
Expand Down Expand Up @@ -176,7 +181,7 @@ void testPredicateDifferent() {

@Test
void testPredicateMoreInXpath() {
assertEquals("../../b[e][f]/c/d", contextualize("/a/b[e]/c", "/a/b[e][f]/c/d"));
assertEquals("..[e][f]/c/d", contextualize("/a/b[e]/c", "/a/b[e][f]/c/d"));
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/eu/europa/ted/eforms/xpath/XPathStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void testComparison_DifferentElement() {

assertFalse(a.isTheSameAs(b));

assertFalse(a.isSimilarTo(b));
assertFalse(a.isSameAsOrNarrowerThan(b));
}

@Test
Expand All @@ -43,7 +43,7 @@ void testComparison_MorePredicates() {

assertTrue(a.isTheSameAs(b));

assertTrue(a.isSimilarTo(b));
assertTrue(b.isSameAsOrNarrowerThan(a));
}

@Test
Expand All @@ -53,7 +53,7 @@ void testComparison_LessPredicates() {

assertFalse(a.isTheSameAs(b));

assertFalse(a.isSimilarTo(b));
assertFalse(b.isSameAsOrNarrowerThan(a));
}

@Test
Expand All @@ -63,7 +63,7 @@ void testComparison_DifferentPredicate() {

assertFalse(a.isTheSameAs(b));

assertFalse(a.isSimilarTo(b));
assertFalse(a.isSameAsOrNarrowerThan(b));
}

@Test
Expand All @@ -73,7 +73,7 @@ void testComparison_NoPredicates() {

assertFalse(a.isTheSameAs(b));

assertTrue(a.isSimilarTo(b));
assertTrue(a.isSameAsOrNarrowerThan(b));
}

@Test
Expand All @@ -83,7 +83,7 @@ void testComparison_AddPredicates() {

assertTrue(a.isTheSameAs(b));

assertTrue(a.isSimilarTo(b));
assertTrue(b.isSameAsOrNarrowerThan(a));
}

private XPathStep buildStep(String elt, String... predicates) {
Expand Down

0 comments on commit 42bd78a

Please sign in to comment.