public Expression optimize(ExpressionVisitor visitor, ItemType contextItemType) throws XPathException { boolean allAtomic = true; if (children != null) { for (int c=0; c<children.length; c++) { children[c] = visitor.optimize(children[c], contextItemType); adoptChildExpression(children[c]); if (!Literal.isAtomic(children[c])) { allAtomic = false; } } if (allAtomic) { Item[] items = new Item[children.length]; for (int c=0; c<children.length; c++) { items[c] = (AtomicValue)((Literal)children[c]).getValue(); } return new Literal(new SequenceExtent(items)); } } return this; }
@Override protected Expression preEvaluate() { // If the value can be determined from knowledge of one operand, precompute the result if (Literal.isConstantBoolean(getLhsExpression(), false) || Literal.isConstantBoolean(getRhsExpression(), false)) { // A and false() => false() // false() and B => false() return Literal.makeLiteral(BooleanValue.FALSE, this); } else if (Literal.hasEffectiveBooleanValue(getLhsExpression(), true)) { // true() and B => B return forceToBoolean(getRhsExpression()); } else if (Literal.hasEffectiveBooleanValue(getRhsExpression(), true)) { // A and true() => A return forceToBoolean(getLhsExpression()); } else { return this; } }
private Expression preEvaluate(ExpressionVisitor visitor) throws XPathException { if (Literal.isAtomic(operand)) { return Literal.makeLiteral( BooleanValue.get(effectiveBooleanValue(visitor.getStaticContext().makeEarlyEvaluationContext()))); } if (Literal.isEmptySequence(operand)) { return new Literal(BooleanValue.get(allowEmpty)); } return this; }
/** * Optimize for constant argument values */ public Expression fixArguments(Expression... arguments) throws XPathException { // Check if any arguments are known to be empty, with a declared result for that case for (int i = 0; i < getArity(); i++) { if (Literal.isEmptySequence(arguments[i]) && resultIfEmpty(i) != null) { return Literal.makeLiteral(details.resultIfEmpty[i].materialize()); } } return null; }
/** * Compile time evaluation * @param visitor an expression visitor */ public Expression preEvaluate(ExpressionVisitor visitor) throws XPathException { String baseURI = visitor.getStaticContext().getBaseURI(); if (baseURI == null) { return Literal.makeEmptySequence(); } return Literal.makeLiteral(new AnyURIValue(baseURI)); }
!(((Literal) getCondition(i)).getValue() instanceof BooleanValue)) { final boolean b; try { b = ((Literal) getCondition(i)).getValue().effectiveBooleanValue(); } catch (XPathException err) { err.setLocation(getLocation()); throw err; setCondition(i, Literal.makeLiteral(BooleanValue.get(b), this)); !Literal.isConstantBoolean(getCondition(i), false) && !Literal.isConstantBoolean(getCondition(i), true)) { return Literal.makeEmptySequence();
Literal.isConstantBoolean(operand0, false) || Literal.isConstantBoolean(operand1, false))) { return new Literal(BooleanValue.FALSE); Literal.isConstantBoolean(operand0, true) || Literal.isConstantBoolean(operand1, true))) { return new Literal(BooleanValue.TRUE); th.isSubType(operand1.getItemType(th), BuiltInAtomicType.BOOLEAN) && !visitor.isLoopingSubexpression(null)) { Expression cond = Choose.makeConditional(operand0, operand1, Literal.makeLiteral(BooleanValue.FALSE)); ExpressionTool.copyLocationInfo(this, cond); return cond;
/** * Get the value represented by this Literal * * @return the constant value */ @Override public StringValue getValue() { return (StringValue)super.getValue(); }
private Expression makeUnaryExpression(int operator, Expression operand) { if (Literal.isAtomic(operand)) { // very early evaluation of expressions like "-1", so they are treated as numeric literals AtomicValue val = (AtomicValue) ((Literal) operand).getValue(); if (val instanceof NumericValue) { if (env.isInBackwardsCompatibleMode()) { val = new DoubleValue(((NumericValue) val).getDoubleValue()); } AtomicValue value = operator == Token.NEGATE ? ((NumericValue) val).negate() : (NumericValue) val; return Literal.makeLiteral(value); } } return env.getConfiguration().getTypeChecker(env.isInBackwardsCompatibleMode()).makeArithmeticExpression( Literal.makeLiteral(Int64Value.ZERO), operator, operand); }
@Override public Expression makeFunctionCall(Expression[] arguments) { if (Literal.isAtomic(arguments[1])) { Sequence<?> index = ((Literal) arguments[1]).getValue(); if (index instanceof IntegerValue) { try { long value = ((IntegerValue) index).longValue(); if (value <= 0) { return arguments[0]; } else if (value == 1) { return new TailExpression(arguments[0], 2); } } catch (XPathException err) { // } } } return super.makeFunctionCall(arguments); }
/** * Copy an expression. This makes a deep copy. * * @return the copy of the original expression */ public Expression copy() { return new Literal(value); }
/** * Default constructor used by subclasses */ public SimpleNodeConstructor() { Expression select = Literal.makeEmptySequence(); selectOp = new Operand(this, select, OperandRole.SINGLE_ATOMIC); }
/** * Determine the cardinality of the function. */ public int computeCardinality() { if (getNumberOfArguments() == 3 && Literal.isConstantOne(argument[2])) { return StaticProperty.ALLOWS_ZERO_OR_ONE; } return argument[0].getCardinality() | StaticProperty.ALLOWS_ZERO; }
private void flatten(List list) { for (int i=0; i<children.length; i++) { if (children[i] instanceof Block) { ((Block)children[i]).flatten(list); } else if (Literal.isEmptySequence(children[i])) { // no-op } else { list.add(children[i]); } } }
getLhs().typeCheck(visitor, contextInfo); getBase().setFiltered(true); if (Literal.isEmptySequence(getBase())) { return getBase(); if (Literal.isConstantOne(getFilter())) { Expression fie = FirstItemExpression.makeFirstItemExpression(getBase()); ExpressionTool.copyLocationInfo(this, fie);
if (Literal.isConstantBoolean(filter, true)) { } else if (Literal.isConstantBoolean(filter, false)) { if (Literal.isConstantOne(filters[0])) { firstElementPattern = true; specialFilter = true; ComparisonExpression comp = (ComparisonExpression)filters[0]; if (comp.getSingletonOperator() == Token.FEQ && (comp.getOperands()[0] instanceof Position && Literal.isConstantOne(comp.getOperands()[1])) || (comp.getOperands()[1] instanceof Position && Literal.isConstantOne(comp.getOperands()[0]))) { firstElementPattern = true; specialFilter = true;
/** * Get the two operands of the comparison * @return the two operands */ public Expression[] getOperands() { return new Expression[] {operand, Literal.makeLiteral(Int64Value.makeIntegerValue(comparand))}; }
/** * Compute the cardinality of the sequence returned by evaluating this instruction * @return the static cardinality */ public int computeCardinality() { int card = 0; boolean includesTrue = false; for (int i=0; i<actions.length; i++) { card = Cardinality.union(card, actions[i].getCardinality()); if (Literal.isConstantBoolean(conditions[i], true)) { includesTrue = true; } } if (!includesTrue) { // we may drop off the end and return an empty sequence (typical for xsl:if) card = Cardinality.union(card, StaticProperty.ALLOWS_ZERO); } return card; }
/** * Export information about this function item to the export() or explain() output */ default void export(ExpressionPresenter out) throws XPathException { out.startElement("map"); out.emitAttribute("size", "" + size()); for (KeyValuePair kvp : keyValuePairs()) { Literal.exportAtomicValue(kvp.key, out); Literal.exportValue(kvp.value, out); } out.endElement(); }
public CharSequence evaluateAsString(XPathContext context) throws XPathException { AtomicValue value = (AtomicValue) evaluateItem(context); if (value == null) return ""; return value.getStringValueCS(); }