Debian Codegraph @

net/sf/saxon/functions/TreatFn.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class supports the XPath 2.0 functions exactly-one(), one-or-more(), zero-or-one().
22 * Because Saxon doesn't do strict static type checking, these are essentially identity
23 * functions; the run-time type checking is done as part of the function call mechanism
24 */
25
26 public abstract class TreatFn extends SystemFunction implements Callable {
27
28 /**
29 * Return the error code to be used for type errors
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