| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.SystemFunctionCall; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.om.Item; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.om.SequenceTool; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.ItemType; |
| 19 |
import net.sf.saxon.type.Type; |
| 20 |
import net.sf.saxon.value.NumericValue; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class InsertBefore extends SystemFunction { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 39 |
NumericValue n = (NumericValue) arguments[1].head(); |
| 40 |
int pos = (int) n.longValue(); |
| 41 |
return SequenceTool.toLazySequence( |
| 42 |
new InsertIterator(arguments[0].iterate(), arguments[2].iterate(), pos)); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 47 |
return new SystemFunctionCall(this, arguments) { |
| 48 |
@Override |
| 49 |
public ItemType getItemType() { |
| 50 |
return Type.getCommonSuperType(getArg(0).getItemType(), getArg(2).getItemType()); |
| 51 |
} |
| 52 |
}; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static class InsertIterator implements SequenceIterator<Item<?>> { |
| 61 |
|
| 62 |
private SequenceIterator<?> base; |
| 63 |
private SequenceIterator<?> insert; |
| 64 |
private int insertPosition; |
| 65 |
private int position = 0; |
| 66 |
private boolean inserting = false; |
| 67 |
|
| 68 |
public InsertIterator(SequenceIterator<?> base, SequenceIterator<?> insert, int insertPosition) { |
| 69 |
this.base = base; |
| 70 |
this.insert = insert; |
| 71 |
this.insertPosition = insertPosition < 1 ? 1 : insertPosition; |
| 72 |
this.inserting = insertPosition == 1; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
public Item<?> next() throws XPathException { |
| 77 |
Item<?> nextItem; |
| 78 |
if (inserting) { |
| 79 |
nextItem = insert.next(); |
| 80 |
if (nextItem == null) { |
| 81 |
inserting = false; |
| 82 |
nextItem = base.next(); |
| 83 |
} |
| 84 |
} else { |
| 85 |
if (position == insertPosition - 1) { |
| 86 |
nextItem = insert.next(); |
| 87 |
if (nextItem == null) { |
| 88 |
nextItem = base.next(); |
| 89 |
} else { |
| 90 |
inserting = true; |
| 91 |
} |
| 92 |
} else { |
| 93 |
nextItem = base.next(); |
| 94 |
if (nextItem == null && position < insertPosition - 1) { |
| 95 |
inserting = true; |
| 96 |
nextItem = insert.next(); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
if (nextItem == null) { |
| 101 |
position = -1; |
| 102 |
return null; |
| 103 |
} else { |
| 104 |
position++; |
| 105 |
return nextItem; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
public void close() { |
| 110 |
base.close(); |
| 111 |
insert.close(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public int getProperties() { |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
public String getStreamerName() { |
| 131 |
return "InsertBefore"; |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|