    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jxpath.ri.axes;
    17
    18  import org.apache.commons.jxpath.NodeSet;
    19  import org.apache.commons.jxpath.ri.EvalContext;
    20  import org.apache.commons.jxpath.ri.model.NodePointer;
    21
    22  /***
    23   * A simple context that is based on a NodeSet.
    24   *
    25   * @author Dmitri Plotnikov
    26   * @version $Revision: 1.3 $ $Date: 2004/02/29 14:17:37 $
    27   */
    28  public class NodeSetContext extends EvalContext {
    29      private boolean startedSet = false;
    30      private NodeSet nodeSet;
    31
    32      public NodeSetContext(EvalContext parentContext, NodeSet nodeSet) {
    33          super(parentContext);
    34          this.nodeSet = nodeSet;
    35      }
    36
    37      public NodeSet getNodeSet() {
    38          return nodeSet;
    39      }
    40
    41      public NodePointer getCurrentNodePointer() {
    42          if (position == 0) {
    43              if (!setPosition(1)) {
    44                  return null;
    45              }
    46          }
    47          return (NodePointer) nodeSet.getPointers().get(position - 1);
    48      }
    49
    50      public boolean setPosition(int position) {
    51          super.setPosition(position);
    52          return position >= 1 && position <= nodeSet.getPointers().size();
    53      }
    54
    55      public boolean nextSet() {
    56          if (startedSet) {
    57              return false;
    58          }
    59          startedSet = true;
    60          return true;
    61      }
    62
    63      public boolean nextNode() {
    64          return setPosition(position + 1);
    65      }
    66  }