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.Pointer;
21  import org.apache.commons.jxpath.ri.EvalContext;
22  import org.apache.commons.jxpath.ri.compiler.NodeTest;
23  import org.apache.commons.jxpath.ri.model.NodeIterator;
24  import org.apache.commons.jxpath.ri.model.NodePointer;
25  
26  /**
27   * EvalContext that can walk the "child::", "following-sibling::" and "preceding-sibling::" axes.
28   */
29  public class ChildContext extends EvalContext {
30  
31      private final NodeTest nodeTest;
32      private final boolean startFromParentLocation;
33      private final boolean reverse;
34      private NodeIterator iterator;
35  
36      /**
37       * Constructs a new ChildContext.
38       *
39       * @param parentContext           parent EvalContext
40       * @param nodeTest                NodeTest
41       * @param startFromParentLocation whether to start from parent location
42       * @param reverse                 whether to iterate in reverse
43       */
44      public ChildContext(final EvalContext parentContext, final NodeTest nodeTest, final boolean startFromParentLocation, final boolean reverse) {
45          super(parentContext);
46          this.nodeTest = nodeTest;
47          this.startFromParentLocation = startFromParentLocation;
48          this.reverse = reverse;
49      }
50  
51      @Override
52      public NodePointer getCurrentNodePointer() {
53          if (position == 0 && !setPosition(1)) {
54              return null;
55          }
56          return iterator == null ? null : iterator.getNodePointer();
57      }
58  
59      /**
60       * This method is called on the last context on the path when only one value is needed. Note that this will return the whole property, even if it is a
61       * collection. It will not extract the first element of the collection. For example, "books" will return the collection of books rather than the first book
62       * from that collection.
63       *
64       * @return Pointer
65       */
66      @Override
67      public Pointer getSingleNodePointer() {
68          if (position == 0) {
69              while (nextSet()) {
70                  prepare();
71                  if (iterator == null) {
72                      return null;
73                  }
74                  // See if there is a property there, singular or collection
75                  final NodePointer pointer = iterator.getNodePointer();
76                  if (pointer != null) {
77                      return pointer;
78                  }
79              }
80              return null;
81          }
82          return getCurrentNodePointer();
83      }
84  
85      @Override
86      public boolean nextNode() {
87          return setPosition(getCurrentPosition() + 1);
88      }
89  
90      /**
91       * Allocates a PropertyIterator.
92       */
93      private void prepare() {
94          final NodePointer parent = parentContext.getCurrentNodePointer();
95          if (parent == null) {
96              return;
97          }
98          final NodePointer useParent = startFromParentLocation ? parent.getParent() : parent;
99          iterator = useParent == null ? null : useParent.childIterator(nodeTest, reverse, startFromParentLocation ? parent : null);
100     }
101 
102     @Override
103     public void reset() {
104         super.reset();
105         iterator = null;
106     }
107 
108     @Override
109     public boolean setPosition(final int position) {
110         final int oldPosition = getCurrentPosition();
111         super.setPosition(position);
112         if (oldPosition == 0) {
113             prepare();
114         }
115         return iterator != null && iterator.setPosition(position);
116     }
117 }