1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.Item; 3 import net.sf.saxon.om.NamePool; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.style.StandardNames; 6 import net.sf.saxon.trans.DynamicError; 7 import net.sf.saxon.trans.XPathException; 8 import net.sf.saxon.type.ItemType; 9 import net.sf.saxon.type.Type; 10 import net.sf.saxon.value.*; 11 12 15 16 public final class NumericPromoter extends UnaryExpression implements MappingFunction { 17 18 private int requiredType; 20 27 28 public NumericPromoter(Expression sequence, int requiredType) { 29 super(sequence); 30 this.requiredType = requiredType; 31 ExpressionTool.copyLocationInfo(sequence, this); 32 } 33 34 37 38 public Expression simplify(StaticContext env) throws XPathException { 39 operand = operand.simplify(env); 40 if (operand instanceof AtomicValue) { 41 return promote(((AtomicValue)operand), null); 42 } else if (operand instanceof Value) { 43 return SequenceExtent.makeSequenceExtent(iterate(null)).reduce(); 44 } 45 return this; 46 } 47 48 51 52 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 53 operand = operand.typeCheck(env, contextItemType); 54 return this; 55 } 56 57 60 61 public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException { 62 operand = operand.optimize(opt, env, contextItemType); 63 return this; 64 } 65 66 69 70 public SequenceIterator iterate(XPathContext context) throws XPathException { 71 SequenceIterator base = operand.iterate(context); 72 return new MappingIterator(base, this, null); 73 } 74 75 78 79 public Item evaluateItem(XPathContext context) throws XPathException { 80 Item item = operand.evaluateItem(context); 81 if (item==null) return null; 82 return promote(((AtomicValue)item), context); 83 } 84 85 88 89 public Object map(Item item, XPathContext context) throws XPathException { 90 return promote(((AtomicValue)item), context); 91 } 92 93 96 97 private AtomicValue promote(AtomicValue value, XPathContext context) throws XPathException { 98 AtomicValue v = value.getPrimitiveValue(); 99 if (!(v instanceof NumericValue || v instanceof UntypedAtomicValue)) { 100 DynamicError err = new DynamicError("Cannot promote non-numeric value to " + getItemType().toString()); 101 err.setLocator(this); 102 err.setXPathContext(context); 103 throw err; 104 } 105 return v.convert(requiredType, context); 106 } 107 108 113 114 public ItemType getItemType() { 115 if (requiredType == StandardNames.XS_DOUBLE) { 116 return Type.DOUBLE_TYPE; 117 } else { 118 return Type.FLOAT_TYPE; 119 } 120 } 121 122 125 126 public boolean equals(Object other) { 127 return super.equals(other) && 128 requiredType == ((NumericPromoter)other).requiredType; 129 } 130 131 135 136 protected String displayOperator(NamePool pool) { 137 return "promote items to " + getItemType().toString(pool); 138 } 139 140 } 141 142 143 144 | Popular Tags |