1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jxpath.ri.compiler;
19
20 import org.apache.commons.jxpath.ri.Compiler;
21
22
23
24
25 public class Step {
26
27
28
29
30
31
32
33
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
74
75
76
77
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
87
88
89
90 public int getAxis() {
91 return axis;
92 }
93
94
95
96
97
98
99 public NodeTest getNodeTest() {
100 return nodeTest;
101 }
102
103
104
105
106
107
108 public Expression[] getPredicates() {
109 return predicates;
110 }
111
112
113
114
115
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 }