| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.oper.OperandArray; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.Sequence; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 19 |
import net.sf.saxon.type.FunctionItemType; |
| 20 |
import net.sf.saxon.type.SpecificFunctionType; |
| 21 |
import net.sf.saxon.value.SequenceType; |
| 22 |
import net.sf.saxon.value.StringValue; |
| 23 |
|
| 24 |
import java.util.Arrays; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class Concat extends SystemFunction { |
| 32 |
|
| 33 |
@Override |
| 34 |
protected Sequence<?> resultIfEmpty(int arg) { |
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public OperandRole[] getOperandRoles() { |
| 44 |
OperandRole[] roles = new OperandRole[getArity()]; |
| 45 |
OperandRole operandRole = new OperandRole(0, OperandUsage.ABSORPTION); |
| 46 |
for (int i = 0; i < getArity(); i++) { |
| 47 |
roles[i] = operandRole; |
| 48 |
} |
| 49 |
return roles; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Override |
| 60 |
public FunctionItemType getFunctionItemType() { |
| 61 |
SequenceType[] argTypes = new SequenceType[getArity()]; |
| 62 |
Arrays.fill(argTypes, SequenceType.OPTIONAL_ATOMIC); |
| 63 |
return new SpecificFunctionType(argTypes, SequenceType.SINGLE_STRING); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@Override |
| 78 |
public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException { |
| 79 |
if (OperandArray.every(arguments, |
| 80 |
arg -> arg.getCardinality() == StaticProperty.EXACTLY_ONE && arg.getItemType() == BuiltInAtomicType.BOOLEAN)) { |
| 81 |
|
| 82 |
visitor.getStaticContext().issueWarning( |
| 83 |
"Did you intend to apply string concatenation to boolean operands? " |
| 84 |
+ "Perhaps you intended 'or' rather than '||'. " |
| 85 |
+ "To suppress this warning, use string() on the arguments.", arguments[0].getLocation()); |
| 86 |
} |
| 87 |
return new SystemFunctionCall.Optimized(this, arguments) { |
| 88 |
@Override |
| 89 |
public CharSequence evaluateAsString(XPathContext context) throws XPathException { |
| 90 |
FastStringBuffer buffer = new FastStringBuffer(256); |
| 91 |
for (Operand o: operands()) { |
| 92 |
Item it = o.getChildExpression().evaluateItem(context); |
| 93 |
if (it != null) { |
| 94 |
buffer.append(it.getStringValueCS()); |
| 95 |
} |
| 96 |
} |
| 97 |
return buffer; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 102 |
return new StringValue(evaluateAsString(context)); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
private boolean isSingleBoolean(Expression arg) { |
| 109 |
return arg.getCardinality() == StaticProperty.EXACTLY_ONE && arg.getItemType() == BuiltInAtomicType.BOOLEAN; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 114 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 115 |
for (Sequence<?> arg : arguments) { |
| 116 |
Item item = arg.head(); |
| 117 |
if (item != null) { |
| 118 |
fsb.append(item.getStringValueCS()); |
| 119 |
} |
| 120 |
} |
| 121 |
return new StringValue(fsb); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public SequenceType getRequiredType(int arg) { |
| 130 |
return getDetails().argumentTypes[0]; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
public String getCompilerName() { |
| 135 |
return "ConcatCompiler"; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|