/** * Get the data type of the items returned * * @param th the type hierarchy cache * @return an integer representing the data type */ public ItemType getItemType(TypeHierarchy th) { // special case the filter [. instance of x] if (filter instanceof InstanceOfExpression && ((InstanceOfExpression)filter).getBaseExpression() instanceof ContextItemExpression) { return ((InstanceOfExpression)filter).getRequiredItemType(); } return start.getItemType(th); }
private Expression makeSequenceTypeExpression(Expression lhs, int operator, /*@NotNull*/ SequenceType type) { switch (operator) { case Token.INSTANCE_OF: return new InstanceOfExpression(lhs, type); case Token.TREAT_AS: return TreatExpression.make(lhs, type); default: throw new IllegalArgumentException(); } }
/** * Evaluate the expression */ public Item evaluateItem(XPathContext context) throws XPathException { return BooleanValue.get(effectiveBooleanValue(context)); }
/** * Evaluate the expression as a boolean */ public boolean effectiveBooleanValue(XPathContext context) throws XPathException { SequenceIterator iter = getBaseExpression().iterate(context); return isInstance(iter, context); }
/** * Copy an expression. This makes a deep copy. * * @return the copy of the original expression */ public Expression copy() { return new InstanceOfExpression(getBaseExpression().copy(), SequenceType.makeSequenceType(targetType, targetCardinality)); }
private AbstractExpression exprFor (InstanceOfExpression expr) { ItemType type = expr.getRequiredItemType(); String typeExpr; if (type.isPlainType()) { typeExpr = type.toString(); } else if (type instanceof NodeTest) { lux.xpath.NodeTest nodeTest = nodeTestFor((NodeTest)type); typeExpr = nodeTest.toString(); } else { throw new LuxException ("Unsupported node test in instance-of expression: " + expr.toString()); } return new InstanceOf(typeExpr, exprFor (expr.getBaseExpression())); }
/** * Produce a short string identifying the expression for use in error messages * * @return a short string, sufficient to identify the expression */ @Override public String toShortString() { String occ = Cardinality.getOccurrenceIndicator(targetCardinality); return getBaseExpression().toShortString() + " instance of " + targetType.toString() + occ; }
getOperand().typeCheck(visitor, contextInfo); Expression operand = getBaseExpression(); if (operand instanceof Literal) { Literal lit = Literal.makeLiteral( evaluateItem(visitor.getStaticContext().makeEarlyEvaluationContext()), this); ExpressionTool.copyLocationInfo(this, lit); return lit;
/** * Get a UType indicating which kinds of items this Pattern can match. * * @return a UType indicating all the primitive types of item that the pattern can match. */ @Override public UType getUType() { if (getPredicate() instanceof InstanceOfExpression) { return ((InstanceOfExpression) getPredicate()).getRequiredItemType().getUType(); } else { return UType.ANY; } }
/** * Type-check the expression * @return the checked expression */ public Expression typeCheck(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException { operand = visitor.typeCheck(operand, contextItemType); if (operand instanceof Literal) { return Literal.makeLiteral( (AtomicValue)evaluateItem(visitor.getStaticContext().makeEarlyEvaluationContext())); } // See if we can get the answer by static analysis. if (Cardinality.subsumes(targetCardinality, operand.getCardinality())) { final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); int relation = th.relationship(operand.getItemType(th), targetType); if (relation == TypeHierarchy.SAME_TYPE || relation == TypeHierarchy.SUBSUMED_BY) { return Literal.makeLiteral(BooleanValue.TRUE); } else if (relation == TypeHierarchy.DISJOINT) { // if the item types are disjoint, the result might still be true if both sequences are empty if (!Cardinality.allowsZero(targetCardinality) || !Cardinality.allowsZero(operand.getCardinality())) { return Literal.makeLiteral(BooleanValue.FALSE); } } } return this; }
/** * Copy an expression. This makes a deep copy. * * @return the copy of the original expression */ public Expression copy() { return new InstanceOfExpression(getBaseExpression().copy(), SequenceType.makeSequenceType(targetType, targetCardinality)); }
/** * The toString() method for an expression attempts to give a representation of the expression * in an XPath-like form, but there is no guarantee that the syntax will actually be true XPath. * In the case of XSLT instructions, the toString() method gives an abstracted view of the syntax */ @Override public String toString() { String occ = Cardinality.getOccurrenceIndicator(targetCardinality); return "(" + getBaseExpression().toString() + " instance of " + targetType.toString() + occ + ")"; }
getOperand().typeCheck(visitor, contextInfo); Expression operand = getBaseExpression(); if (operand instanceof Literal) { Literal lit = Literal.makeLiteral( evaluateItem(visitor.getStaticContext().makeEarlyEvaluationContext()), this); ExpressionTool.copyLocationInfo(this, lit); return lit;
/** * Evaluate the expression as a boolean */ public boolean effectiveBooleanValue(XPathContext context) throws XPathException { SequenceIterator iter = getBaseExpression().iterate(context); return isInstance(iter, context); }
/** * Get a UType indicating which kinds of items this Pattern can match. * * @return a UType indicating all the primitive types of item that the pattern can match. */ @Override public UType getUType() { if (getPredicate() instanceof InstanceOfExpression) { return ((InstanceOfExpression) getPredicate()).getRequiredItemType().getUType(); } else { return UType.ANY; } }
/** * Type-check the expression * @return the checked expression */ public Expression typeCheck(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException { operand = visitor.typeCheck(operand, contextItemType); if (operand instanceof Literal) { return Literal.makeLiteral( (AtomicValue)evaluateItem(visitor.getStaticContext().makeEarlyEvaluationContext())); } // See if we can get the answer by static analysis. if (Cardinality.subsumes(targetCardinality, operand.getCardinality())) { final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); int relation = th.relationship(operand.getItemType(th), targetType); if (relation == TypeHierarchy.SAME_TYPE || relation == TypeHierarchy.SUBSUMED_BY) { return Literal.makeLiteral(BooleanValue.TRUE); } else if (relation == TypeHierarchy.DISJOINT) { // if the item types are disjoint, the result might still be true if both sequences are empty if (!Cardinality.allowsZero(targetCardinality) || !Cardinality.allowsZero(operand.getCardinality())) { return Literal.makeLiteral(BooleanValue.FALSE); } } } return this; }
/** * Get the data type of the items returned * * @param th the type hierarchy cache * @return an integer representing the data type */ public ItemType getItemType(TypeHierarchy th) { // special case the filter [. instance of x] if (filter instanceof InstanceOfExpression && ((InstanceOfExpression)filter).getBaseExpression() instanceof ContextItemExpression) { return ((InstanceOfExpression)filter).getRequiredItemType(); } return start.getItemType(th); }
private Expression makeSequenceTypeExpression(Expression lhs, int operator, /*@NotNull*/ SequenceType type) { switch (operator) { case Token.INSTANCE_OF: return new InstanceOfExpression(lhs, type); case Token.TREAT_AS: return TreatExpression.make(lhs, type); default: throw new IllegalArgumentException(); } }
/** * Copy an expression. This makes a deep copy. * * @return the copy of the original expression * @param rebindings variable references that need to be rebound */ /*@NotNull*/ public Expression copy(RebindingMap rebindings) { InstanceOfExpression exp = new InstanceOfExpression(getBaseExpression().copy(rebindings), SequenceType.makeSequenceType(targetType, targetCardinality)); ExpressionTool.copyLocationInfo(this, exp); return exp; }
/** * Produce a short string identifying the expression for use in error messages * * @return a short string, sufficient to identify the expression */ @Override public String toShortString() { String occ = Cardinality.getOccurrenceIndicator(targetCardinality); return getBaseExpression().toShortString() + " instance of " + targetType.toString() + occ; }