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  
18  package org.apache.commons.jxpath.ri.compiler;
19  
20  import org.apache.commons.jxpath.ri.Compiler;
21  
22  /**
23   * A step.
24   */
25  public class Step {
26  
27      /**
28       * Decode an axis code to its name.
29       *
30       * @param axis int code
31       * @return String name.
32       * @see Compiler
33       * @see "http://www.w3.org/TR/xpath#axes"
34       */
35      public static String axisToString(final int axis) {
36          switch (axis) {
37          case Compiler.AXIS_SELF:
38              return "self";
39          case Compiler.AXIS_CHILD:
40              return "child";
41          case Compiler.AXIS_PARENT:
42              return "parent";
43          case Compiler.AXIS_ANCESTOR:
44              return "ancestor";
45          case Compiler.AXIS_ATTRIBUTE:
46              return "attribute";
47          case Compiler.AXIS_NAMESPACE:
48              return "namespace";
49          case Compiler.AXIS_PRECEDING:
50              return "preceding";
51          case Compiler.AXIS_FOLLOWING:
52              return "following";
53          case Compiler.AXIS_DESCENDANT:
54              return "descendant";
55          case Compiler.AXIS_ANCESTOR_OR_SELF:
56              return "ancestor-or-self";
57          case Compiler.AXIS_FOLLOWING_SIBLING:
58              return "following-sibling";
59          case Compiler.AXIS_PRECEDING_SIBLING:
60              return "preceding-sibling";
61          case Compiler.AXIS_DESCENDANT_OR_SELF:
62              return "descendant-or-self";
63          default:
64              return "UNKNOWN";
65          }
66      }
67  
68      private final int axis;
69      private final NodeTest nodeTest;
70      private final Expression[] predicates;
71  
72      /**
73       * Constructs a new Step.
74       *
75       * @param axis       axis code
76       * @param nodeTest   step test
77       * @param predicates predicate expressions
78       */
79      protected Step(final int axis, final NodeTest nodeTest, final Expression[] predicates) {
80          this.axis = axis;
81          this.nodeTest = nodeTest;
82          this.predicates = predicates;
83      }
84  
85      /**
86       * Gets the axis code.
87       *
88       * @return int
89       */
90      public int getAxis() {
91          return axis;
92      }
93  
94      /**
95       * Gets the step test.
96       *
97       * @return NodeTest
98       */
99      public NodeTest getNodeTest() {
100         return nodeTest;
101     }
102 
103     /**
104      * Gets the predicates.
105      *
106      * @return Expression[]
107      */
108     public Expression[] getPredicates() {
109         return predicates;
110     }
111 
112     /**
113      * Tests whether this step contains any predicate that is context dependent.
114      *
115      * @return boolean
116      */
117     public boolean isContextDependent() {
118         if (predicates != null) {
119             for (final Expression predicate : predicates) {
120                 if (predicate.isContextDependent()) {
121                     return true;
122                 }
123             }
124         }
125         return false;
126     }
127 
128     @Override
129     public String toString() {
130         final StringBuilder buffer = new StringBuilder();
131         final int axis = getAxis();
132         if (axis == Compiler.AXIS_CHILD) {
133             buffer.append(nodeTest);
134         } else if (axis == Compiler.AXIS_ATTRIBUTE) {
135             buffer.append('@');
136             buffer.append(nodeTest);
137         } else if (axis == Compiler.AXIS_SELF && nodeTest instanceof NodeTypeTest && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
138             buffer.append(".");
139         } else if (axis == Compiler.AXIS_PARENT && nodeTest instanceof NodeTypeTest && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
140             buffer.append("..");
141         } else if (axis == Compiler.AXIS_DESCENDANT_OR_SELF && nodeTest instanceof NodeTypeTest
142                 && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE && (predicates == null || predicates.length == 0)) {
143             buffer.append("");
144         } else {
145             buffer.append(axisToString(axis));
146             buffer.append("::");
147             buffer.append(nodeTest);
148         }
149         final Expression[] predicates = getPredicates();
150         if (predicates != null) {
151             for (final Expression predicate : predicates) {
152                 buffer.append('[');
153                 buffer.append(predicate);
154                 buffer.append(']');
155             }
156         }
157         return buffer.toString();
158     }
159 }