View Javadoc

    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.Pointer;
    19  import org.apache.commons.jxpath.ri.EvalContext;
    20  import org.apache.commons.jxpath.ri.model.NodePointer;
    21
    22  /***
    23   * A single-set EvalContext that provides access to the current node of
    24   * the parent context and nothing else.  It does not pass the iteration
    25   * on to the parent context.
    26   *
    27   * @author Dmitri Plotnikov
    28   * @version $Revision: 1.15 $ $Date: 2004/07/30 13:51:01 $
    29   */
    30  public class InitialContext extends EvalContext {
    31      private boolean startedSet = false;
    32      private boolean started = false;
    33      private boolean collection;
    34      private NodePointer nodePointer;
    35
    36      public InitialContext(EvalContext parentContext) {
    37          super(parentContext);
    38          nodePointer =
    39              (NodePointer) parentContext.getCurrentNodePointer().clone();
    40          if (nodePointer != null) {
    41              collection =
    42                  (nodePointer.getIndex() == NodePointer.WHOLE_COLLECTION);
    43          }
    44      }
    45
    46      public Pointer getSingleNodePointer() {
    47          return nodePointer;
    48      }
    49
    50      public NodePointer getCurrentNodePointer() {
    51          return nodePointer;
    52      }
    53
    54      public Object getValue() {
    55          return nodePointer.getValue();
    56      }
    57
    58      public boolean nextNode() {
    59          return setPosition(position + 1);
    60      }
    61
    62      public boolean setPosition(int position) {
    63          this.position = position;
    64          if (collection) {
    65              if (position >= 1 && position <= nodePointer.getLength()) {
    66                  nodePointer.setIndex(position - 1);
    67                  return true;
    68              }
    69              return false;
    70          }
    71          else {
    72              return position == 1;
    73          }
    74      }
    75
    76      public boolean nextSet() {
    77          if (started) {
    78              return false;
    79          }
    80          started = true;
    81          return true;
    82      }
    83  }