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 import java.util.logging.Level ; 37 38 41 public class AxisIterator extends NodeIterator { 42 protected NodeIterator _parentIter; 43 protected AbstractPattern _axis; 44 protected Node _node; 45 protected Node _next; 46 protected Node _lastNode; 47 protected AbstractPattern _match; 48 49 protected AxisIterator(ExprEnvironment env) 50 { 51 super(env); 52 } 53 54 63 public AxisIterator(NodeIterator parentIter, AbstractPattern axis, 64 Node node, ExprEnvironment env, 65 AbstractPattern match) 66 throws XPathException 67 { 68 super(env); 69 70 _parentIter = parentIter; 71 _axis = axis; 72 _match = match; 73 74 if (parentIter != null && parentIter.hasNext()) { 75 node = parentIter.nextNode(); 76 } 77 78 if (node != null) { 79 _lastNode = axis.lastNode(node); 80 _node = findFirstMatchingNode(axis.firstNode(node, _env)); 81 } 82 83 _next = _node; 84 } 85 86 89 public boolean hasNext() 90 { 91 if (_next == null) { 92 try { 93 _next = nextNode(); 94 } catch (XPathException e) { 95 log.log(Level.FINE, e.toString(), e); 96 } 97 } 98 99 return _next != null; 100 } 101 102 105 public Node nextNode() 106 throws XPathException 107 { 108 if (_next != null) { 109 _node = _next; 110 _next = null; 111 112 return _node; 113 } 114 115 if (_node != null) 116 _node = findFirstMatchingNode(_axis.nextNode(_node, _lastNode)); 117 118 _next = null; 119 120 return _node; 121 } 122 123 126 private Node findFirstMatchingNode(Node node) 127 throws XPathException 128 { 129 while (true) { 130 if (node != null) { 131 if (_match == null || _match.match(node, _env)) { 132 _position++; 133 return node; 134 } 135 else { 136 node = _axis.nextNode(node, _lastNode); 137 continue; 138 } 139 } 140 141 else if (_parentIter == null || (node = _parentIter.nextNode()) == null) 142 return null; 143 else { 144 _position = 0; 145 _size = 0; 146 _lastNode = _axis.lastNode(node); 147 node = _axis.firstNode(node, _env); 148 } 149 } 150 } 151 152 155 public int getContextSize() 156 { 157 if (_size == 0) { 158 _size = _position; 159 160 try { 161 Node ptr = _node == null ? null : _axis.nextNode(_node, _lastNode); 162 163 for (; ptr != null; ptr = _axis.nextNode(ptr, _lastNode)) { 164 if (_match == null || _match.match(ptr, _env)) 165 _size++; 166 } 167 } catch (XPathException e) { 168 log.log(Level.FINE, e.toString(), e); 169 } 170 } 171 172 return _size; 173 } 174 175 176 public Object clone() 177 { 178 AxisIterator iter = new AxisIterator(_env); 179 180 iter.copy(this); 181 182 if (_parentIter != null) 183 iter._parentIter = (NodeIterator) _parentIter.clone(); 184 185 iter._axis = _axis; 186 iter._node = _node; 187 iter._match = _match; 188 189 return iter; 190 } 191 192 public String toString() 193 { 194 return "AxisIterator[axis:" + _axis + ",match:" + _match + "]"; 195 } 196 } 197 | Popular Tags |