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.trans.XPathException; 6 import net.sf.saxon.type.AtomicType; 7 import net.sf.saxon.type.ItemType; 8 import net.sf.saxon.type.Type; 9 import net.sf.saxon.value.AtomicValue; 10 import net.sf.saxon.value.Cardinality; 11 import net.sf.saxon.value.SequenceExtent; 12 import net.sf.saxon.value.Value; 13 14 18 19 public final class AtomicSequenceConverter extends UnaryExpression implements MappingFunction { 20 21 private AtomicType reqItemType; 22 private int requiredPrimitiveType; 23 24 31 32 public AtomicSequenceConverter(Expression sequence, AtomicType requiredItemType) { 33 super(sequence); 34 this.reqItemType = requiredItemType; 35 this.requiredPrimitiveType = requiredItemType.getPrimitiveType(); 36 ExpressionTool.copyLocationInfo(sequence, this); 37 } 38 39 42 43 public Expression simplify(StaticContext env) throws XPathException { 44 operand = operand.simplify(env); 45 if (operand instanceof Value) { 46 return new SequenceExtent(iterate(null)); 47 } 48 return this; 49 } 50 51 54 55 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 56 operand = operand.typeCheck(env, contextItemType); 57 if (Type.isSubType(operand.getItemType(), reqItemType)) { 58 return operand; 59 } else if (!Cardinality.allowsMany(operand.getCardinality())) { 60 CastExpression cast = new CastExpression(operand, reqItemType, 61 (operand.getCardinality() & StaticProperty.ALLOWS_ZERO) != 0); 62 ExpressionTool.copyLocationInfo(this, cast); 63 cast.setParentExpression(getParentExpression()); 64 return cast; 65 } else { 66 return this; 67 } 68 } 69 70 74 75 public int computeSpecialProperties() { 76 int p = super.computeSpecialProperties(); 77 return p | StaticProperty.NON_CREATIVE; 78 } 79 80 83 84 public SequenceIterator iterate(XPathContext context) throws XPathException { 85 SequenceIterator base = operand.iterate(context); 86 return new MappingIterator(base, this, null); 87 } 88 89 92 93 public Item evaluateItem(XPathContext context) throws XPathException { 94 Item item = operand.evaluateItem(context); 95 if (item==null) return null; 96 return ((AtomicValue)item).convert(requiredPrimitiveType, context); 97 } 98 99 102 103 public Object map(Item item, XPathContext context) throws XPathException { 104 return ((AtomicValue)item).convert(requiredPrimitiveType, context); 105 } 106 107 112 113 public ItemType getItemType() { 114 return reqItemType; 115 } 116 117 120 121 public int computeCardinality() { 122 return operand.getCardinality(); 123 } 124 125 128 129 public boolean equals(Object other) { 130 return super.equals(other) && 131 requiredPrimitiveType == ((AtomicSequenceConverter)other).requiredPrimitiveType; 132 } 133 134 138 139 protected String displayOperator(NamePool pool) { 140 return "convert items to " + reqItemType.toString(pool); 141 } 142 143 } 144 145 146 147 | Popular Tags |