Debian Codegraph @

net/sf/saxon/expr/ItemTypeCheckingFunction.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.Configuration;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.expr.parser.RoleDiagnostic;
13 import net.sf.saxon.om.Item;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.BuiltInAtomicType;
16 import net.sf.saxon.type.ItemType;
17 import net.sf.saxon.type.TypeHierarchy;
18 import net.sf.saxon.type.UType;
19
20 /**
21 * A mapping function for use in conjunction with an {@link ItemMappingIterator} that checks that
22 * all the items in a sequence are instances of a given item type
23 *
24 */
25 public class ItemTypeCheckingFunction<T extends Item<?>> implements ItemMappingFunction<T, T> {
26
27 private ItemType requiredItemType;
28 private RoleDiagnostic role;
29 private Location location;
30 private Expression failingExpression;
31 private Configuration config = null;
32
33 /**
34 * Create the type-checking function
35 *
36 * @param requiredItemType the item type that all items in the sequence must conform to
37 * @param role information for error messages
38 * @param failingExpression the location of the expression for error messages
39 * @param config the Saxon configuration
40 */
41
42 public ItemTypeCheckingFunction(ItemType requiredItemType, RoleDiagnostic role, Expression failingExpression, Configuration config) {
43 this.requiredItemType = requiredItemType;
44 this.role = role;
45 this.failingExpression = failingExpression;
46 this.location = failingExpression.getLocation();
47 this.config = config;
48 }
49
50 public ItemTypeCheckingFunction(ItemType requiredItemType, RoleDiagnostic role, Location locator, Configuration config) {
51 this.requiredItemType = requiredItemType;
52 this.role = role;
53 this.location = locator;
54 this.config = config;
55 }
56
57 public T mapItem(T item) throws XPathException {
58 testConformance(item, config);
59 return item;
60 }
61
62 private void testConformance(T item, Configuration config) throws XPathException {
63 final TypeHierarchy th = config.getTypeHierarchy();
64 if (requiredItemType.matches(item, th)) {
65 // OK, no action
66 } else if (requiredItemType.getUType().subsumes(UType.STRING) && BuiltInAtomicType.ANY_URI.matches(item, th)) {
67 // OK, no action
68 } else {
69 String message = role.composeErrorMessage(requiredItemType, item, th);
70
71 String errorCode = role.getErrorCode();
72 if ("XPDY0050".equals(errorCode)) {
73 // error in "treat as" assertion
74 XPathException te = new XPathException(message, errorCode);
75 te.setFailingExpression(failingExpression);
76 te.setLocator(location);
77 te.setIsTypeError(false);
78 throw te;
79 } else {
80 XPathException te = new XPathException(message, errorCode);
81 te.setFailingExpression(failingExpression);
82 te.setLocator(location);
83 te.setIsTypeError(true);
84 throw te;
85 }
86 }
87 }
88
89 }
90