| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.iter; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.AdjacentTextNodeMerger; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.NodeInfo; |
| 13 |
import net.sf.saxon.om.SequenceIterator; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 16 |
import net.sf.saxon.tree.util.Orphan; |
| 17 |
import net.sf.saxon.type.Type; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class AdjacentTextNodeMergingIterator implements LookaheadIterator<Item<?>> { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private SequenceIterator<?> base; |
| 30 |
private Item<?> next; |
| 31 |
|
| 32 |
public AdjacentTextNodeMergingIterator(SequenceIterator<?> base) throws XPathException { |
| 33 |
this.base = base; |
| 34 |
next = base.next(); |
| 35 |
} |
| 36 |
|
| 37 |
public boolean hasNext() { |
| 38 |
return next != null; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
public Item next() throws XPathException { |
| 43 |
Item current = next; |
| 44 |
if (current == null) { |
| 45 |
return null; |
| 46 |
} |
| 47 |
next = base.next(); |
| 48 |
|
| 49 |
if (AdjacentTextNodeMerger.isTextNode(current)) { |
| 50 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256); |
| 51 |
fsb.append(current.getStringValueCS()); |
| 52 |
while (AdjacentTextNodeMerger.isTextNode(next)) { |
| 53 |
fsb.append(next.getStringValueCS() ); |
| 54 |
|
| 55 |
next = base.next(); |
| 56 |
} |
| 57 |
if (fsb.isEmpty()) { |
| 58 |
return next(); |
| 59 |
} else { |
| 60 |
Orphan o = new Orphan(((NodeInfo) current).getConfiguration()); |
| 61 |
o.setNodeKind(Type.TEXT); |
| 62 |
o.setStringValue(fsb); |
| 63 |
current = o; |
| 64 |
return current; |
| 65 |
} |
| 66 |
} else { |
| 67 |
return current; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public void close() { |
| 72 |
base.close(); |
| 73 |
} |
| 74 |
|
| 75 |
public int getProperties() { |
| 76 |
return LOOKAHEAD; |
| 77 |
} |
| 78 |
} |
| 79 |
|