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