| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.s9api; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 14 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.s9api.streams.Step; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.FunctionItemType; |
| 19 |
import net.sf.saxon.type.TypeHierarchy; |
| 20 |
|
| 21 |
import java.util.stream.Stream; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class XdmFunctionItem extends XdmItem { |
| 28 |
|
| 29 |
protected XdmFunctionItem() {} |
| 30 |
|
| 31 |
public XdmFunctionItem(Function fi) { |
| 32 |
setValue(fi); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public QName getName() { |
| 42 |
Function fi = (Function) getUnderlyingValue(); |
| 43 |
StructuredQName sq = fi.getFunctionName(); |
| 44 |
return sq == null ? null : new QName(sq); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public int getArity() { |
| 54 |
Function fi = (Function) getUnderlyingValue(); |
| 55 |
return fi.getArity(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
public boolean isAtomicValue() { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static XdmFunctionItem getSystemFunction(Processor processor, QName name, int arity) throws SaxonApiException { |
| 79 |
try { |
| 80 |
Configuration config = processor.getUnderlyingConfiguration(); |
| 81 |
Function f = config.getSystemFunction(name.getStructuredQName(), arity); |
| 82 |
return f==null ? null : new XdmFunctionItem(f); |
| 83 |
} catch (XPathException e) { |
| 84 |
throw new SaxonApiException(e); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public java.util.function.Function<? super XdmValue, ? extends XdmValue> asFunction(Processor processor) { |
| 98 |
if (getArity() == 1) { |
| 99 |
return (java.util.function.Function<XdmValue, XdmValue>) arg -> { |
| 100 |
try { |
| 101 |
return XdmFunctionItem.this.call(processor, arg); |
| 102 |
} catch (SaxonApiException e) { |
| 103 |
throw new SaxonApiUncheckedException(e); |
| 104 |
} |
| 105 |
}; |
| 106 |
} else { |
| 107 |
throw new IllegalStateException("Function arity must be one"); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public Step<XdmItem> asStep(Processor processor) { |
| 122 |
if (getArity() == 1) { |
| 123 |
return new Step<XdmItem>() { |
| 124 |
public Stream<? extends XdmItem> apply(XdmItem arg) { |
| 125 |
try { |
| 126 |
return XdmFunctionItem.this.call(processor, arg).stream(); |
| 127 |
} catch (SaxonApiException e) { |
| 128 |
throw new SaxonApiUncheckedException(e); |
| 129 |
} |
| 130 |
} |
| 131 |
}; |
| 132 |
} else { |
| 133 |
throw new IllegalStateException("Function arity must be one"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public XdmValue call(Processor processor, XdmValue... arguments) throws SaxonApiException { |
| 149 |
if (arguments.length != getArity()) { |
| 150 |
throw new SaxonApiException("Supplied " + arguments.length + " arguments, required " + getArity()); |
| 151 |
} |
| 152 |
try { |
| 153 |
Function fi = (Function) getUnderlyingValue(); |
| 154 |
FunctionItemType type = fi.getFunctionItemType(); |
| 155 |
Sequence<?>[] argVals = new Sequence[arguments.length]; |
| 156 |
TypeHierarchy th = processor.getUnderlyingConfiguration().getTypeHierarchy(); |
| 157 |
for (int i = 0; i < arguments.length; i++) { |
| 158 |
net.sf.saxon.value.SequenceType required = type.getArgumentTypes()[i]; |
| 159 |
Sequence<?> val = arguments[i].getUnderlyingValue(); |
| 160 |
if (!required.matches(val, th)) { |
| 161 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, "", i); |
| 162 |
val = th.applyFunctionConversionRules(val, required, role, ExplicitLocation.UNKNOWN_LOCATION); |
| 163 |
} |
| 164 |
argVals[i] = val; |
| 165 |
} |
| 166 |
Configuration config = processor.getUnderlyingConfiguration(); |
| 167 |
Controller controller = new Controller(config); |
| 168 |
XPathContext context = controller.newXPathContext(); |
| 169 |
context = fi.makeNewContext(context, controller); |
| 170 |
|
| 171 |
Sequence<?> result = fi.call(context, argVals); |
| 172 |
if (!fi.isTrustedResultType()) { |
| 173 |
result = result.materialize(); |
| 174 |
net.sf.saxon.value.SequenceType required = type.getResultType(); |
| 175 |
if (!required.matches(result, th)) { |
| 176 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.FUNCTION_RESULT, "", 0); |
| 177 |
result = th.applyFunctionConversionRules(result.materialize(), required, role, ExplicitLocation.UNKNOWN_LOCATION); |
| 178 |
} |
| 179 |
} |
| 180 |
Sequence<?> se = result.iterate().materialize(); |
| 181 |
return XdmValue.wrap(se); |
| 182 |
} catch (XPathException e) { |
| 183 |
throw new SaxonApiException(e); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
} |