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