| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.functions.AbstractFunction; |
| 11 |
import net.sf.saxon.lib.ConversionRules; |
| 12 |
import net.sf.saxon.om.*; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.type.*; |
| 15 |
import net.sf.saxon.value.AnyURIValue; |
| 16 |
import net.sf.saxon.value.AtomicValue; |
| 17 |
import net.sf.saxon.value.SequenceType; |
| 18 |
import net.sf.saxon.value.StringValue; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class UnionConstructorFunction extends AbstractFunction { |
| 24 |
|
| 25 |
protected UnionType targetType; |
| 26 |
protected NamespaceResolver resolver; |
| 27 |
protected boolean allowEmpty; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public UnionConstructorFunction(UnionType targetType, NamespaceResolver resolver, boolean allowEmpty) { |
| 36 |
this.targetType = targetType; |
| 37 |
this.resolver = resolver; |
| 38 |
this.allowEmpty = allowEmpty; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected OperandRole getOperandRole() { |
| 47 |
return OperandRole.SINGLE_ATOMIC; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public boolean isAllowEmpty() { |
| 56 |
return allowEmpty; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public UnionType getTargetType() { |
| 65 |
return targetType; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public NamespaceResolver getNamespaceResolver() { |
| 74 |
return resolver; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public FunctionItemType getFunctionItemType() { |
| 83 |
SequenceType resultType = targetType.getResultTypeOfCast(); |
| 84 |
|
| 85 |
SequenceType argType = allowEmpty ? SequenceType.OPTIONAL_ATOMIC : SequenceType.SINGLE_ATOMIC; |
| 86 |
|
| 87 |
return new SpecificFunctionType( |
| 88 |
new SequenceType[]{argType}, resultType); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public StructuredQName getFunctionName() { |
| 97 |
return targetType.getStructuredQName(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public String getDescription() { |
| 108 |
return getFunctionName().getDisplayName(); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public int getArity() { |
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public AtomicSequence cast(AtomicValue value, XPathContext context) |
| 131 |
throws XPathException { |
| 132 |
ConversionRules rules = context.getConfiguration().getConversionRules(); |
| 133 |
if (value == null) { |
| 134 |
throw new NullPointerException(); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if (value instanceof StringValue && !(value instanceof AnyURIValue)) { |
| 140 |
try { |
| 141 |
return targetType.getTypedValue(value.getStringValueCS(), resolver, rules); |
| 142 |
} catch (ValidationException e) { |
| 143 |
e.setErrorCode("FORG0001"); |
| 144 |
throw e; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
AtomicType label = value.getItemType(); |
| 151 |
Iterable<PlainType> memberTypes = targetType.getPlainMemberTypes(); |
| 152 |
|
| 153 |
|
| 154 |
if (targetType.isPlainType()) { |
| 155 |
for (PlainType member : memberTypes) { |
| 156 |
if (label.equals(member)) { |
| 157 |
return value; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
for (PlainType member : memberTypes) { |
| 162 |
AtomicType t = label; |
| 163 |
while (t != null) { |
| 164 |
if (t.equals(member)) { |
| 165 |
return value; |
| 166 |
} else { |
| 167 |
t = t.getBaseType() instanceof AtomicType ? (AtomicType) t.getBaseType() : null; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
for (PlainType type : memberTypes) { |
| 176 |
if (type instanceof AtomicType) { |
| 177 |
Converter c = rules.getConverter(value.getItemType(), (AtomicType) type); |
| 178 |
if (c != null) { |
| 179 |
ConversionResult result = c.convert(value); |
| 180 |
if (result instanceof AtomicValue) { |
| 181 |
|
| 182 |
if (!targetType.isPlainType()) { |
| 183 |
ValidationFailure vf = targetType.checkAgainstFacets((AtomicValue)result, rules); |
| 184 |
if (vf == null) { |
| 185 |
return (AtomicValue) result; |
| 186 |
} |
| 187 |
} else { |
| 188 |
return (AtomicValue) result; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
throw new XPathException("Cannot convert the supplied value to " + targetType.getDescription(), "FORG0001"); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public AtomicSequence call(XPathContext context, Sequence[] args) throws XPathException { |
| 207 |
AtomicValue val = (AtomicValue) args[0].head(); |
| 208 |
if (val == null) { |
| 209 |
if (allowEmpty) { |
| 210 |
return EmptyAtomicSequence.getInstance(); |
| 211 |
} else { |
| 212 |
XPathException e = new XPathException("Cast expression does not allow an empty sequence to be supplied", "XPTY0004"); |
| 213 |
e.setIsTypeError(true); |
| 214 |
throw e; |
| 215 |
} |
| 216 |
} |
| 217 |
return cast(val, context); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public static AtomicSequence cast(AtomicValue value, UnionType targetType, |
| 232 |
NamespaceResolver nsResolver, ConversionRules rules) |
| 233 |
throws XPathException { |
| 234 |
|
| 235 |
if (value == null) { |
| 236 |
throw new NullPointerException(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if (value instanceof StringValue && !(value instanceof AnyURIValue)) { |
| 242 |
try { |
| 243 |
return targetType.getTypedValue(value.getStringValueCS(), nsResolver, rules); |
| 244 |
} catch (ValidationException e) { |
| 245 |
e.setErrorCode("FORG0001"); |
| 246 |
throw e; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
AtomicType label = value.getItemType(); |
| 253 |
Iterable<PlainType> memberTypes = targetType.getPlainMemberTypes(); |
| 254 |
|
| 255 |
|
| 256 |
for (PlainType member : memberTypes) { |
| 257 |
if (label.equals(member)) { |
| 258 |
return value; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
for (PlainType member : memberTypes) { |
| 263 |
AtomicType t = label; |
| 264 |
while (t != null) { |
| 265 |
if (t.equals(member)) { |
| 266 |
return value; |
| 267 |
} else { |
| 268 |
t = t.getBaseType() instanceof AtomicType ? (AtomicType) t.getBaseType() : null; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
for (PlainType type : memberTypes) { |
| 276 |
if (type instanceof AtomicType) { |
| 277 |
Converter c = rules.getConverter(value.getItemType(), (AtomicType) type); |
| 278 |
if (c != null) { |
| 279 |
ConversionResult result = c.convert(value); |
| 280 |
if (result instanceof AtomicValue) { |
| 281 |
return (AtomicValue) result; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
throw new XPathException("Cannot convert the supplied value to " + targetType.getDescription(), "FORG0001"); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|