| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.wrapper; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.event.Stripper; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.lib.NamespaceConstant; |
| 14 |
import net.sf.saxon.om.AtomicSequence; |
| 15 |
import net.sf.saxon.om.AxisInfo; |
| 16 |
import net.sf.saxon.om.NameOfNode; |
| 17 |
import net.sf.saxon.om.NodeInfo; |
| 18 |
import net.sf.saxon.pattern.NodeKindTest; |
| 19 |
import net.sf.saxon.pattern.NodeTest; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 22 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 23 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 24 |
import net.sf.saxon.type.ComplexType; |
| 25 |
import net.sf.saxon.type.SchemaType; |
| 26 |
import net.sf.saxon.type.Type; |
| 27 |
import net.sf.saxon.type.UType; |
| 28 |
import net.sf.saxon.value.Whitespace; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class SpaceStrippedNode extends AbstractVirtualNode implements WrappingFunction { |
| 43 |
|
| 44 |
protected SpaceStrippedNode() { |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected SpaceStrippedNode(NodeInfo node, SpaceStrippedNode parent) { |
| 56 |
this.node = node; |
| 57 |
this.parent = parent; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected static SpaceStrippedNode makeWrapper(NodeInfo node, |
| 72 |
SpaceStrippedDocument docWrapper, |
| 73 |
SpaceStrippedNode parent) { |
| 74 |
SpaceStrippedNode wrapper = new SpaceStrippedNode(node, parent); |
| 75 |
wrapper.docWrapper = docWrapper; |
| 76 |
return wrapper; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public VirtualNode makeWrapper(NodeInfo node, VirtualNode parent) { |
| 89 |
SpaceStrippedNode wrapper = new SpaceStrippedNode(node, (SpaceStrippedNode) parent); |
| 90 |
wrapper.docWrapper = this.docWrapper; |
| 91 |
return wrapper; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static boolean isPreservedNode(NodeInfo node, SpaceStrippedDocument docWrapper, NodeInfo actualParent) { |
| 103 |
|
| 104 |
|
| 105 |
if (node.getNodeKind() != Type.TEXT || actualParent == null || !Whitespace.isWhite(node.getStringValueCS())) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
SchemaType type = actualParent.getSchemaType(); |
| 111 |
if (type.isSimpleType() || ((ComplexType) type).isSimpleContent()) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if (docWrapper.containsPreserveSpace()) { |
| 117 |
NodeInfo p = actualParent; |
| 118 |
|
| 119 |
|
| 120 |
while (p.getNodeKind() == Type.ELEMENT) { |
| 121 |
String val = p.getAttributeValue(NamespaceConstant.XML, "space"); |
| 122 |
if (val != null) { |
| 123 |
if ("preserve".equals(val)) { |
| 124 |
return true; |
| 125 |
} else if ("default".equals(val)) { |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
p = p.getParent(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
if (docWrapper.containsAssertions()) { |
| 135 |
NodeInfo p = actualParent; |
| 136 |
|
| 137 |
|
| 138 |
while (p.getNodeKind() == Type.ELEMENT) { |
| 139 |
SchemaType t = p.getSchemaType(); |
| 140 |
if (t instanceof ComplexType && ((ComplexType) t).hasAssertions()) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
p = p.getParent(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
try { |
| 149 |
int preserve = docWrapper.getStrippingRule().isSpacePreserving(NameOfNode.makeName(actualParent), null); |
| 150 |
return preserve == Stripper.ALWAYS_PRESERVE; |
| 151 |
} catch (XPathException e) { |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public AtomicSequence atomize() throws XPathException { |
| 166 |
if (getNodeKind() == Type.ELEMENT) { |
| 167 |
return getSchemaType().atomize(this); |
| 168 |
} else { |
| 169 |
return node.atomize(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public boolean equals(Object other) { |
| 182 |
if (other instanceof SpaceStrippedNode) { |
| 183 |
return node.equals(((SpaceStrippedNode) other).node); |
| 184 |
} else { |
| 185 |
return node.equals(other); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public int compareOrder( NodeInfo other) { |
| 201 |
if (other instanceof SpaceStrippedNode) { |
| 202 |
return node.compareOrder(((SpaceStrippedNode) other).node); |
| 203 |
} else { |
| 204 |
return node.compareOrder(other); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public CharSequence getStringValueCS() { |
| 214 |
|
| 215 |
switch (getNodeKind()) { |
| 216 |
case Type.DOCUMENT: |
| 217 |
case Type.ELEMENT: |
| 218 |
AxisIterator iter = iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.makeNodeKindTest(Type.TEXT)); |
| 219 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C64); |
| 220 |
while (true) { |
| 221 |
NodeInfo it = iter.next(); |
| 222 |
if (it == null) { |
| 223 |
break; |
| 224 |
} |
| 225 |
sb.append(it.getStringValueCS()); |
| 226 |
} |
| 227 |
return sb.condense(); |
| 228 |
default: |
| 229 |
return node.getStringValueCS(); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public NodeInfo getParent() { |
| 239 |
if (parent == null) { |
| 240 |
NodeInfo realParent = node.getParent(); |
| 241 |
if (realParent != null) { |
| 242 |
parent = makeWrapper(realParent, (SpaceStrippedDocument) docWrapper, null); |
| 243 |
} |
| 244 |
} |
| 245 |
return parent; |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { |
| 250 |
if (nodeTest.getUType().intersection(UType.TEXT) == UType.VOID || |
| 251 |
axisNumber == AxisInfo.ATTRIBUTE || axisNumber == AxisInfo.NAMESPACE) { |
| 252 |
|
| 253 |
return new WrappingIterator(node.iterateAxis(axisNumber, nodeTest), this, getParentForAxis(axisNumber)); |
| 254 |
} else { |
| 255 |
return new StrippingIterator(node.iterateAxis(axisNumber, nodeTest), getParentForAxis(axisNumber)); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public AxisIterator iterateAxis(byte axisNumber) { |
| 268 |
switch (axisNumber) { |
| 269 |
case AxisInfo.ATTRIBUTE: |
| 270 |
case AxisInfo.NAMESPACE: |
| 271 |
return new WrappingIterator(node.iterateAxis(axisNumber), this, this); |
| 272 |
case AxisInfo.CHILD: |
| 273 |
return new StrippingIterator(node.iterateAxis(axisNumber), this); |
| 274 |
case AxisInfo.FOLLOWING_SIBLING: |
| 275 |
case AxisInfo.PRECEDING_SIBLING: |
| 276 |
SpaceStrippedNode parent = (SpaceStrippedNode) getParent(); |
| 277 |
if (parent == null) { |
| 278 |
return EmptyIterator.OfNodes.THE_INSTANCE; |
| 279 |
} else { |
| 280 |
return new StrippingIterator(node.iterateAxis(axisNumber), parent); |
| 281 |
} |
| 282 |
default: |
| 283 |
return new StrippingIterator(node.iterateAxis(axisNumber), null); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
private SpaceStrippedNode getParentForAxis(byte axisNumber) { |
| 288 |
switch (axisNumber) { |
| 289 |
case AxisInfo.CHILD: |
| 290 |
case AxisInfo.ATTRIBUTE: |
| 291 |
case AxisInfo.NAMESPACE: |
| 292 |
return this; |
| 293 |
case AxisInfo.FOLLOWING_SIBLING: |
| 294 |
case AxisInfo.PRECEDING_SIBLING: |
| 295 |
return (SpaceStrippedNode)getParent(); |
| 296 |
default: |
| 297 |
return null; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 306 |
|
| 307 |
|
| 308 |
Stripper stripper = new Stripper(((SpaceStrippedDocument) docWrapper).getStrippingRule(), out); |
| 309 |
node.copy(stripper, copyOptions, locationId); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
private final class StrippingIterator implements AxisIterator { |
| 322 |
|
| 323 |
AxisIterator base; |
| 324 |
SpaceStrippedNode parent; |
| 325 |
NodeInfo currentVirtualNode; |
| 326 |
int position; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public StrippingIterator(AxisIterator base, SpaceStrippedNode parent) { |
| 337 |
this.base = base; |
| 338 |
this.parent = parent; |
| 339 |
position = 0; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public NodeInfo next() { |
| 345 |
NodeInfo nextRealNode; |
| 346 |
while (true) { |
| 347 |
nextRealNode = base.next(); |
| 348 |
if (nextRealNode == null) { |
| 349 |
return null; |
| 350 |
} |
| 351 |
if (isPreserved(nextRealNode)) { |
| 352 |
break; |
| 353 |
} |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
currentVirtualNode = makeWrapper(nextRealNode, (SpaceStrippedDocument) docWrapper, parent); |
| 358 |
position++; |
| 359 |
return currentVirtualNode; |
| 360 |
} |
| 361 |
|
| 362 |
private boolean isPreserved(NodeInfo nextRealNode) { |
| 363 |
if (nextRealNode.getNodeKind() != Type.TEXT) { |
| 364 |
return true; |
| 365 |
} |
| 366 |
NodeInfo actualParent = |
| 367 |
parent == null ? nextRealNode.getParent() : parent.node; |
| 368 |
return isPreservedNode(nextRealNode, (SpaceStrippedDocument)docWrapper, actualParent); |
| 369 |
} |
| 370 |
|
| 371 |
public void close() { |
| 372 |
base.close(); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public int getProperties() { |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
} |
| 393 |
|