Debian Codegraph @

net/sf/saxon/ma/arrays/ArrayItemType.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.ma.arrays;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.StaticProperty;
12 import net.sf.saxon.expr.parser.RoleDiagnostic;
13 import net.sf.saxon.om.Genre;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.GroundedValue;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.trans.Err;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.util.FastStringBuffer;
20 import net.sf.saxon.type.*;
21 import net.sf.saxon.value.Cardinality;
22 import net.sf.saxon.value.SequenceType;
23
24 import java.util.Optional;
25 import java.util.function.Function;
26
27 /**
28 * An instance of this class represents a specific array item type, for example
29 * function(xs:int) as xs:boolean
30 */
31 public class ArrayItemType extends AnyFunctionType {
32
33 public final static ArrayItemType ANY_ARRAY_TYPE = new ArrayItemType(SequenceType.ANY_SEQUENCE);
34
35 public final static SequenceType SINGLE_ARRAY =
36 SequenceType.makeSequenceType(ArrayItemType.ANY_ARRAY_TYPE, StaticProperty.EXACTLY_ONE);
37
38 private SequenceType memberType;
39
40 public ArrayItemType(SequenceType memberType) {
41 this.memberType = memberType;
42 }
43
44 /**
45 * Determine the Genre (top-level classification) of this type
46 *
47 * @return the Genre to which this type belongs, specifically {@link Genre#ARRAY}
48 */
49 @Override
50 public Genre getGenre() {
51 return Genre.ARRAY;
52 }
53
54 /**
55 * Get the type of the members of the array
56 * @return the type to which all members of the array must conform
57 */
58
59 public SequenceType getMemberType() {
60 return memberType;
61 }
62
63 /**
64 * Ask whether this function item type is a map type. In this case function coercion (to the map type)
65 * will never succeed.
66 *
67 * @return true if this FunctionItemType is a map type
68 */
69 @Override
70 public boolean isMapType() {
71 return false;
72 }
73
74 /**
75 * Ask whether this function item type is an array type. In this case function coercion (to the array type)
76 * will never succeed.
77 *
78 * @return true if this FunctionItemType is an array type
79 */
80 @Override
81 public boolean isArrayType() {
82 return true;
83 }
84
85 /**
86 * Get an alphabetic code representing the type, or at any rate, the nearest built-in type
87 * from which this type is derived. The codes are designed so that for any two built-in types
88 * A and B, alphaCode(A) is a prefix of alphaCode(B) if and only if A is a supertype of B.
89 *
90 * @return the alphacode for the nearest containing built-in type
91 */
92 @Override
93 public String getAlphaCode() {
94 return "FA";
95 }
96
97 /**
98 * Ask whether values of this type are atomizable
99 *
100 * @return true unless it is known that these items will be elements with element-only
101 * content, in which case return false
102 * @param th The type hierarchy cache
103 */
104 @Override
105 public boolean isAtomizable(TypeHierarchy th) {
106 return true;
107 }
108
109 /**
110 * Get the item type of the atomic values that will be produced when an item
111 * of this type is atomized
112 *
113 * @return the item type of the atomic values that will be produced when an item
114 * of this type is atomized
115 */
116 @Override
117 public PlainType getAtomizedItemType() {
118 return memberType.getPrimaryType().getAtomizedItemType();
119 }
120
121 /**
122 * Get the arity (number of arguments) of this function type
123 *
124 * @return the number of argument types in the function signature
125 */
126
127 public int getArity() {
128 return 1;
129 }
130
131 /**
132 * Get the argument types of this array, viewed as a function
133 *
134 * @return the list of argument types of this array, viewed as a function
135 */
136
137 public SequenceType[] getArgumentTypes() {
138 // regardless of the key type, a function call on this map can supply any atomic value
139 return new SequenceType[]{BuiltInAtomicType.INTEGER.one()};
140 }
141
142 /**
143 * Test whether a given item conforms to this type
144 *
145 * @param item The item to be tested
146 * @param th The type hierarchy cache
147 * @return true if the item is an instance of this type; false otherwise
148 */
149 @Override
150 public boolean matches(Item item, TypeHierarchy th) throws XPathException{
151 if (!(item instanceof ArrayItem)) {
152 return false;
153 }
154 if (this == ANY_ARRAY_TYPE) {
155 return true;
156 } else {
157 for (Sequence<?> s : ((ArrayItem) item).members()){
158 if (!memberType.matches(s, th)){
159 return false;
160 }
161 }
162 return true;
163 }
164 }
165
166 /**
167 * Get the result type of this array, viewed as a function
168 *
169 * @return the result type of this array, viewed as a function
170 */
171
172 public SequenceType getResultType() {
173 return memberType;
174 }
175
176 /**
177 * Produce a representation of this type name for use in error messages.
178 *
179 * @return a string representation of the type, in notation resembling but not necessarily
180 * identical to XPath syntax
181 */
182 public String toString() {
183 return makeString(SequenceType::toString);
184 }
185
186 private String makeString(Function<SequenceType, String> show) {
187 if (this.equals(ANY_ARRAY_TYPE)) {
188 return "array(*)";
189 } else {
190 FastStringBuffer sb = new FastStringBuffer(100);
191 sb.append("array(");
192 sb.append(show.apply(memberType));
193 sb.append(")");
194 return sb.toString();
195 }
196 }
197
198 /**
199 * Return a string representation of this ItemType suitable for use in stylesheet
200 * export files. This differs from the result of toString() in that it will not contain
201 * any references to anonymous types. Note that it may also use the Saxon extended syntax
202 * for union types and tuple types. The default implementation returns the result of
203 * calling {@code toString()}.
204 *
205 * @return the string representation as an instance of the XPath SequenceType construct
206 */
207 @Override
208 public String toExportString() {
209 return makeString(SequenceType::toExportString);
210 }
211
212 /**
213 * Test whether this array type equals another array type
214 */
215
216 public boolean equals(Object other) {
217 if (this == other) {
218 return true;
219 }
220 if (other instanceof ArrayItemType) {
221 ArrayItemType f2 = (ArrayItemType) other;
222 return memberType.equals(f2.memberType);
223 }
224 return false;
225 }
226
227 /**
228 * Returns a hash code value for the object.
229 */
230 @Override
231 public int hashCode() {
232 return memberType.hashCode();
233 }
234
235 /**
236 * Determine the relationship of one function item type to another
237 *
238 * @return for example {@link net.sf.saxon.type.TypeHierarchy#SUBSUMES}, {@link net.sf.saxon.type.TypeHierarchy#SAME_TYPE}
239 */
240
241 public int relationship(FunctionItemType other, TypeHierarchy th) {
242 if (other == AnyFunctionType.getInstance()) {
243 return TypeHierarchy.SUBSUMED_BY;
244 } else if (equals(other)) {
245 return TypeHierarchy.SAME_TYPE;
246 } else if (other == ArrayItemType.ANY_ARRAY_TYPE) {
247 return TypeHierarchy.SUBSUMED_BY;
248 } else if (other.isMapType()){
249 return TypeHierarchy.DISJOINT;
250 } else if (other instanceof ArrayItemType) {
251 // See bug 3720. Array types are never disjoint, because the empty array
252 // is an instance of every array type
253 ArrayItemType f2 = (ArrayItemType) other;
254 int rel = th.sequenceTypeRelationship(memberType, f2.memberType);
255 return rel==TypeHierarchy.DISJOINT ? TypeHierarchy.OVERLAPS : rel;
256 } else {
257 int rel = TypeHierarchy.DISJOINT;
258 //#ifdefined HOF
259 rel = new SpecificFunctionType(getArgumentTypes(), getResultType()).relationship(other, th);
260 if (rel == TypeHierarchy.SUBSUMES || rel == TypeHierarchy.SAME_TYPE) {
261 rel = TypeHierarchy.OVERLAPS;
262 }
263 //#endif
264 return rel;
265 }
266 }
267
268 @Override
269 public Expression makeFunctionSequenceCoercer(Expression exp, RoleDiagnostic role)
270 throws XPathException {
271 Expression result = exp;
272 //#ifdefined HOF ~
273 result = new SpecificFunctionType(getArgumentTypes(), getResultType()).makeFunctionSequenceCoercer(exp, role);
274 //#endif
275 return result;
276 }
277
278 /**
279 * Get extra diagnostic information about why a supplied item does not conform to this
280 * item type, if available. If extra information is returned, it should be in the form of a complete
281 * sentence, minus the closing full stop. No information should be returned for obvious cases.
282 *
283 * @param item the item that doesn't match this type
284 * @param th the type hierarchy cache
285 * @return optionally, a message explaining why the item does not match the type
286 */
287 @Override
288 public Optional<String> explainMismatch(Item item, TypeHierarchy th) {
289 if (item instanceof ArrayItem) {
290 for (int i=0; i<((ArrayItem)item).arrayLength(); i++) {
291 try {
292 GroundedValue<?> member = ((ArrayItem) item).get(i);
293 if (!memberType.matches(member, th)) {
294 String s = "The " + RoleDiagnostic.ordinal(i+1) +
295 " member of the supplied array {" +
296 Err.depictSequence(member) +
297 "} does not match the required member type " +
298 memberType;
299 Optional<String> more = memberType.explainMismatch(member, th);
300 if (more.isPresent()) {
301 s = s + ". " + more.get();
302 }
303 return Optional.of(s);
304 }
305 } catch (XPathException e) {
306 return Optional.empty();
307 }
308 }
309 }
310 return Optional.empty();
311 }
312
313 /**
314 * Generate Javascript code to test whether an item conforms to this item type
315 *
316 * @param knownToBe a type that this item is known to conform to
317 * @param targetVersion the version of Saxon-JS SEF format
318 * @return a Javascript instruction or sequence of instructions, which can be used as the body
319 * of a Javascript function, and which returns a boolean indication whether the value of the
320 * variable "item" is an instance of this item type.
321 * @throws XPathException if JS code cannot be generated for this item type, for example because
322 * the test is schema-aware.
323 */
324 @Override
325 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException {
326 if (this == ANY_ARRAY_TYPE) {
327 return "return SaxonJS.U.isArray(item)";
328 } else if (targetVersion == 1) {
329 FastStringBuffer fsb = new FastStringBuffer(256);
330 fsb.append("function v(item) {" + memberType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};");
331 fsb.append(Cardinality.generateJavaScriptChecker(memberType.getCardinality()));
332 fsb.append("return SaxonJS.U.isArray(item) && " +
333 "SaxonJS.U.ForArray(item.value).every(function(seq){return c(seq.length) && SaxonJS.U.ForArray(seq).every(v)});");
334 return fsb.toString();
335 } else {
336 // targetVersion == 2
337 FastStringBuffer fsb = new FastStringBuffer(256);
338 fsb.append("function v(item) {" + memberType.getPrimaryType().generateJavaScriptItemTypeTest(AnyItemType.getInstance(), targetVersion) + "};");
339 fsb.append(Cardinality.generateJavaScriptChecker(memberType.getCardinality()));
340 fsb.append("return SaxonJS.U.isConstrainedArray(item, v, c);");
341 return fsb.toString();
342
343 }
344 }
345 }
346
347 // Copyright (c) 2015-2016 Saxonica Limited.