| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.util; |
| 9 |
|
| 10 |
import net.sf.saxon.pattern.*; |
| 11 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 12 |
import net.sf.saxon.type.Type; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public abstract class SteppingNavigator { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
static <N extends SteppingNode<N>> N getFollowingNode(N start, N anchor) { |
| 33 |
N nodei = start.getFirstChild(); |
| 34 |
if (nodei != null) { |
| 35 |
return nodei; |
| 36 |
} |
| 37 |
if (start.isSameNodeInfo(anchor)) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
nodei = start; |
| 41 |
N parenti = start.getParent(); |
| 42 |
do { |
| 43 |
nodei = nodei.getNextSibling(); |
| 44 |
if (nodei != null) { |
| 45 |
return nodei; |
| 46 |
} else if (parenti.isSameNodeInfo(anchor)) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
nodei = parenti; |
| 50 |
parenti = parenti.getParent(); |
| 51 |
} while (parenti != null); |
| 52 |
|
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private interface Stepper<N extends SteppingNode<N>> { |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
N step(N node); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
private static class FollowingNodeStepper<N extends SteppingNode<N>> implements Stepper<N> { |
| 77 |
|
| 78 |
N anchor; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
FollowingNodeStepper(N anchor) { |
| 87 |
this.anchor = anchor; |
| 88 |
} |
| 89 |
|
| 90 |
public N step(N node) { |
| 91 |
return getFollowingNode(node, anchor); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private static class FollowingFilteredNodeStepper<N extends SteppingNode<N>> implements Stepper<N> { |
| 102 |
|
| 103 |
N anchor; |
| 104 |
NodeTest test; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
FollowingFilteredNodeStepper(N anchor, NodeTest test) { |
| 114 |
this.anchor = anchor; |
| 115 |
this.test = test; |
| 116 |
} |
| 117 |
|
| 118 |
public N step(N node) { |
| 119 |
do { |
| 120 |
node = getFollowingNode(node, anchor); |
| 121 |
} while (node != null && !test.matchesNode(node)); |
| 122 |
return node; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private static class FollowingElementStepper<N extends SteppingNode<N>> implements Stepper<N> { |
| 134 |
|
| 135 |
N anchor; |
| 136 |
String uri; |
| 137 |
String local; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
FollowingElementStepper(N anchor, String uri, String local) { |
| 148 |
this.anchor = anchor; |
| 149 |
this.uri = uri; |
| 150 |
this.local = local; |
| 151 |
} |
| 152 |
|
| 153 |
public N step(N node) { |
| 154 |
return node.getSuccessorElement(anchor, uri, local); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
private static class FollowingFingerprintedElementStepper<N extends SteppingNode<N>> implements Stepper<N> { |
| 165 |
|
| 166 |
N anchor; |
| 167 |
int fingerprint; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
FollowingFingerprintedElementStepper(N anchor, int fingerprint) { |
| 177 |
this.anchor = anchor; |
| 178 |
this.fingerprint = fingerprint; |
| 179 |
} |
| 180 |
|
| 181 |
public N step(N node) { |
| 182 |
do { |
| 183 |
node = getFollowingNode(node, anchor); |
| 184 |
} while (node != null && node.getFingerprint() != fingerprint); |
| 185 |
return node; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static class DescendantAxisIterator<N extends SteppingNode<N>> implements AxisIterator { |
| 195 |
|
| 196 |
private N start; |
| 197 |
private N current; |
| 198 |
|
| 199 |
private Stepper<N> stepper; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public DescendantAxisIterator(N start, boolean includeSelf, NodeTest test) { |
| 210 |
this.start = start; |
| 211 |
|
| 212 |
if (!(includeSelf && test.matchesNode(start))) { |
| 213 |
|
| 214 |
current = start; |
| 215 |
} |
| 216 |
|
| 217 |
if (test == null || test == AnyNodeTest.getInstance()) { |
| 218 |
stepper = new FollowingNodeStepper<>(start); |
| 219 |
} else if (test instanceof NameTest) { |
| 220 |
if (test.getPrimitiveType() == Type.ELEMENT) { |
| 221 |
NameTest nt = (NameTest) test; |
| 222 |
if (start.hasFingerprint()) { |
| 223 |
stepper = new FollowingFingerprintedElementStepper<>(start, nt.getFingerprint()); |
| 224 |
} else { |
| 225 |
stepper = new FollowingElementStepper<>(start, nt.getNamespaceURI(), nt.getLocalPart()); |
| 226 |
} |
| 227 |
} else { |
| 228 |
stepper = new FollowingFilteredNodeStepper<>(start, test); |
| 229 |
} |
| 230 |
} else if (test instanceof NodeKindTest) { |
| 231 |
if (test.getPrimitiveType() == Type.ELEMENT) { |
| 232 |
stepper = new FollowingElementStepper<>(start, null, null); |
| 233 |
} else { |
| 234 |
stepper = new FollowingFilteredNodeStepper<>(start, test); |
| 235 |
} |
| 236 |
} else if (test instanceof LocalNameTest) { |
| 237 |
if (test.getPrimitiveType() == Type.ELEMENT) { |
| 238 |
LocalNameTest nt = (LocalNameTest) test; |
| 239 |
stepper = new FollowingElementStepper<>(start, null, nt.getLocalName()); |
| 240 |
} else { |
| 241 |
stepper = new FollowingFilteredNodeStepper<>(start, test); |
| 242 |
} |
| 243 |
} else if (test instanceof NamespaceTest) { |
| 244 |
if (test.getPrimitiveType() == Type.ELEMENT) { |
| 245 |
NamespaceTest nt = (NamespaceTest) test; |
| 246 |
stepper = new FollowingElementStepper<>(start, nt.getNamespaceURI(), null); |
| 247 |
} else { |
| 248 |
stepper = new FollowingFilteredNodeStepper<>(start, test); |
| 249 |
} |
| 250 |
} else { |
| 251 |
stepper = new FollowingFilteredNodeStepper<>(start, test); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
public N next() { |
| 257 |
if (current == null) { |
| 258 |
|
| 259 |
current = start; |
| 260 |
return start; |
| 261 |
} |
| 262 |
N curr = stepper.step(current); |
| 263 |
|
| 264 |
return current = curr; |
| 265 |
} |
| 266 |
|
| 267 |
public void close() { |
| 268 |
} |
| 269 |
|
| 270 |
public int getProperties() { |
| 271 |
return 0; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|