1 package com.icl.saxon.tinytree; 2 import com.icl.saxon.om.*; 3 import com.icl.saxon.Context; 4 import com.icl.saxon.expr.NodeSetExtent; 5 import com.icl.saxon.pattern.NodeTest; 6 import com.icl.saxon.pattern.NameTest; 7 import com.icl.saxon.output.Outputter; 8 import com.icl.saxon.tree.DOMExceptionImpl; 9 10 import org.w3c.dom.*; 11 import javax.xml.transform.TransformerException ; 12 13 14 21 22 abstract class TinyNodeImpl extends AbstractNode { 23 24 protected TinyDocumentImpl document; 25 protected int nodeNr; 26 protected TinyNodeImpl parent = null; 27 28 34 35 public void setSystemId(String uri) { 36 short type = document.nodeType[nodeNr]; 37 if (type==ATTRIBUTE || type==NAMESPACE) { 38 ((TinyNodeImpl)getParent()).setSystemId(uri); 39 } else { 40 document.setSystemId(nodeNr, uri); 41 } 42 } 43 44 49 50 protected void setParentNode(TinyNodeImpl parent) { 51 this.parent = parent; 52 } 53 54 59 60 public boolean isSameNode(NodeInfo other) { 61 if (this==other) return true; 62 if (!(other instanceof TinyNodeImpl)) return false; 63 return this.document==((TinyNodeImpl)other).document && 64 this.nodeNr==((TinyNodeImpl)other).nodeNr; 65 } 66 67 70 71 public String getSystemId() { 72 return document.getSystemId(nodeNr); 73 } 74 75 79 80 public String getBaseURI() { 81 return (getParent()).getBaseURI(); 82 } 83 84 87 88 public Node getOriginatingNode() { 89 return this; 90 } 91 92 93 96 97 public void setLineNumber(int line) { 98 document.setLineNumber(nodeNr, line); 99 } 100 101 102 105 106 public int getLineNumber() { 107 return document.getLineNumber(nodeNr); 108 } 109 110 115 116 protected long getSequenceNumber() { 117 return (long)nodeNr << 32; 118 } 119 120 128 129 public final int compareOrder(NodeInfo other) { 130 long a = getSequenceNumber(); 131 long b = ((TinyNodeImpl)other).getSequenceNumber(); 132 if (a<b) return -1; 133 if (a>b) return +1; 134 return 0; 135 } 136 137 140 141 public int getFingerprint() { 142 int nc = getNameCode(); 143 if (nc==-1) return -1; 144 return nc & 0xfffff; 145 } 146 147 150 151 public int getNameCode() { 152 return document.nameCode[nodeNr]; 154 } 155 156 160 161 public String getPrefix() { 162 int code = document.nameCode[nodeNr]; 163 if (code<0) return ""; 164 if ((code>>20 & 0xff) == 0) return ""; 165 return document.getNamePool().getPrefix(code); 166 } 167 168 174 175 public String getURI() { 176 int code = document.nameCode[nodeNr]; 177 if (code<0) return ""; 178 return document.getNamePool().getURI(code); 179 } 180 181 187 188 public String getDisplayName() { 189 int code = document.nameCode[nodeNr]; 190 if (code<0) return ""; 191 return document.getNamePool().getDisplayName(code); 192 } 193 194 199 200 public String getLocalName() { 201 int code = document.nameCode[nodeNr]; 202 if (code<0) return ""; 203 return document.getNamePool().getLocalName(code); 204 } 205 206 212 213 public AxisEnumeration getEnumeration( 214 byte axisNumber, 215 NodeTest nodeTest) { 216 217 219 short type = getNodeType(); 220 switch (axisNumber) { 221 case Axis.ANCESTOR: 222 if (type==ROOT) { 223 return EmptyEnumeration.getInstance(); 224 } else { 225 return new AncestorEnumeration(document, this, nodeTest, false); 226 } 227 228 case Axis.ANCESTOR_OR_SELF: 229 if (type==ROOT) { 230 if (nodeTest.matches(this)) { 231 return new SingletonEnumeration(this); 232 } else { 233 return EmptyEnumeration.getInstance(); 234 } 235 } else { 236 return new AncestorEnumeration(document, this, nodeTest, true); 237 } 238 239 case Axis.ATTRIBUTE: 240 if (type!=ELEMENT) return EmptyEnumeration.getInstance(); 241 if (document.offset[nodeNr]<0) return EmptyEnumeration.getInstance(); 242 return new AttributeEnumeration(document, nodeNr, nodeTest); 243 244 case Axis.CHILD: 245 if (hasChildNodes()) { 246 return new SiblingEnumeration(document, this, nodeTest, true); 247 } else { 248 return EmptyEnumeration.getInstance(); 249 } 250 251 case Axis.DESCENDANT: 252 if (type==ROOT && 253 nodeTest instanceof NameTest && 254 nodeTest.getNodeType()==ELEMENT) { 255 return ((TinyDocumentImpl)this).getAllElements( 256 ((NameTest)nodeTest).getFingerprint()); 257 } else if (hasChildNodes()) { 258 return new DescendantEnumeration(document, this, nodeTest, false); 259 } else { 260 return EmptyEnumeration.getInstance(); 261 } 262 263 case Axis.DESCENDANT_OR_SELF: 264 if (hasChildNodes()) { 265 return new DescendantEnumeration(document, this, nodeTest, true); 266 } else { 267 if (nodeTest.matches(this)) { 268 return new SingletonEnumeration(this); 269 } else { 270 return EmptyEnumeration.getInstance(); 271 } 272 } 273 case Axis.FOLLOWING: 274 if (type==ROOT) { 275 return EmptyEnumeration.getInstance(); 276 } else if (type==ATTRIBUTE || type==NAMESPACE) { 277 return new FollowingEnumeration( 278 document, (TinyNodeImpl)getParent(), nodeTest, true); 279 } else { 280 return new FollowingEnumeration( 281 document, this, nodeTest, false); 282 } 283 284 case Axis.FOLLOWING_SIBLING: 285 if (type==ROOT || type==ATTRIBUTE || type==NAMESPACE) { 286 return EmptyEnumeration.getInstance(); 287 } else { 288 return new SiblingEnumeration( 289 document, this, nodeTest, false); 290 } 291 292 case Axis.NAMESPACE: 293 if (type!=ELEMENT) return EmptyEnumeration.getInstance(); 294 return new NamespaceEnumeration((TinyElementImpl)this, nodeTest); 295 296 case Axis.PARENT: 297 NodeInfo parent = (NodeInfo)getParent(); 298 if (parent==null) return EmptyEnumeration.getInstance(); 299 if (nodeTest.matches(parent)) return new SingletonEnumeration(parent); 300 return EmptyEnumeration.getInstance(); 301 302 case Axis.PRECEDING: 303 if (type==ROOT) { 304 return EmptyEnumeration.getInstance(); 305 } else if (type==ATTRIBUTE || type==NAMESPACE) { 306 return new PrecedingEnumeration( 307 document, (TinyNodeImpl)getParent(), nodeTest, false); 308 } else { 309 return new PrecedingEnumeration( 310 document, this, nodeTest, false); 311 } 312 313 case Axis.PRECEDING_SIBLING: 314 if (type==ROOT || type==ATTRIBUTE || type==NAMESPACE) { 315 return EmptyEnumeration.getInstance(); 316 } else { 317 return new PrecedingSiblingEnumeration( 318 document, this, nodeTest); 319 } 320 321 case Axis.SELF: 322 if (nodeTest.matches(this)) return new SingletonEnumeration(this); 323 return EmptyEnumeration.getInstance(); 324 325 case Axis.PRECEDING_OR_ANCESTOR: 326 if (type==ROOT) { 327 return EmptyEnumeration.getInstance(); 328 } else if (type==ATTRIBUTE || type==NAMESPACE) { 329 return new PrecedingEnumeration( 330 document, (TinyNodeImpl)getParent(), nodeTest, true); 331 } else { 332 return new PrecedingEnumeration( 333 document, this, nodeTest, true); 334 } 335 336 default: 337 throw new IllegalArgumentException ("Unknown axis number " + axisNumber); 338 } 339 } 340 341 345 346 public NodeInfo getParent() { 347 if (parent!=null) return parent; 348 349 for (int i=nodeNr-1; i>=0; i--) { 351 if (document.depth[i]<document.depth[nodeNr]) { 352 parent = document.getNode(i); 353 return parent; 354 } 355 } 356 parent = document; 357 return parent; 358 } 359 360 365 366 public boolean hasChildNodes() { 367 return false; 369 } 370 371 377 378 public boolean hasAttributes() { 379 return false; 381 } 382 383 391 392 public String getAttributeValue( String uri, String localName ) { 393 return null; 394 } 395 396 397 405 406 411 412 417 418 public String getAttributeValue(int fingerprint) { 419 return null; 421 } 422 423 427 428 public DocumentInfo getDocumentRoot() { 429 return document; 430 } 431 432 439 440 public void outputNamespaceNodes(Outputter out, boolean includeAncestors) 441 throws TransformerException 442 {} 443 444 448 449 public String generateId() { 450 return document.generateId() + 451 NODE_LETTER[getNodeType()] + 452 nodeNr; 453 } 454 455 } 456 457 | Popular Tags |