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