1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.xpath.ExprEnvironment; 32 import com.caucho.xpath.XPathException; 33 34 import org.w3c.dom.Node ; 35 36 39 public class UniqueIterator extends NodeIterator { 40 private NodeIterator _baseIterator; 41 42 private Node _node; 43 44 private Node []_oldNodes; 45 private int _top; 46 47 50 public UniqueIterator(ExprEnvironment env) 51 { 52 super(env); 53 } 54 55 58 public UniqueIterator(ExprEnvironment env, NodeIterator baseIterator) 59 throws XPathException 60 { 61 super(env); 62 63 _baseIterator = baseIterator; 64 _node = baseIterator.nextNode(); 65 _oldNodes = new Node [32]; 66 } 67 68 71 public boolean hasNext() 72 { 73 return _node != null; 74 } 75 76 79 public Node nextNode() 80 throws XPathException 81 { 82 Node next = _node; 83 84 if (next == null) 85 return null; 86 87 if (_top == _oldNodes.length) { 88 Node []newNodes = new Node [_oldNodes.length * 2]; 89 System.arraycopy(_oldNodes, 0, newNodes, 0, _oldNodes.length); 90 _oldNodes = newNodes; 91 } 92 93 _oldNodes[_top++] = next; 94 95 while (true) { 96 _node = _baseIterator.nextNode(); 97 98 if (_node == null) 99 return next; 100 101 int i; 102 for (i = _top - 1; i >= 0; i--) 103 if (_oldNodes[i] == _node) 104 break; 105 106 if (i < 0) 107 break; 108 } 109 110 return next; 111 } 112 113 116 public Object clone() 117 { 118 UniqueIterator clone = new UniqueIterator(_env); 119 120 clone._node = _node; 121 clone._oldNodes = new Node [_oldNodes.length]; 122 System.arraycopy(_oldNodes, 0, clone._oldNodes, 0, _oldNodes.length); 123 clone._top = _top; 124 125 return clone; 126 } 127 } 128 129 | Popular Tags |