1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.StaticContext; 4 import net.sf.saxon.expr.TailExpression; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.om.Item; 7 import net.sf.saxon.om.SequenceIterator; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.type.ItemType; 10 import net.sf.saxon.value.AtomicValue; 11 import net.sf.saxon.value.IntegerValue; 12 import net.sf.saxon.value.NumericValue; 13 14 17 18 19 public class Remove extends SystemFunction { 20 21 24 25 public Expression simplify(StaticContext env) throws XPathException { 26 Expression exp = super.simplify(env); 27 if (exp instanceof Remove) { 28 return ((Remove)exp).simplifyAsTailExpression(); 29 } else { 30 return exp; 31 } 32 } 33 34 39 40 private Expression simplifyAsTailExpression() { 41 if (argument[1] instanceof IntegerValue && 42 ((IntegerValue)argument[1]).longValue() == 1) { 43 return new TailExpression(argument[0], 2); 44 } else { 45 return this; 46 } 47 } 48 49 53 54 public ItemType getItemType() { 55 return argument[0].getItemType(); 56 } 57 58 61 62 public SequenceIterator iterate(XPathContext context) throws XPathException { 63 SequenceIterator seq = argument[0].iterate(context); 64 AtomicValue n0 = (AtomicValue)argument[1].evaluateItem(context); 65 NumericValue n = (NumericValue)n0.getPrimitiveValue(); 66 int pos = (int)n.longValue(); 67 if (pos < 1) { 68 return seq; 69 } 70 return new RemoveIterator(seq, pos); 71 } 72 73 private class RemoveIterator implements SequenceIterator { 74 75 SequenceIterator base; 76 int removePosition; 77 int position = 0; 78 Item current = null; 79 80 public RemoveIterator(SequenceIterator base, int removePosition) { 81 this.base = base; 82 this.removePosition = removePosition; 83 } 84 85 public Item next() throws XPathException { 86 current = base.next(); 87 if (current != null && base.position() == removePosition) { 88 current = base.next(); 89 } 90 if (current == null) { 91 position = -1; 92 } else { 93 position++; 94 } 95 return current; 96 } 97 98 public Item current() { 99 return current; 100 } 101 102 public int position() { 103 return position; 104 } 105 106 public SequenceIterator getAnother() throws XPathException { 107 return new RemoveIterator( base.getAnother(), 108 removePosition); 109 } 110 111 120 121 public int getProperties() { 122 return 0; 123 } 124 } 125 126 } 127 128 | Popular Tags |