1 package net.sf.saxon.expr; 2 import net.sf.saxon.om.AtomizableIterator; 3 import net.sf.saxon.om.Item; 4 import net.sf.saxon.om.SequenceIterator; 5 import net.sf.saxon.trans.XPathException; 6 7 19 20 public final class MappingIterator implements SequenceIterator, AtomizableIterator { 21 22 private SequenceIterator base; 23 private MappingFunction action; 24 private XPathContext context; 25 private SequenceIterator results = null; 27 private boolean atomizing = false; 28 private Item current = null; 29 private int position = 0; 30 31 39 40 public MappingIterator(SequenceIterator base, MappingFunction action, XPathContext context) { 41 this.base = base; 43 this.action = action; 44 this.context = context; 45 } 46 47 public Item next() throws XPathException { 48 Item nextItem; 49 while (true) { 50 if (results != null) { 51 nextItem = results.next(); 52 if (nextItem != null) { 53 break; 54 } else { 55 results = null; 56 } 57 } 58 Item nextSource = base.next(); 59 if (nextSource != null) { 60 Object obj = action.map(nextSource, context); 62 63 66 if (obj != null) { 67 if (obj instanceof Item) { 68 nextItem = (Item)obj; 69 results = null; 70 break; 71 } 72 results = (SequenceIterator)obj; 73 if (atomizing && results instanceof AtomizableIterator) { 74 ((AtomizableIterator)results).setIsAtomizing(atomizing); 75 } 76 nextItem = results.next(); 77 if (nextItem == null) { 78 results = null; 79 } else { 80 break; 81 } 82 } 83 } else { 85 results = null; 86 current = null; 87 position = -1; 88 return null; 89 } 90 } 91 92 current = nextItem; 93 position++; 95 return nextItem; 96 } 97 98 public Item current() { 99 return current; 100 } 101 102 public int position() { 103 return position; 104 } 105 106 107 public SequenceIterator getAnother() throws XPathException { 108 SequenceIterator newBase = base.getAnother(); 110 XPathContext c = context; 111 if (c!=null) { 112 c = c.newMinorContext(); 113 c.setCurrentIterator(newBase); 114 c.setOrigin(context.getOrigin()); 115 } 116 MappingIterator m = new MappingIterator(newBase, action, c); 117 m.setIsAtomizing(atomizing); 118 return m; 119 } 120 121 130 131 public int getProperties() { 132 return 0; 133 } 134 135 144 145 public void setIsAtomizing(boolean atomizing) { 146 this.atomizing = atomizing; 147 } 148 149 } 150 151 | Popular Tags |