| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 22 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 66 |
} else if (requiredItemType.getUType().subsumes(UType.STRING) && BuiltInAtomicType.ANY_URI.matches(item, th)) { |
| 67 |
|
| 68 |
} else { |
| 69 |
String message = role.composeErrorMessage(requiredItemType, item, th); |
| 70 |
|
| 71 |
String errorCode = role.getErrorCode(); |
| 72 |
if ("XPDY0050".equals(errorCode)) { |
| 73 |
|
| 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 |
|