1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.compiler;
18
19 import org.apache.commons.jxpath.ri.Compiler;
20
21
22
23
24
25 public class Step {
26 private int axis;
27 private NodeTest nodeTest;
28 private Expression[] predicates;
29
30
31
32
33
34
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
44
45
46 public int getAxis() {
47 return axis;
48 }
49
50
51
52
53
54 public NodeTest getNodeTest() {
55 return nodeTest;
56 }
57
58
59
60
61
62 public Expression[] getPredicates() {
63 return predicates;
64 }
65
66
67
68
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
128
129
130
131
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 }