| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.type.SpecificFunctionType; |
| 11 |
import net.sf.saxon.om.*; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.type.FunctionItemType; |
| 14 |
import net.sf.saxon.type.UnionType; |
| 15 |
import net.sf.saxon.value.AtomicValue; |
| 16 |
import net.sf.saxon.value.BooleanValue; |
| 17 |
import net.sf.saxon.value.SequenceType; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class UnionCastableFunction extends UnionConstructorFunction { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public UnionCastableFunction(UnionType targetType, NamespaceResolver resolver, boolean allowEmpty) { |
| 32 |
super(targetType, resolver, allowEmpty); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public FunctionItemType getFunctionItemType() { |
| 41 |
|
| 42 |
return new SpecificFunctionType( |
| 43 |
new SequenceType[]{SequenceType.ANY_SEQUENCE}, SequenceType.SINGLE_BOOLEAN); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public StructuredQName getFunctionName() { |
| 52 |
return null; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private boolean effectiveBooleanValue(SequenceIterator iter, XPathContext context) throws XPathException { |
| 64 |
|
| 65 |
|
| 66 |
int count = 0; |
| 67 |
Item item; |
| 68 |
while ((item = iter.next()) != null) { |
| 69 |
if (item instanceof NodeInfo) { |
| 70 |
AtomicSequence atomizedValue = item.atomize(); |
| 71 |
int length = SequenceTool.getLength(atomizedValue); |
| 72 |
count += length; |
| 73 |
if (count > 1) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
if (length != 0) { |
| 77 |
AtomicValue av = atomizedValue.head(); |
| 78 |
if (!castable(av, context)) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
} else if (item instanceof AtomicValue) { |
| 83 |
AtomicValue av = (AtomicValue) item; |
| 84 |
count++; |
| 85 |
if (count > 1) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
if (!castable(av, context)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
} else { |
| 92 |
throw new XPathException("Input to 'castable' operator cannot be atomized", "XPTY0004"); |
| 93 |
} |
| 94 |
} |
| 95 |
return count != 0 || allowEmpty; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private boolean castable(AtomicValue value, XPathContext context) { |
| 106 |
try { |
| 107 |
cast(value, context); |
| 108 |
return true; |
| 109 |
} catch (XPathException err) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public BooleanValue call(XPathContext context, Sequence[] args) throws XPathException { |
| 124 |
boolean value = effectiveBooleanValue(args[0].iterate(), context); |
| 125 |
return BooleanValue.get(value); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|