1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.log.Log; 32 import com.caucho.xml.XmlUtil; 33 import com.caucho.xpath.ExprEnvironment; 34 import com.caucho.xpath.StylesheetEnv; 35 import com.caucho.xpath.XPathException; 36 import com.caucho.xpath.XPathFun; 37 import com.caucho.xpath.expr.Var; 38 39 import org.w3c.dom.Attr ; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Node ; 42 43 import java.util.Iterator ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 50 public abstract class NodeIterator implements ExprEnvironment, Iterator <Node > { 51 protected static final Logger log = Log.open(NodeIterator.class); 52 53 protected ExprEnvironment _env; 54 55 protected Node _contextNode; 56 protected int _position; 57 protected int _size; 58 59 protected NodeIterator(ExprEnvironment env) 60 { 61 65 66 _env = env; 67 } 68 69 72 public abstract boolean hasNext(); 73 74 77 public abstract Node nextNode() 78 throws XPathException; 79 80 83 public Node next() 84 { 85 Node value = null; 86 87 try { 88 value = nextNode(); 89 } catch (Exception e) { 90 log.log(Level.FINE, e.toString(), e); 91 } 92 93 return value; 94 } 95 96 99 public SelectedNode nextSelectedNode() 100 throws XPathException 101 { 102 Node node = nextNode(); 103 104 if (node == null) 105 return null; 106 else if (node instanceof Attr ) 107 return new SelectedAttribute(node); 108 else 109 return new SelectedNode(node); 110 } 111 112 115 public Node getCurrentNode() 116 { 117 return _env.getCurrentNode(); 118 } 119 120 123 public Node getContextNode() 124 { 125 if (_contextNode != null) 126 return _contextNode; 127 else 128 return _env.getContextNode(); 129 } 130 131 134 public Node setContextNode(Node node) 135 { 136 Node oldNode = _contextNode; 137 _contextNode = node; 138 return oldNode; 139 } 140 141 144 public int getContextPosition() 145 { 146 return _position; 147 } 148 149 152 public int getContextSize() 153 { 154 if (_size == 0) { 155 _size = _position; 156 157 NodeIterator clone = (NodeIterator) clone(); 158 try { 159 while (clone != null && clone.nextNode() != null) 160 _size++; 161 } catch (XPathException e) { 162 } 163 } 164 165 return _size; 166 } 167 168 171 public Document getOwnerDocument() 172 { 173 return _env.getOwnerDocument(); 174 } 175 176 179 public Var getVar(String name) 180 { 181 return _env.getVar(name); 182 } 183 184 187 public XPathFun getFunction(String name) 188 { 189 return _env.getFunction(name); 190 } 191 194 public StylesheetEnv getStylesheetEnv() 195 { 196 return _env.getStylesheetEnv(); 197 } 198 199 202 public Object systemProperty(String namespaceURI, String localName) 203 { 204 return _env.getOwnerDocument(); 205 } 206 207 210 public String stringValue(Node node) 211 { 212 return XmlUtil.textValue(node); 213 } 214 215 218 public int getPositionIndex() 219 { 220 return 0; 221 } 222 223 226 public void setMorePositions(boolean more) 227 { 228 } 229 230 233 public abstract Object clone(); 234 235 238 public void copy(NodeIterator src) 239 { 240 _env = src._env; 241 _position = src._position; 242 _size = src._size; 243 } 244 245 248 public void remove() 249 throws UnsupportedOperationException 250 { 251 throw new UnsupportedOperationException (); 252 } 253 } 254 | Popular Tags |