1 28 29 package com.caucho.xpath.pattern; 30 31 import com.caucho.log.Log; 32 import com.caucho.xpath.Env; 33 import com.caucho.xpath.ExprEnvironment; 34 import com.caucho.xpath.XPathException; 35 36 import org.w3c.dom.Node ; 37 38 import java.util.logging.Logger ; 39 40 51 abstract public class AbstractPattern { 52 protected static final Logger log = Log.open(AbstractPattern.class); 53 54 public static final String XMLNS = "http://www.w3.org/2000/xmlns/"; 56 57 protected AbstractPattern _parent; 58 protected AbstractPattern _child; 59 60 AbstractPattern(AbstractPattern parent) 61 { 62 _parent = parent; 63 64 if (parent != null && parent._child == null) 65 parent._child = this; 66 } 67 68 71 public AbstractPattern getParent() 72 { 73 return _parent; 74 } 75 76 79 public double getPriority() 80 { 81 return 0.5; 82 } 83 84 89 public String getNodeName() 90 { 91 return "*"; 92 } 93 94 102 public NodeIterator select(Node node, ExprEnvironment env) 103 throws XPathException 104 { 105 NodeIterator base = createNodeIterator(node, env, copyPosition()); 106 107 if (isStrictlyAscending()) 108 return base; 109 else 110 return new MergeIterator(env, base); 111 } 112 113 123 public NodeIterator selectUnique(Node node, ExprEnvironment env) 124 throws XPathException 125 { 126 NodeIterator base = createNodeIterator(node, env, copyPosition()); 127 128 if (isUnique()) 129 return base; 130 else 131 return new UniqueIterator(env, base); 132 } 133 134 142 public Node findAny(Node node, ExprEnvironment env) 143 throws XPathException 144 { 145 NodeIterator base = createNodeIterator(node, env, copyPosition()); 146 147 return base.nextNode(); 148 } 149 150 153 public boolean isStrictlyAscending() 154 { 155 if (_parent != null) 156 return _parent.isStrictlyAscending(); 157 else 158 return false; 159 } 160 161 164 public boolean isUnique() 165 { 166 if (_parent != null) 167 return _parent.isUnique(); 168 else 169 return false; 170 } 171 172 175 boolean isSingleSelect() 176 { 177 if (_parent != null) 178 return _parent.isSingleSelect(); 179 else 180 return false; 181 } 182 183 186 boolean isSingleLevel() 187 { 188 return isSingleSelect(); 189 } 190 191 200 public NodeIterator createNodeIterator(Node node, ExprEnvironment env, 201 AbstractPattern pattern) 202 throws XPathException 203 { 204 if (_parent == null) 205 throw new RuntimeException (String.valueOf(this) + " " + getClass()); 206 else 207 return _parent.createNodeIterator(node, env, pattern); 208 } 209 210 218 public Node firstNode(Node node, ExprEnvironment env) 219 throws XPathException 220 { 221 throw new UnsupportedOperationException (String.valueOf(this) + " " + getClass()); 222 } 223 224 231 public Node lastNode(Node node) 232 { 233 return null; 234 } 235 236 244 public Node nextNode(Node node, Node last) 245 throws XPathException 246 { 247 throw new UnsupportedOperationException (); 248 } 249 250 258 public abstract boolean match(Node node, ExprEnvironment env) 259 throws XPathException; 260 261 264 public boolean isAscending() 265 { 266 return true; 267 } 268 269 278 public int position(Node node, Env env, AbstractPattern pattern) 279 throws XPathException 280 { 281 return _parent.position(node, env, pattern); 282 } 283 284 293 public int count(Node node, Env env, AbstractPattern pattern) 294 throws XPathException 295 { 296 return _parent.count(node, env, pattern); 297 } 298 299 302 public AbstractPattern copyAxis() 303 { 304 if (_parent != null) 305 return _parent.copyAxis(); 306 else 307 return null; 308 } 309 310 313 public AbstractPattern copyPosition() 314 { 315 return this; 316 } 317 318 322 protected String getPrefix() 323 { 324 if (_parent == null || 325 _parent instanceof FromAny) 326 return ""; 327 else if (_parent instanceof FromContext) { FromContext context = (FromContext) _parent; 329 330 String name = ""; 331 for (int i = 0; i < context.getCount(); i++) { 332 name += "../"; 333 } 334 return name; 335 } 336 else if (_parent instanceof FromRoot) 337 return "/"; 338 else 339 return _parent + "/"; 340 } 341 342 public String toPatternString() 343 { 344 return toString(); 345 } 346 } 347 | Popular Tags |