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.Pointer;
19: import org.apache.commons.jxpath.ri.EvalContext;
20: import org.apache.commons.jxpath.ri.model.NodePointer;
21:
22: /**
23: * A single-set EvalContext that provides access to the current node of
24: * the parent context and nothing else. It does not pass the iteration
25: * on to the parent context.
26: *
27: * @author Dmitri Plotnikov
28: * @version $Revision: 1.15 $ $Date: 2004/07/30 13:51:01 $
29: */
30: public class InitialContext extends EvalContext {
31: private boolean startedSet = false;
32: private boolean started = false;
33: private boolean collection;
34: private NodePointer nodePointer;
35:
36: public InitialContext(EvalContext parentContext) {
37: super (parentContext);
38: nodePointer = (NodePointer) parentContext
39: .getCurrentNodePointer().clone();
40: if (nodePointer != null) {
41: collection = (nodePointer.getIndex() == NodePointer.WHOLE_COLLECTION);
42: }
43: }
44:
45: public Pointer getSingleNodePointer() {
46: return nodePointer;
47: }
48:
49: public NodePointer getCurrentNodePointer() {
50: return nodePointer;
51: }
52:
53: public Object getValue() {
54: return nodePointer.getValue();
55: }
56:
57: public boolean nextNode() {
58: return setPosition(position + 1);
59: }
60:
61: public boolean setPosition(int position) {
62: this .position = position;
63: if (collection) {
64: if (position >= 1 && position <= nodePointer.getLength()) {
65: nodePointer.setIndex(position - 1);
66: return true;
67: }
68: return false;
69: } else {
70: return position == 1;
71: }
72: }
73:
74: public boolean nextSet() {
75: if (started) {
76: return false;
77: }
78: started = true;
79: return true;
80: }
81: }
|