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 org.apache.commons.jxpath.ri.EvalContext;
19: import org.apache.commons.jxpath.ri.compiler.NodeTest;
20: import org.apache.commons.jxpath.ri.model.NodePointer;
21:
22: /**
23: * EvalContext that walks the "ancestor::" and "ancestor-or-self::" axes.
24: *
25: * @author Dmitri Plotnikov
26: * @version $Revision: 1.15 $ $Date: 2004/03/25 03:49:50 $
27: */
28: public class AncestorContext extends EvalContext {
29: private NodeTest nodeTest;
30: private boolean setStarted = false;
31: private NodePointer currentNodePointer;
32: private boolean includeSelf;
33:
34: /**
35: * @param parentContext represents the previous step on the path
36: * @param includeSelf differentiates between "ancestor::" and "ancestor-
37: * or-self::" axes
38: * @param nameTest is the name of the element(s) we are looking for
39: */
40: public AncestorContext(EvalContext parentContext,
41: boolean includeSelf, NodeTest nodeTest) {
42: super (parentContext);
43: this .includeSelf = includeSelf;
44: this .nodeTest = nodeTest;
45: }
46:
47: public NodePointer getCurrentNodePointer() {
48: return currentNodePointer;
49: }
50:
51: public int getDocumentOrder() {
52: return -1;
53: }
54:
55: public void reset() {
56: super .reset();
57: setStarted = false;
58: }
59:
60: public boolean setPosition(int position) {
61: if (position < getCurrentPosition()) {
62: reset();
63: }
64:
65: while (getCurrentPosition() < position) {
66: if (!nextNode()) {
67: return false;
68: }
69: }
70: return true;
71: }
72:
73: public boolean nextNode() {
74: if (!setStarted) {
75: setStarted = true;
76: currentNodePointer = parentContext.getCurrentNodePointer();
77: if (includeSelf) {
78: if (currentNodePointer.testNode(nodeTest)) {
79: position++;
80: return true;
81: }
82: }
83: }
84:
85: while (true) {
86: currentNodePointer = currentNodePointer
87: .getImmediateParentPointer();
88:
89: if (currentNodePointer == null) {
90: return false;
91: }
92:
93: if (currentNodePointer.testNode(nodeTest)) {
94: position++;
95: return true;
96: }
97: }
98: }
99: }
|