Debian Codegraph @

net/sf/saxon/functions/InsertBefore.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.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 * The XPath 2.0 insert-before() function
24 */
25
26
27 public class InsertBefore extends SystemFunction {
28
29 /**
30 * Evaluate the expression as a general function call
31 *
32 * @param context the dynamic evaluation context
33 * @param arguments the values of the arguments, supplied as SequenceIterators
34 * @return the result of the evaluation, in the form of a SequenceIterator
35 * @throws net.sf.saxon.trans.XPathException
36 * if a dynamic error occurs during the evaluation of the expression
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 * Insertion iterator. This is supplied with an iterator over the base sequence,
57 * an iterator over the sequence to be inserted, and the insert position.
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 * Get properties of this iterator, as a bit-significant integer.
116 *
117 * @return the properties of this iterator. This will be some combination of
118 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
119 * and {@link #LOOKAHEAD}. It is always
120 * acceptable to return the value zero, indicating that there are no known special properties.
121 * It is acceptable for the properties of the iterator to change depending on its state.
122 */
123
124 public int getProperties() {
125 return 0;
126 }
127
128 }
129
130 public String getStreamerName() {
131 return "InsertBefore";
132 }
133
134 }
135