| 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 |
|
| 24 |
public class ListConstructorFunction extends AbstractFunction { |
| 25 |
|
| 26 |
protected ListType targetType; |
| 27 |
protected NamespaceResolver nsResolver; |
| 28 |
protected boolean allowEmpty; |
| 29 |
protected SimpleType memberType; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public ListConstructorFunction(ListType targetType, NamespaceResolver resolver, boolean allowEmpty) throws MissingComponentException { |
| 39 |
this.targetType = targetType; |
| 40 |
this.nsResolver = resolver; |
| 41 |
this.allowEmpty = allowEmpty; |
| 42 |
this.memberType = targetType.getItemType(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public ListType getTargetType() { |
| 52 |
return targetType; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public SimpleType getMemberType() { |
| 61 |
return memberType; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public boolean isAllowEmpty() { |
| 71 |
return allowEmpty; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public FunctionItemType getFunctionItemType() { |
| 81 |
AtomicType resultType = BuiltInAtomicType.ANY_ATOMIC; |
| 82 |
if (memberType.isAtomicType()) { |
| 83 |
resultType = (AtomicType) memberType; |
| 84 |
} |
| 85 |
|
| 86 |
SequenceType argType = allowEmpty ? SequenceType.OPTIONAL_ATOMIC : SequenceType.SINGLE_ATOMIC; |
| 87 |
|
| 88 |
return new SpecificFunctionType( |
| 89 |
new SequenceType[]{argType}, |
| 90 |
SequenceType.makeSequenceType(resultType, StaticProperty.ALLOWS_ZERO_OR_MORE)); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public StructuredQName getFunctionName() { |
| 99 |
return targetType.getStructuredQName(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public String getDescription() { |
| 110 |
return getFunctionName().getDisplayName(); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public int getArity() { |
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public AtomicSequence call(XPathContext context, Sequence[] args) throws XPathException { |
| 132 |
AtomicValue val = (AtomicValue) args[0].head(); |
| 133 |
if (val == null) { |
| 134 |
if (allowEmpty) { |
| 135 |
return EmptyAtomicSequence.getInstance(); |
| 136 |
} else { |
| 137 |
XPathException e = new XPathException("Cast expression does not allow an empty sequence to be supplied", "XPTY0004"); |
| 138 |
e.setIsTypeError(true); |
| 139 |
throw e; |
| 140 |
} |
| 141 |
} |
| 142 |
if (!(val instanceof StringValue) || val instanceof AnyURIValue) { |
| 143 |
XPathException e = new XPathException("Only xs:string and xs:untypedAtomic can be cast to a list type", "XPTY0004"); |
| 144 |
e.setIsTypeError(true); |
| 145 |
throw e; |
| 146 |
} |
| 147 |
|
| 148 |
ConversionRules rules = context.getConfiguration().getConversionRules(); |
| 149 |
CharSequence cs = val.getStringValueCS(); |
| 150 |
ValidationFailure failure = targetType.validateContent(cs, nsResolver, rules); |
| 151 |
if (failure != null) { |
| 152 |
throw failure.makeException(); |
| 153 |
} |
| 154 |
return targetType.getTypedValue(cs, nsResolver, rules); |
| 155 |
} |
| 156 |
} |
| 157 |
|