1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri.axes;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22
23 import org.apache.commons.jxpath.BasicNodeSet;
24 import org.apache.commons.jxpath.ri.EvalContext;
25 import org.apache.commons.jxpath.ri.model.NodePointer;
26
27
28
29
30 public class UnionContext extends NodeSetContext {
31
32 private final EvalContext[] contexts;
33 private boolean prepared;
34
35
36
37
38
39
40
41 public UnionContext(final EvalContext parentContext, final EvalContext[] contexts) {
42 super(parentContext, new BasicNodeSet());
43 this.contexts = contexts;
44 }
45
46 @Override
47 public int getDocumentOrder() {
48 return contexts.length > 1 ? 1 : super.getDocumentOrder();
49 }
50
51 @Override
52 public boolean setPosition(final int position) {
53 if (!prepared) {
54 prepared = true;
55 final BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
56 final ArrayList<NodePointer> pointers = new ArrayList<>();
57 for (final EvalContext ctx : contexts) {
58 while (ctx.nextSet()) {
59 while (ctx.nextNode()) {
60 final NodePointer ptr = ctx.getCurrentNodePointer();
61 if (!pointers.contains(ptr)) {
62 pointers.add(ptr);
63 }
64 }
65 }
66 }
67 sortPointers(pointers);
68 for (final Iterator<NodePointer> it = pointers.iterator(); it.hasNext();) {
69 nodeSet.add(it.next());
70 }
71 }
72 return super.setPosition(position);
73 }
74 }