Debian Codegraph @

net/sf/saxon/expr/ListConstructorFunction.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.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 * A function item representing a constructor function for a list type
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 * Create the constructor function.
33 *
34 * @param targetType the type to which the function will convert its input
35 * @param resolver namespace resolver for use if the target type is namespace-sensitive
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 * Get the list type
47 *
48 * @return the last type to which we are casting
49 */
50
51 public ListType getTargetType() {
52 return targetType;
53 }
54
55 /**
56 * Get the list item type (member type)
57 * @return the item type of the list
58 */
59
60 public SimpleType getMemberType() {
61 return memberType;
62 }
63
64 /**
65 * Ask whether an empty sequence is allowed
66 *
67 * @return true if passing an empty sequence is allowed (and returns empty)
68 */
69
70 public boolean isAllowEmpty() {
71 return allowEmpty;
72 }
73
74
75 /**
76 * Get the item type of the function item
77 *
78 * @return the function item's type
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 * Get the name of the function, or null if it is anonymous
95 *
96 * @return the function name, or null for an anonymous inline function
97 */
98 public StructuredQName getFunctionName() {
99 return targetType.getStructuredQName();
100 }
101
102 /**
103 * Get a description of this function for use in error messages. For named functions, the description
104 * is the function name (as a lexical QName). For others, it might be, for example, "inline function",
105 * or "partially-applied ends-with function".
106 *
107 * @return a description of the function for use in error messages
108 */
109 public String getDescription() {
110 return getFunctionName().getDisplayName();
111 }
112
113 /**
114 * Get the arity of the function
115 *
116 * @return the number of arguments in the function signature
117 */
118 public int getArity() {
119 return 1;
120 }
121
122 /**
123 * Invoke the function
124 *
125 * @param context the XPath dynamic evaluation context
126 * @param args the actual arguments to be supplied
127 * @return the result of invoking the function
128 * @throws net.sf.saxon.trans.XPathException
129 * if a dynamic error occurs within the function
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