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