    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.compiler;
    17
    18  import org.apache.commons.jxpath.ri.Compiler;
    19
    20  /***
    21   * @author Dmitri Plotnikov
    22   * @version $Revision: 1.9 $ $Date: 2004/02/29 14:17:39 $
    23   */
    24  public class Step {
    25      private int axis;
    26      private NodeTest nodeTest;
    27      private Expression[] predicates;
    28
    29      protected Step(int axis, NodeTest nodeTest, Expression[] predicates) {
    30          this.axis = axis;
    31          this.nodeTest = nodeTest;
    32          this.predicates = predicates;
    33      }
    34
    35      public int getAxis() {
    36          return axis;
    37      }
    38
    39      public NodeTest getNodeTest() {
    40          return nodeTest;
    41      }
    42
    43      public Expression[] getPredicates() {
    44          return predicates;
    45      }
    46
    47      public boolean isContextDependent() {
    48          if (predicates != null) {
    49              for (int i = 0; i < predicates.length; i++) {
    50                  if (predicates[i].isContextDependent()) {
    51                      return true;
    52                  }
    53              }
    54          }
    55          return false;
    56      }
    57
    58      public String toString() {
    59          StringBuffer buffer = new StringBuffer();
    60          int axis = getAxis();
    61          if (axis == Compiler.AXIS_CHILD) {
    62              buffer.append(nodeTest);
    63          }
    64          else if (axis == Compiler.AXIS_ATTRIBUTE) {
    65              buffer.append('@');
    66              buffer.append(nodeTest);
    67          }
    68          else if (axis == Compiler.AXIS_SELF
    69                  && nodeTest instanceof NodeTypeTest
    70                  && ((NodeTypeTest) nodeTest).getNodeType()
    71                      == Compiler.NODE_TYPE_NODE) {
    72              buffer.append(".");
    73          }
    74          else if (axis == Compiler.AXIS_PARENT
    75                  && nodeTest instanceof NodeTypeTest
    76                  && ((NodeTypeTest) nodeTest).getNodeType()
    77                      == Compiler.NODE_TYPE_NODE) {
    78              buffer.append("..");
    79          }
    80          else if (axis == Compiler.AXIS_DESCENDANT_OR_SELF
    81                  && nodeTest instanceof NodeTypeTest
    82                  && ((NodeTypeTest) nodeTest).getNodeType()
    83                      == Compiler.NODE_TYPE_NODE
    84                  && (predicates == null || predicates.length == 0)) {
    85              buffer.append("");
    86          }
    87          else {
    88              buffer.append(axisToString(axis));
    89              buffer.append("::");
    90              buffer.append(nodeTest);
    91          }
    92          Expression[] predicates = getPredicates();
    93          if (predicates != null) {
    94              for (int i = 0; i < predicates.length; i++) {
    95                  buffer.append('[');
    96                  buffer.append(predicates[i]);
    97                  buffer.append(']');
    98              }
    99          }
    100         return buffer.toString();
    101     }
    102
    103     public static String axisToString(int axis) {
    104         switch (axis) {
    105             case Compiler.AXIS_SELF :
    106                 return "self";
    107             case Compiler.AXIS_CHILD :
    108                 return "child";
    109             case Compiler.AXIS_PARENT :
    110                 return "parent";
    111             case Compiler.AXIS_ANCESTOR :
    112                 return "ancestor";
    113             case Compiler.AXIS_ATTRIBUTE :
    114                 return "attribute";
    115             case Compiler.AXIS_NAMESPACE :
    116                 return "namespace";
    117             case Compiler.AXIS_PRECEDING :
    118                 return "preceding";
    119             case Compiler.AXIS_FOLLOWING :
    120                 return "following";
    121             case Compiler.AXIS_DESCENDANT :
    122                 return "descendant";
    123             case Compiler.AXIS_ANCESTOR_OR_SELF :
    124                 return "ancestor-or-self";
    125             case Compiler.AXIS_FOLLOWING_SIBLING :
    126                 return "following-sibling";
    127             case Compiler.AXIS_PRECEDING_SIBLING :
    128                 return "preceding-sibling";
    129             case Compiler.AXIS_DESCENDANT_OR_SELF :
    130                 return "descendant-or-self";
    131         }
    132         return "UNKNOWN";
    133     }
    134 }