Debian Codegraph @

net/sf/saxon/functions/Concat.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.*;
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 * Implementation of the fn:concat() function
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 * Get the roles of the arguments, for the purposes of streaming
40 *
41 * @return an array of OperandRole objects, one for each argument
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 * Get the item type of the function item
56 *
57 * @return the function item's type
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 * Allow the function to create an optimized call based on the values of the actual arguments
68 *
69 * @param visitor the expression visitor
70 * @param contextInfo information about the context item
71 * @param arguments the supplied arguments to the function call. Note: modifying the contents
72 * of this array should not be attempted, it is likely to have no effect.
73 * @return either a function call on this function, or an expression that delivers
74 * the same result, or null indicating that no optimization has taken place
75 * @throws XPathException if an error is detected
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 // Warning if all the arguments are booleans: probably a misuse of the '||' operator
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 * Get the required type of the nth argument
127 */
128
129 public SequenceType getRequiredType(int arg) {
130 return getDetails().argumentTypes[0];
131 // concat() is a special case
132 }
133
134 public String getCompilerName() {
135 return "ConcatCompiler";
136 }
137
138
139
140 }
141