| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.wrapper; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.om.AtomicSequence; |
| 14 |
import net.sf.saxon.om.AxisInfo; |
| 15 |
import net.sf.saxon.om.NodeInfo; |
| 16 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 17 |
import net.sf.saxon.pattern.NodeTest; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 20 |
import net.sf.saxon.tree.iter.SingleNodeIterator; |
| 21 |
import net.sf.saxon.tree.util.Navigator; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
import net.sf.saxon.value.UntypedAtomicValue; |
| 24 |
|
| 25 |
import javax.xml.transform.SourceLocator; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class SnapshotNode extends VirtualCopy implements NodeInfo { |
| 34 |
|
| 35 |
protected NodeInfo pivot; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected SnapshotNode(NodeInfo base, NodeInfo pivot) { |
| 47 |
super(base, pivot.getRoot()); |
| 48 |
this.pivot = pivot; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static SnapshotNode makeSnapshot(NodeInfo original) { |
| 59 |
SnapshotNode vc = new SnapshotNode(original, original); |
| 60 |
|
| 61 |
Configuration config = original.getConfiguration(); |
| 62 |
VirtualTreeInfo doc = new VirtualTreeInfo(config); |
| 63 |
long docNr = config.getDocumentNumberAllocator().allocateDocumentNumber(); |
| 64 |
doc.setDocumentNumber(docNr); |
| 65 |
doc.setCopyAccumulators(true); |
| 66 |
vc.tree = doc; |
| 67 |
doc.setRootNode(vc.getRoot()); |
| 68 |
|
| 69 |
return vc; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
protected SnapshotNode wrap(NodeInfo node) { |
| 81 |
SnapshotNode vc = new SnapshotNode(node, pivot); |
| 82 |
vc.tree = tree; |
| 83 |
return vc; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public CharSequence getStringValueCS() { |
| 94 |
if (Navigator.isAncestorOrSelf(original, pivot)) { |
| 95 |
return pivot.getStringValueCS(); |
| 96 |
} else { |
| 97 |
return original.getStringValueCS(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public NodeInfo getParent() { |
| 109 |
if (parent == null) { |
| 110 |
NodeInfo basep = original.getParent(); |
| 111 |
if (basep == null) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
parent = wrap(basep); |
| 115 |
} |
| 116 |
return parent; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
@Override |
| 126 |
public NodeInfo getRoot() { |
| 127 |
return super.getRoot(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 138 |
Navigator.copy(this, out, copyOptions, locationId); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public AtomicSequence atomize() throws XPathException { |
| 147 |
switch (getNodeKind()) { |
| 148 |
case Type.ATTRIBUTE: |
| 149 |
case Type.TEXT: |
| 150 |
case Type.COMMENT: |
| 151 |
case Type.PROCESSING_INSTRUCTION: |
| 152 |
case Type.NAMESPACE: |
| 153 |
return original.atomize(); |
| 154 |
default: |
| 155 |
if (Navigator.isAncestorOrSelf(pivot, original)) { |
| 156 |
return original.atomize(); |
| 157 |
} else { |
| 158 |
|
| 159 |
|
| 160 |
return new UntypedAtomicValue(pivot.getStringValueCS()); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public boolean isId() { |
| 173 |
return original.isId(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public boolean isIdref() { |
| 183 |
return original.isIdref(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public boolean isNilled() { |
| 193 |
return original.isNilled(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public String getPublicId() { |
| 208 |
return original instanceof SourceLocator ? original.getPublicId() : null; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { |
| 227 |
switch (getNodeKind()) { |
| 228 |
case Type.ATTRIBUTE: |
| 229 |
case Type.NAMESPACE: |
| 230 |
case Type.TEXT: |
| 231 |
case Type.COMMENT: |
| 232 |
case Type.PROCESSING_INSTRUCTION: |
| 233 |
return super.iterateAxis(axisNumber, nodeTest); |
| 234 |
default: |
| 235 |
if (!original.isSameNodeInfo(pivot) && Navigator.isAncestorOrSelf(original, pivot)) { |
| 236 |
|
| 237 |
switch (axisNumber) { |
| 238 |
case AxisInfo.CHILD: |
| 239 |
|
| 240 |
|
| 241 |
return SingleNodeIterator.makeIterator(getChildOfAncestorNode()); |
| 242 |
case AxisInfo.DESCENDANT: |
| 243 |
case AxisInfo.DESCENDANT_OR_SELF: |
| 244 |
|
| 245 |
AxisIterator iter = new Navigator.DescendantEnumeration( |
| 246 |
this, axisNumber == AxisInfo.DESCENDANT_OR_SELF, true); |
| 247 |
if (!(nodeTest instanceof AnyNodeTest)) { |
| 248 |
iter = new Navigator.AxisFilter(iter, nodeTest); |
| 249 |
} |
| 250 |
return iter; |
| 251 |
default: |
| 252 |
return super.iterateAxis(axisNumber, nodeTest); |
| 253 |
} |
| 254 |
|
| 255 |
} else { |
| 256 |
return super.iterateAxis(axisNumber, nodeTest); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
private NodeInfo getChildOfAncestorNode() { |
| 269 |
int pivotKind = pivot.getNodeKind(); |
| 270 |
SnapshotNode p = wrap(pivot); |
| 271 |
if ((pivotKind == Type.ATTRIBUTE || pivotKind == Type.NAMESPACE) && p.getParent().isSameNodeInfo(this)) { |
| 272 |
return null; |
| 273 |
} |
| 274 |
while (true) { |
| 275 |
SnapshotNode q = (SnapshotNode)p.getParent(); |
| 276 |
if (q == null) { |
| 277 |
throw new AssertionError(); |
| 278 |
} |
| 279 |
if (q.isSameNodeInfo(this)) { |
| 280 |
return p; |
| 281 |
} |
| 282 |
p = q; |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
@Override |
| 289 |
protected boolean isIncludedInCopy(NodeInfo sourceNode) { |
| 290 |
switch (sourceNode.getNodeKind()) { |
| 291 |
case Type.ATTRIBUTE: |
| 292 |
case Type.NAMESPACE: |
| 293 |
return isIncludedInCopy(sourceNode.getParent()); |
| 294 |
default: |
| 295 |
return Navigator.isAncestorOrSelf(pivot, sourceNode) || Navigator.isAncestorOrSelf(sourceNode, pivot); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
} |
| 301 |
|