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.ri.EvalContext;
    19  import org.apache.commons.jxpath.ri.QName;
    20  import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
    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 walks the "namespace::" axis.
    27   *
    28   * @author Dmitri Plotnikov
    29   * @version $Revision: 1.10 $ $Date: 2004/03/25 05:42:01 $
    30   */
    31  public class NamespaceContext extends EvalContext {
    32      private NodeTest nodeTest;
    33      private boolean setStarted = false;
    34      private NodeIterator iterator;
    35      private NodePointer currentNodePointer;
    36
    37      /***
    38       * @param parentContext represents the previous step on the path
    39       * @param nodeTest is the name of the namespace we are looking for
    40       */
    41      public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) {
    42          super(parentContext);
    43          this.nodeTest = nodeTest;
    44      }
    45
    46      public NodePointer getCurrentNodePointer() {
    47          return currentNodePointer;
    48      }
    49
    50      public void reset() {
    51          setStarted = false;
    52          iterator = null;
    53          super.reset();
    54      }
    55
    56      public boolean setPosition(int position) {
    57          if (position < getCurrentPosition()) {
    58              reset();
    59          }
    60
    61          while (getCurrentPosition() < position) {
    62              if (!nextNode()) {
    63                  return false;
    64              }
    65          }
    66          return true;
    67      }
    68
    69      public boolean nextNode() {
    70          super.setPosition(getCurrentPosition() + 1);
    71          if (!setStarted) {
    72              setStarted = true;
    73              if (!(nodeTest instanceof NodeNameTest)) {
    74                  return false;
    75              }
    76
    77              NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
    78              QName testName = nodeNameTest.getNodeName();
    79              if (testName.getPrefix() != null) {
    80                  return false;
    81              }
    82              if (nodeNameTest.isWildcard()) {
    83                  iterator =
    84                      parentContext.getCurrentNodePointer().namespaceIterator();
    85              }
    86              else {
    87                  currentNodePointer =
    88                      parentContext.getCurrentNodePointer().namespacePointer(
    89                              testName.getName());
    90                  return currentNodePointer != null;
    91              }
    92          }
    93
    94          if (iterator == null) {
    95              return false;
    96          }
    97          if (!iterator.setPosition(iterator.getPosition() + 1)) {
    98              return false;
    99          }
    100         currentNodePointer = iterator.getNodePointer();
    101         return true;
    102     }
    103 }