01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.jxpath.ri.axes;
17:
18: import java.util.HashSet;
19:
20: import org.apache.commons.jxpath.BasicNodeSet;
21: import org.apache.commons.jxpath.ri.EvalContext;
22: import org.apache.commons.jxpath.ri.model.NodePointer;
23:
24: /**
25: * EvalContext that represents a union between other contexts - result
26: * of a union operation like (a | b)
27: *
28: * @author Dmitri Plotnikov
29: * @version $Revision: 1.12 $ $Date: 2004/02/29 14:17:38 $
30: */
31: public class UnionContext extends NodeSetContext {
32: private boolean startedSet = false;
33: private EvalContext contexts[];
34: private boolean prepared = false;
35:
36: public UnionContext(EvalContext parentContext,
37: EvalContext contexts[]) {
38: super (parentContext, new BasicNodeSet());
39: this .contexts = contexts;
40: }
41:
42: public int getDocumentOrder() {
43: if (contexts.length > 1) {
44: return 1;
45: }
46: return super .getDocumentOrder();
47: }
48:
49: public boolean setPosition(int position) {
50: if (!prepared) {
51: prepared = true;
52: BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
53: HashSet set = new HashSet();
54: for (int i = 0; i < contexts.length; i++) {
55: EvalContext ctx = (EvalContext) contexts[i];
56: while (ctx.nextSet()) {
57: while (ctx.nextNode()) {
58: NodePointer ptr = ctx.getCurrentNodePointer();
59: if (!set.contains(ptr)) {
60: nodeSet.add(ptr);
61: set.add(ptr);
62: }
63: }
64: }
65: }
66: }
67: return super.setPosition(position);
68: }
69: }
|