Debian Codegraph @

net/sf/saxon/functions/StringJoin.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.StaticProperty;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.One;
17 import net.sf.saxon.om.Sequence;
18 import net.sf.saxon.om.ZeroOrOne;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.tree.util.FastStringBuffer;
21 import net.sf.saxon.type.BuiltInAtomicType;
22 import net.sf.saxon.value.Cardinality;
23 import net.sf.saxon.value.StringValue;
24
25 /**
26 * fn:string-join(string* $sequence, string $separator)
27 */
28
29 public class StringJoin extends FoldingFunction {
30
31 private boolean returnEmptyIfEmpty;
32
33 /**
34 * Indicate that when the input sequence (first argument) is empty, the function should return
35 * an empty sequence rather than an empty string
36 *
37 * @param option true if an empty sequence should be returned when the input is an empty sequence.
38 */
39
40 public void setReturnEmptyIfEmpty(boolean option) {
41 returnEmptyIfEmpty = option;
42 }
43
44 public boolean isReturnEmptyIfEmpty() {
45 return returnEmptyIfEmpty;
46 }
47
48 /**
49 * Determine the cardinality of the function.
50 */
51 @Override
52 public int getCardinality(Expression[] arguments) {
53 if (returnEmptyIfEmpty) {
54 return StaticProperty.ALLOWS_ZERO_OR_ONE;
55 } else {
56 return StaticProperty.EXACTLY_ONE;
57 }
58 }
59
60 /**
61 * Determine whether two expressions are equivalent
62 */
63 @Override
64 public boolean equals(Object o) {
65 return (o instanceof StringJoin) &&
66 super.equals(o) &&
67 returnEmptyIfEmpty == ((StringJoin) o).returnEmptyIfEmpty;
68 }
69
70 /**
71 * Allow the function to create an optimized call based on the values of the actual arguments
72 *
73 * @param visitor the expression visitor
74 * @param contextInfo information about the context item
75 * @param arguments the supplied arguments to the function call
76 * @return either a function call on this function, or an expression that delivers
77 * the same result, or null indicating that no optimization has taken place
78 * @throws net.sf.saxon.trans.XPathException if an error is detected
79 */
80 @Override
81 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
82 Expression e2 = super.makeOptimizedFunctionCall(visitor, contextInfo, arguments);
83 if (e2 != null) {
84 return e2;
85 }
86 int card = arguments[0].getCardinality();
87 if (!Cardinality.allowsMany(card)) {
88 if (Cardinality.allowsZero(card) || arguments[0].getItemType().getPrimitiveItemType() != BuiltInAtomicType.STRING) {
89 return SystemFunction.makeCall("string", getRetainedStaticContext(), arguments[0]);
90 } else {
91 return arguments[0];
92 }
93 }
94 return null;
95 }
96
97 @Override
98 public Fold getFold(XPathContext context, Sequence... additionalArguments) throws XPathException {
99 CharSequence separator = "";
100 if (additionalArguments.length > 0) {
101 separator = additionalArguments[0].head().getStringValueCS();
102 }
103 return new StringJoinFold(separator);
104 }
105
106 private class StringJoinFold implements Fold {
107
108 private int position = 0;
109 private CharSequence separator;
110 private FastStringBuffer data;
111
112 public StringJoinFold(CharSequence separator) {
113 this.separator = separator;
114 this.data = new FastStringBuffer(FastStringBuffer.C64);
115 }
116
117 /**
118 * Process one item in the input sequence, returning a new copy of the working data
119 *
120 * @param item the item to be processed from the input sequence
121 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs
122 */
123 public void processItem(Item<?> item) throws XPathException {
124 if (position == 0) {
125 data.append(item.getStringValueCS());
126 position = 1;
127 } else {
128 data.append(separator);
129 data.append(item.getStringValueCS());
130 }
131 }
132
133 /**
134 * Ask whether the computation has completed. A function that can deliver its final
135 * result without reading the whole input should return true; this will be followed
136 * by a call on result() to deliver the final result.
137 */
138 public boolean isFinished() {
139 return false;
140 }
141
142 /**
143 * Compute the final result of the function, when all the input has been processed
144 * @return the result of the function
145 */
146 public ZeroOrOne<StringValue> result() {
147 if (position == 0 && returnEmptyIfEmpty) {
148 return ZeroOrOne.empty();
149 } else {
150 return One.string(data.toString());
151 }
152 }
153 }
154
155 public String getCompilerName() {
156 return "StringJoinCompiler";
157 }
158
159
160 }
161