| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions.registry; |
| 9 |
|
| 10 |
import com.saxonica.functions.hof.AtomicConstructorFunction; |
| 11 |
import net.sf.saxon.Configuration; |
| 12 |
import net.sf.saxon.expr.*; |
| 13 |
import net.sf.saxon.functions.CallableFunction; |
| 14 |
import net.sf.saxon.functions.FunctionLibrary; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.om.Function; |
| 17 |
import net.sf.saxon.om.NamespaceResolver; |
| 18 |
import net.sf.saxon.om.StructuredQName; |
| 19 |
import net.sf.saxon.trans.SymbolicName; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.*; |
| 22 |
import net.sf.saxon.value.AtomicValue; |
| 23 |
import net.sf.saxon.value.EmptySequence; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
|
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class ConstructorFunctionLibrary implements FunctionLibrary { |
| 34 |
|
| 35 |
private Configuration config; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public ConstructorFunctionLibrary(Configuration config) { |
| 44 |
this.config = config; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public Function getFunctionItem(SymbolicName.F functionName, StaticContext staticContext) throws XPathException { |
| 64 |
if (functionName.getArity() != 1) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
final String uri = functionName.getComponentName().getURI(); |
| 68 |
final String localName = functionName.getComponentName().getLocalPart(); |
| 69 |
final SchemaType type = config.getSchemaType(new StructuredQName("", uri, localName)); |
| 70 |
if (type == null || type.isComplexType()) { |
| 71 |
return null; |
| 72 |
} |
| 73 |
final NamespaceResolver resolver = ((SimpleType) type).isNamespaceSensitive() ? staticContext.getNamespaceResolver() : null; |
| 74 |
if (type instanceof AtomicType) { |
| 75 |
return new AtomicConstructorFunction((AtomicType) type, resolver); |
| 76 |
} else if (type instanceof ListType) { |
| 77 |
return new ListConstructorFunction((ListType)type, resolver, true); |
| 78 |
} else { |
| 79 |
Callable callable = (context, arguments) -> { |
| 80 |
AtomicValue value = (AtomicValue) arguments[0].head(); |
| 81 |
if (value == null) { |
| 82 |
return EmptySequence.getInstance(); |
| 83 |
} |
| 84 |
return UnionConstructorFunction.cast(value, (UnionType) type, resolver, context.getConfiguration().getConversionRules()); |
| 85 |
}; |
| 86 |
SequenceType returnType = ((UnionType) type).getResultTypeOfCast(); |
| 87 |
return new CallableFunction(1, callable, |
| 88 |
new SpecificFunctionType(new SequenceType[]{SequenceType.OPTIONAL_ATOMIC}, returnType)); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public boolean isAvailable(SymbolicName.F functionName) { |
| 96 |
if (functionName.getArity() != 1) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
final String uri = functionName.getComponentName().getURI(); |
| 100 |
final String localName = functionName.getComponentName().getLocalPart(); |
| 101 |
final SchemaType type = config.getSchemaType(new StructuredQName("", uri, localName)); |
| 102 |
if (type == null || type.isComplexType()) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
if (type.isAtomicType() && ((AtomicType) type).isAbstract()) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
return type != AnySimpleType.getInstance(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public Expression bind(SymbolicName.F functionName, Expression[] arguments, StaticContext env, List<String> reasons) { |
| 134 |
final String uri = functionName.getComponentName().getURI(); |
| 135 |
final String localName = functionName.getComponentName().getLocalPart(); |
| 136 |
boolean builtInNamespace = uri.equals(NamespaceConstant.SCHEMA); |
| 137 |
if (builtInNamespace) { |
| 138 |
|
| 139 |
if (functionName.getArity() != 1) { |
| 140 |
reasons.add("A constructor function must have exactly one argument"); |
| 141 |
return null; |
| 142 |
} |
| 143 |
SimpleType type = Type.getBuiltInSimpleType(uri, localName); |
| 144 |
if (type != null) { |
| 145 |
if (type.isAtomicType()) { |
| 146 |
if (((AtomicType) type).isAbstract()) { |
| 147 |
reasons.add("Abstract type used in constructor function: {" + uri + '}' + localName); |
| 148 |
return null; |
| 149 |
} else { |
| 150 |
CastExpression cast = new CastExpression(arguments[0], (AtomicType) type, true); |
| 151 |
if (arguments[0] instanceof StringLiteral) { |
| 152 |
cast.setOperandIsStringLiteral(true); |
| 153 |
} |
| 154 |
return cast; |
| 155 |
} |
| 156 |
} else if (type.isUnionType()) { |
| 157 |
NamespaceResolver resolver = env.getNamespaceResolver(); |
| 158 |
UnionConstructorFunction ucf = new UnionConstructorFunction((UnionType) type, resolver, true); |
| 159 |
return new StaticFunctionCall(ucf, arguments); |
| 160 |
} else { |
| 161 |
NamespaceResolver resolver = env.getNamespaceResolver(); |
| 162 |
try { |
| 163 |
ListConstructorFunction lcf = new ListConstructorFunction((ListType)type, resolver, true); |
| 164 |
return new StaticFunctionCall(lcf, arguments); |
| 165 |
} catch (MissingComponentException e) { |
| 166 |
reasons.add("Missing schema component: " + e.getMessage()); |
| 167 |
return null; |
| 168 |
} |
| 169 |
} |
| 170 |
} else { |
| 171 |
reasons.add("Unknown constructor function: {" + uri + '}' + localName); |
| 172 |
return null; |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
if (arguments.length == 1) { |
| 180 |
SchemaType st = config.getSchemaType(new StructuredQName("", uri, localName)); |
| 181 |
if (st instanceof SimpleType) { |
| 182 |
if (st instanceof AtomicType) { |
| 183 |
return new CastExpression(arguments[0], (AtomicType) st, true); |
| 184 |
} else if (st instanceof ListType && env.getXPathVersion() >= 30) { |
| 185 |
NamespaceResolver resolver = env.getNamespaceResolver(); |
| 186 |
try { |
| 187 |
ListConstructorFunction lcf = new ListConstructorFunction((ListType) st, resolver, true); |
| 188 |
return new StaticFunctionCall(lcf, arguments); |
| 189 |
} catch (MissingComponentException e) { |
| 190 |
reasons.add("Missing schema component: " + e.getMessage()); |
| 191 |
return null; |
| 192 |
} |
| 193 |
} else if (((SimpleType) st).isUnionType() && env.getXPathVersion() >= 30) { |
| 194 |
NamespaceResolver resolver = env.getNamespaceResolver(); |
| 195 |
UnionConstructorFunction ucf = new UnionConstructorFunction((UnionType) st, resolver, true); |
| 196 |
return new StaticFunctionCall(ucf, arguments); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public FunctionLibrary copy() { |
| 213 |
return this; |
| 214 |
} |
| 215 |
|
| 216 |
} |