Debian Codegraph @

net/sf/saxon/expr/UnionCastableFunction.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.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 * Function to test castability to a union type
21 */
22 public class UnionCastableFunction extends UnionConstructorFunction {
23
24 /**
25 * Create a cast expression to a union type
26 * @param targetType the union type that is the result of the cast
27 * @param resolver namespace resolver in case the type is namespace-sensitive
28 * @param allowEmpty true if an empty sequence may be supplied as input, converting to an empty sequence on output
29 */
30
31 public UnionCastableFunction(UnionType targetType, NamespaceResolver resolver, boolean allowEmpty) {
32 super(targetType, resolver, allowEmpty);
33 }
34
35 /**
36 * Get the item type of the function item
37 *
38 * @return the function item's type
39 */
40 public FunctionItemType getFunctionItemType() {
41 // The function accepts any sequence as input, so it can do its own atomization
42 return new SpecificFunctionType(
43 new SequenceType[]{SequenceType.ANY_SEQUENCE}, SequenceType.SINGLE_BOOLEAN);
44 }
45
46 /**
47 * Get the name of the function, or null if it is anonymous
48 *
49 * @return the function name, or null for an anonymous function
50 */
51 public StructuredQName getFunctionName() {
52 return null;
53 }
54
55 /**
56 * Get the effective boolean value of the expression
57 *
58 * @param context The context in which the expression is to be evaluated
59 * @return the effective boolean value
60 * @throws XPathException if an error occurs evaluating the operand
61 */
62
63 private boolean effectiveBooleanValue(SequenceIterator iter, XPathContext context) throws XPathException {
64 // This method does its own atomization so that it can distinguish between atomization
65 // failures and casting failures
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 * Method to perform the castable check of an atomic value to a union type
100 *
101 * @param value the input value to be converted. Must not be null.
102 * @param context the XPath dynamic evaluation context
103 * @return the result of the conversion (may be a sequence if the union includes list types in its membership)
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 * Invoke the function
117 *
118 * @param context the XPath dynamic evaluation context
119 * @param args the actual arguments to be supplied
120 * @return the result of invoking the function
121 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs within the function
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 // Copyright (c) 2018 Saxonica Limited.
132
133