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.pattern.NodeTest; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.type.AnyItemType; 8 import net.sf.saxon.type.AtomicType; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.type.Type; 11 import net.sf.saxon.value.*; 12 13 17 18 public final class UntypedAtomicConverter extends UnaryExpression implements MappingFunction { 19 20 private AtomicType requiredItemType; 21 private boolean allConverted; 22 23 32 33 public UntypedAtomicConverter(Expression sequence, AtomicType requiredItemType, boolean allConverted) { 34 super(sequence); 35 this.requiredItemType = requiredItemType; 36 this.allConverted = allConverted; 37 ExpressionTool.copyLocationInfo(sequence, this); 38 } 39 40 43 44 public ItemType getItemType() { 45 if (allConverted) { 46 return requiredItemType; 47 } else { 48 return Type.getCommonSuperType(requiredItemType, operand.getItemType()); 49 } 50 } 51 52 55 56 public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException { 57 operand = operand.typeCheck(env, contextItemType); 58 if (operand instanceof Value) { 59 return SequenceExtent.makeSequenceExtent(iterate(null)).simplify(env); 60 } 61 ItemType type = operand.getItemType(); 62 if (type instanceof NodeTest) { 63 return this; 64 } 65 if (type==Type.ANY_ATOMIC_TYPE || type instanceof AnyItemType || 66 type==Type.UNTYPED_ATOMIC_TYPE) { 67 return this; 68 } 69 return operand; 72 } 73 74 75 79 80 public int computeSpecialProperties() { 81 int p = super.computeSpecialProperties(); 82 return p | StaticProperty.NON_CREATIVE; 83 } 84 85 88 89 public SequenceIterator iterate(XPathContext context) throws XPathException { 90 SequenceIterator base = operand.iterate(context); 91 return new MappingIterator(base, this, context); 92 } 93 94 97 98 public Item evaluateItem(XPathContext context) throws XPathException { 99 Item item = operand.evaluateItem(context); 100 if (item==null) return null; 101 if (item instanceof UntypedAtomicValue) { 102 try { 103 AtomicValue val = ((UntypedAtomicValue)item).convert(requiredItemType, context, true); 104 if (val instanceof ValidationErrorValue) { 105 throw ((ValidationErrorValue)val).getException(); 106 } 107 return val; 108 } catch (XPathException e) { 109 if (e.getLocator() == null) { 110 e.setLocator(this); 111 } 112 throw e; 113 } 114 } else { 115 return item; 117 } 118 } 119 120 123 124 public Object map(Item item, XPathContext context) throws XPathException { 125 if (item instanceof UntypedAtomicValue) { 126 Value val = ((UntypedAtomicValue)item).convert(requiredItemType, context, true); 127 if (val instanceof ValidationErrorValue) { 128 throw ((ValidationErrorValue)val).getException(); 129 } 130 return val; 131 } else { 132 return item; 133 } 134 } 135 136 140 141 protected String displayOperator(NamePool pool) { 142 return "convert untyped atomic items to " + requiredItemType.toString(pool); 143 } 144 145 } 146 147 | Popular Tags |