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 "parent::" axis.
24: *
25: * @author Dmitri Plotnikov
26: * @version $Revision: 1.15 $ $Date: 2004/03/25 03:49:50 $
27: */
28: public class ParentContext extends EvalContext {
29: private NodeTest nodeTest;
30: private boolean setStarted = false;
31: private NodePointer currentNodePointer;
32:
33: public ParentContext(EvalContext parentContext, NodeTest nodeTest) {
34: super (parentContext);
35: this .nodeTest = nodeTest;
36: }
37:
38: public NodePointer getCurrentNodePointer() {
39: return currentNodePointer;
40: }
41:
42: public int getCurrentPosition() {
43: return 1;
44: }
45:
46: public int getDocumentOrder() {
47: return -1;
48: }
49:
50: public void reset() {
51: super .reset();
52: setStarted = false;
53: }
54:
55: public boolean setPosition(int position) {
56: super .setPosition(position);
57: return position == 1;
58: }
59:
60: public boolean nextNode() {
61: // Each set contains exactly one node: the parent
62: if (setStarted) {
63: return false;
64: }
65: setStarted = true;
66: NodePointer this Location = parentContext
67: .getCurrentNodePointer();
68: currentNodePointer = this Location.getImmediateParentPointer();
69: while (currentNodePointer != null
70: && currentNodePointer.isContainer()) {
71: currentNodePointer = currentNodePointer
72: .getImmediateParentPointer();
73: }
74: if (currentNodePointer != null
75: && currentNodePointer.testNode(nodeTest)) {
76: position++;
77: return true;
78: }
79: return false;
80: }
81: }
|