Prevent XPath from returning the same node multiple times (fixes #370)

This commit is contained in:
Sam Harwell 2014-01-15 14:32:34 -06:00
parent 83ad25ea1d
commit 657d496d71
1 changed files with 5 additions and 3 deletions

View File

@ -12,6 +12,8 @@ import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
/** Represent a subset of XPath XML path syntax for use in identifying nodes in /** Represent a subset of XPath XML path syntax for use in identifying nodes in
@ -176,14 +178,14 @@ loop:
*/ */
public Collection<ParseTree> evaluate(final ParseTree t) { public Collection<ParseTree> evaluate(final ParseTree t) {
ParserRuleContext dummyRoot = new ParserRuleContext(); ParserRuleContext dummyRoot = new ParserRuleContext();
dummyRoot.children = new ArrayList<ParseTree>() {{add(t);}}; // don't set t's parent. dummyRoot.children = Collections.singletonList(t); // don't set t's parent.
Collection<ParseTree> work = new ArrayList<ParseTree>(); Collection<ParseTree> work = Collections.<ParseTree>singleton(dummyRoot);
work.add(dummyRoot); work.add(dummyRoot);
int i = 0; int i = 0;
while ( i < elements.length ) { while ( i < elements.length ) {
Collection<ParseTree> next = new ArrayList<ParseTree>(); Collection<ParseTree> next = new LinkedHashSet<ParseTree>();
for (ParseTree node : work) { for (ParseTree node : work) {
if ( node.getChildCount()>0 ) { if ( node.getChildCount()>0 ) {
// only try to match next element if it has children // only try to match next element if it has children