1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.jxpath.ri.axes;
19
20 import org.apache.commons.jxpath.Pointer;
21 import org.apache.commons.jxpath.ri.EvalContext;
22 import org.apache.commons.jxpath.ri.model.NodePointer;
23
24 /**
25 * A single-set EvalContext that provides access to the current node of the parent context and nothing else. It does not pass the iteration on to the parent
26 * context.
27 */
28 public class InitialContext extends EvalContext {
29
30 private boolean started;
31 private boolean collection;
32 private final NodePointer nodePointer;
33
34 /**
35 * Constructs a new InitialContext.
36 *
37 * @param parentContext parent context
38 */
39 public InitialContext(final EvalContext parentContext) {
40 super(parentContext);
41 nodePointer = (NodePointer) parentContext.getCurrentNodePointer().clone();
42 if (nodePointer != null) {
43 collection = nodePointer.getIndex() == NodePointer.WHOLE_COLLECTION;
44 }
45 }
46
47 @Override
48 public NodePointer getCurrentNodePointer() {
49 return nodePointer;
50 }
51
52 @Override
53 public Pointer getSingleNodePointer() {
54 return nodePointer;
55 }
56
57 @Override
58 public Object getValue() {
59 return nodePointer.getValue();
60 }
61
62 @Override
63 public boolean nextNode() {
64 return setPosition(position + 1);
65 }
66
67 @Override
68 public boolean nextSet() {
69 if (started) {
70 return false;
71 }
72 started = true;
73 return true;
74 }
75
76 @Override
77 public boolean setPosition(final int position) {
78 this.position = position;
79 if (collection) {
80 if (position >= 1 && position <= nodePointer.getLength()) {
81 nodePointer.setIndex(position - 1);
82 return true;
83 }
84 return false;
85 }
86 return position == 1;
87 }
88 }