| 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.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 |
|
| 27 |
|
| 28 |
|
| 29 |
public class StringJoin extends FoldingFunction { |
| 30 |
|
| 31 |
private boolean returnEmptyIfEmpty; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public void setReturnEmptyIfEmpty(boolean option) { |
| 41 |
returnEmptyIfEmpty = option; |
| 42 |
} |
| 43 |
|
| 44 |
public boolean isReturnEmptyIfEmpty() { |
| 45 |
return returnEmptyIfEmpty; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 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 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public boolean isFinished() { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 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 |
|