/** * Make a treat expression * @return the expression */ public static Expression make(Expression sequence, SequenceType type) { RoleLocator role = new RoleLocator(RoleLocator.TYPE_OP, "treat as", 0, null); role.setErrorCode("XPDY0050"); Expression e = CardinalityChecker.makeCardinalityChecker(sequence, type.getCardinality(), role); ItemChecker checker = new ItemChecker(e, type.getPrimaryType(), role); return checker; }
/** * End of event stream */ public void close() throws XPathException { if (count == 0 && !Cardinality.allowsZero(cardinality)) { XPathException err = new XPathException("An empty sequence is not allowed as the " + role.getMessage()); String errorCode = role.getErrorCode(); err.setErrorCode(errorCode); if (!"XPDY0050".equals(errorCode)) { err.setIsTypeError(true); } throw err; } // don't pass on the close event }
return ordinal(operand+1) + " argument of " + name + "()"; case BINARY_EXPR: return ordinal(operand+1) + " operand of '" + name + '\''; case UNARY_EXPR: return "operand of '-'"; return "result of template " + name; case ORDER_BY: return ordinal(operand+1) + " sort key"; case PARAM: return "value of parameter $" + name; case UPDATING_EXPR: return "value of " + ordinal(operand+1) + " operand of " + name + " expression"; default: return "";
/** * Check the select expression against the required type. * @param visitor an expression visitor * @throws XPathException */ private void checkAgainstRequiredType(ExpressionVisitor visitor) throws XPathException { // Note, in some cases we are doing this twice. RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, variableQName, 0, null); role.setSourceLocator(this); SequenceType r = requiredType; if (r != null && select != null) { // check that the expression is consistent with the required type select = TypeChecker.staticTypeCheck(select, requiredType, false, role, visitor); } }
/** * Perform static type checking on an argument to a function call, and add * type conversion logic where necessary. * @param arg argument number, zero-based * @param visitor an expression visitor */ private void checkArgument(int arg, ExpressionVisitor visitor) throws XPathException { RoleLocator role = new RoleLocator(RoleLocator.FUNCTION, getFunctionName(), arg, visitor.getConfiguration().getNamePool()); role.setSourceLocator(this); role.setErrorCode(getErrorCodeForTypeErrors()); argument[arg] = TypeChecker.staticTypeCheck( argument[arg], getRequiredType(arg), visitor.getStaticContext().isInBackwardsCompatibleMode(), role, visitor); }
private void testConformance(Item item, XPathContext context) throws XPathException { if (!requiredItemType.matchesItem(item, true, (context == null ? null : context.getConfiguration()))) { String message; if (context == null) { // no name pool available message = "Supplied value of type " + Type.displayTypeName(item) + " does not match the required type of " + role.getMessage(); } else { final NamePool pool = context.getNamePool(); final TypeHierarchy th = context.getConfiguration().getTypeHierarchy(); message = role.composeErrorMessage(requiredItemType, Value.asValue(item).getItemType(th), pool); } String errorCode = role.getErrorCode(); if ("XPDY0050".equals(errorCode)) { // error in "treat as" assertion dynamicError(message, errorCode, context); } else { typeError(message, errorCode, context); } } }
private void checkItemType(ItemType type, long locationId) throws XPathException { if (!getConfiguration().getTypeHierarchy().isSubType(type, itemType)) { String message = role.composeErrorMessage(itemType, type, getNamePool()); String errorCode = role.getErrorCode(); XPathException err = new XPathException(message); err.setErrorCode(errorCode); if (!"XPDY0050".equals(errorCode)) { err.setIsTypeError(true); } err.setLocator(ExpressionLocation.getSourceLocator(locationId, getPipelineConfiguration().getLocationProvider())); throw err; } }
/** * Check the select expression against the required type. * @param visitor an expression visitor * @throws XPathException */ private void checkAgainstRequiredType(ExpressionVisitor visitor) throws XPathException { // Note, in some cases we are doing this twice. RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, variableQName, 0); //role.setSourceLocator(this); SequenceType r = requiredType; if (r != null && select != null) { // check that the expression is consistent with the required type select = TypeChecker.staticTypeCheck(select, requiredType, false, role, visitor); } }
XPathException err = new XPathException("An empty sequence is not allowed as the " + role.getMessage(), supplied); err.setErrorCode(role.getErrorCode()); err.setIsTypeError(true); err.setLocator(role.getSourceLocator()); throw err; Cardinality.allowsZero(reqCard)) { if (suppliedCard != StaticProperty.EMPTY) { String msg = "Required item type of " + role.getMessage() + " is " + reqItemType.toString(env.getNamePool()) + "; supplied value has item type " + XPathException err = new XPathException("Required item type of " + role.getMessage() + " is " + reqItemType.toString(env.getNamePool()) + "; supplied value has item type " + suppliedItemType.toString(env.getNamePool()), supplied); err.setErrorCode(role.getErrorCode()); err.setIsTypeError(true); err.setLocator(role.getSourceLocator()); throw err; XPathException err = new XPathException("Required cardinality of " + role.getMessage() + " is " + Cardinality.toString(reqCard) + "; supplied value has cardinality " + Cardinality.toString(suppliedCard), supplied); err.setIsTypeError(true); err.setErrorCode(role.getErrorCode()); err.setLocator(role.getSourceLocator()); throw err;
/** * Construct the part of the message giving the required item type * @param requiredItemType the item type required by the context of a particular expression * @param pool the name pool * @return a message of the form "Required item type of X is Y" */ public String composeRequiredMessage(ItemType requiredItemType, NamePool pool) { return "Required item type of " + getMessage() + " is " + requiredItemType.toString(pool); }
/** * Set the error code to be returned (this is used when evaluating the functions such * as exactly-one() which have their own error codes) * @param code the error code to be used */ public void setErrorCode(String code) { role.setErrorCode(code); }
private void typeError(String message, String errorCode) throws XPathException { XPathException e = new XPathException(message, role.getSourceLocator()); e.setIsTypeError(true); e.setErrorCode(errorCode); throw e; }
/** * Perform static type checking on an argument to a function call, and add * type conversion logic where necessary. * @param arg argument number, zero-based * @param visitor an expression visitor */ private void checkArgument(int arg, ExpressionVisitor visitor) throws XPathException { RoleLocator role = new RoleLocator(RoleLocator.FUNCTION, getFunctionName(), arg, visitor.getConfiguration().getNamePool()); role.setSourceLocator(this); role.setErrorCode(getErrorCodeForTypeErrors()); argument[arg] = TypeChecker.staticTypeCheck( argument[arg], getRequiredType(arg), visitor.getStaticContext().isInBackwardsCompatibleMode(), role, visitor); }
public Expression typeCheck(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException { // always called from ArithmeticExpression.typeCheck, so the operand has already been checked. // Now need to ensure that it's numeric Expression oldop = operand; RoleLocator role = new RoleLocator(RoleLocator.UNARY_EXPR, "-", 0, null); role.setSourceLocator(this); operand = TypeChecker.staticTypeCheck(operand, SequenceType.OPTIONAL_NUMERIC, backwardsCompatible, role, visitor); if (operand != oldop) { adoptChildExpression(operand); } return this; }
private void testConformance(Item item, XPathContext context) throws XPathException { if (!requiredItemType.matchesItem(item, true, (context == null ? null : context.getConfiguration()))) { String message; if (context == null) { // no name pool available message = "Supplied value of type " + Type.displayTypeName(item) + " does not match the required type of " + role.getMessage(); } else { final NamePool pool = context.getNamePool(); final TypeHierarchy th = context.getConfiguration().getTypeHierarchy(); message = role.composeErrorMessage(requiredItemType, Value.asValue(item).getItemType(th), pool); } String errorCode = role.getErrorCode(); if ("XPDY0050".equals(errorCode)) { // error in "treat as" assertion dynamicError(message, errorCode, context); } else { typeError(message, errorCode, context); } } }
private void checkItemType(ItemType type, long locationId) throws XPathException { if (!getConfiguration().getTypeHierarchy().isSubType(type, itemType)) { String message = role.composeErrorMessage(itemType, type, getNamePool()); String errorCode = role.getErrorCode(); XPathException err = new XPathException(message); err.setErrorCode(errorCode); if (!"XPDY0050".equals(errorCode)) { err.setIsTypeError(true); } err.setLocator(ExpressionLocation.getSourceLocator(locationId, getPipelineConfiguration().getLocationProvider())); throw err; } }
/** * Type-check the pattern. * Default implementation does nothing. This is only needed for patterns that contain * variable references or function calls. * @return the optimised Pattern */ public Pattern analyze(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException { idExpression = visitor.typeCheck(idExpression, contextItemType); RoleLocator role = new RoleLocator(RoleLocator.FUNCTION, "id", 1); idExpression = TypeChecker.staticTypeCheck(idExpression, SequenceType.ATOMIC_SEQUENCE, false, role, visitor); return this; }
XPathException err = new XPathException("An empty sequence is not allowed as the " + role.getMessage(), supplied); err.setErrorCode(role.getErrorCode()); err.setIsTypeError(true); throw err; Cardinality.allowsZero(reqCard)) { if (suppliedCard != StaticProperty.EMPTY) { String msg = "Required item type of " + role.getMessage() + " is " + reqItemType.toString(env.getNamePool()) + "; supplied value has item type " + XPathException err = new XPathException("Required item type of " + role.getMessage() + " is " + reqItemType.toString(env.getNamePool()) + "; supplied value has item type " + suppliedItemType.toString(env.getNamePool()), supplied); err.setErrorCode(role.getErrorCode()); err.setIsTypeError(true); err.setLocator(role.getSourceLocator()); throw err; XPathException err = new XPathException("Required item type of " + role.getMessage() + " is " + reqItemType.toString(env.getNamePool()) + "; supplied value has item type " + suppliedItemType.toString(env.getNamePool()), supplied); err.setErrorCode(role.getErrorCode()); err.setIsTypeError(true); err.setLocator(role.getSourceLocator()); throw err;
/** * Construct the part of the message giving the required item type */ public String composeRequiredMessage(ItemType requiredItemType, NamePool pool) { return "Required item type of " + getMessage() + " is " + requiredItemType.toString(pool); }
/** * Set the error code to be returned (this is used when evaluating the functions such * as exactly-one() which have their own error codes) * @param code the error code to be used */ public void setErrorCode(String code) { role.setErrorCode(code); }