View Javadoc
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.ri.EvalContext;
21  import org.apache.commons.jxpath.ri.compiler.NodeTest;
22  import org.apache.commons.jxpath.ri.model.NodePointer;
23  
24  /**
25   * EvalContext that walks the "parent::" axis.
26   */
27  public class ParentContext extends EvalContext {
28  
29      private final NodeTest nodeTest;
30      private boolean setStarted;
31      private NodePointer currentNodePointer;
32  
33      /**
34       * Constructs a new ParentContext.
35       *
36       * @param parentContext parent context
37       * @param nodeTest      test
38       */
39      public ParentContext(final EvalContext parentContext, final NodeTest nodeTest) {
40          super(parentContext);
41          this.nodeTest = nodeTest;
42      }
43  
44      @Override
45      public NodePointer getCurrentNodePointer() {
46          return currentNodePointer;
47      }
48  
49      @Override
50      public int getCurrentPosition() {
51          return 1;
52      }
53  
54      @Override
55      public int getDocumentOrder() {
56          return -1;
57      }
58  
59      @Override
60      public boolean nextNode() {
61          // Each set contains exactly one node: the parent
62          if (setStarted) {
63              return false;
64          }
65          setStarted = true;
66          final NodePointer thisLocation = parentContext.getCurrentNodePointer();
67          currentNodePointer = thisLocation.getImmediateParentPointer();
68          while (currentNodePointer != null && currentNodePointer.isContainer()) {
69              currentNodePointer = currentNodePointer.getImmediateParentPointer();
70          }
71          if (currentNodePointer != null && currentNodePointer.testNode(nodeTest)) {
72              position++;
73              return true;
74          }
75          return false;
76      }
77  
78      @Override
79      public void reset() {
80          super.reset();
81          setStarted = false;
82      }
83  
84      @Override
85      public boolean setPosition(final int position) {
86          super.setPosition(position);
87          return position == 1;
88      }
89  }