Debian Codegraph @

net/sf/saxon/tree/iter/AdjacentTextNodeMergingIterator.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * AdjacentTextNodeMergingIterator is an iterator that eliminates zero-length text nodes
21 * and merges adjacent text nodes from the underlying iterator
22 */
23
24 public class AdjacentTextNodeMergingIterator implements LookaheadIterator<Item<?>> {
25
26 // Ideally we would specify bounds: AdjacentTextNodeMergingIterator<? extends Item super NodeInfo>,
27 // but Java doesn't allow both an upper and a lower bound
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 /*@Nullable*/
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() /*.toString() */);
54 // NOTE: toString() shouldn't be necessary - added 2011-05-05 for bug workaround; removed again 2011-07-14
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