| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Callable; |
| 11 |
import net.sf.saxon.expr.CardinalityCheckingIterator; |
| 12 |
import net.sf.saxon.expr.StaticProperty; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.expr.parser.RoleDiagnostic; |
| 15 |
import net.sf.saxon.om.LazySequence; |
| 16 |
import net.sf.saxon.om.Sequence; |
| 17 |
import net.sf.saxon.om.SequenceIterator; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public abstract class TreatFn extends SystemFunction implements Callable { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract String getErrorCodeForTypeErrors(); |
| 33 |
|
| 34 |
public abstract int getRequiredCardinality(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 40 |
SequenceIterator<?> iterator = arguments[0].iterate(); |
| 41 |
int card = getRequiredCardinality(); |
| 42 |
RoleDiagnostic role = makeRoleDiagnostic(); |
| 43 |
iterator = new CardinalityCheckingIterator<>(iterator, card, role, null); |
| 44 |
return new LazySequence<>(iterator); |
| 45 |
} |
| 46 |
|
| 47 |
public RoleDiagnostic makeRoleDiagnostic() { |
| 48 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.FUNCTION, getFunctionName().getDisplayName(), 0); |
| 49 |
role.setErrorCode(getErrorCodeForTypeErrors()); |
| 50 |
return role; |
| 51 |
} |
| 52 |
|
| 53 |
public String getStreamerName() { |
| 54 |
return "TreatFn"; |
| 55 |
} |
| 56 |
|
| 57 |
public static class ExactlyOne extends TreatFn { |
| 58 |
public int getRequiredCardinality() { |
| 59 |
return StaticProperty.EXACTLY_ONE; |
| 60 |
} |
| 61 |
|
| 62 |
public String getErrorCodeForTypeErrors() { |
| 63 |
return "FORG0005"; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public static class OneOrMore extends TreatFn { |
| 68 |
public int getRequiredCardinality() { |
| 69 |
return StaticProperty.ALLOWS_ONE_OR_MORE; |
| 70 |
} |
| 71 |
|
| 72 |
public String getErrorCodeForTypeErrors() { |
| 73 |
return "FORG0004"; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public static class ZeroOrOne extends TreatFn { |
| 78 |
public int getRequiredCardinality() { |
| 79 |
return StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 80 |
} |
| 81 |
|
| 82 |
public String getErrorCodeForTypeErrors() { |
| 83 |
return "FORG0003"; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
} |
| 89 |
|