1 23 24 package org.enhydra.xml.lazydom; 25 26 import org.enhydra.apache.xerces.dom.ElementNSImpl; 27 import org.enhydra.apache.xerces.dom.NodeImpl; 28 import org.enhydra.xml.io.PreFormattedText; 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.DOMException ; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 36 40 public class LazyElementNS extends ElementNSImpl implements LazyElement { 41 50 protected LazyElementNS(LazyDocument ownerDoc, 51 LazyElement template, 52 String namespaceURI, 53 String qualifiedName) { 54 super(ownerDoc, 55 (template != null) ? template.getNamespaceURI() : namespaceURI, 56 (template != null) ? template.getNodeName() : qualifiedName); 57 if (template != null) { 58 fTemplateNode = template; 59 fNodeId = template.getNodeId(); 60 } else { 61 fParentExpanded = true; 63 fChildrenExpanded = true; 64 fAttributesExpanded = true; 65 } 66 } 67 68 72 75 private LazyElement fTemplateNode = null; 76 77 82 public void makeTemplateNode(int nodeId, 83 String text) { 84 fNodeId = nodeId; 85 fIsTemplateNode = true; 86 fPreFormattedText = text; 87 } 88 89 93 public LazyElement getTemplateElement() { 94 return fTemplateNode; 95 } 96 97 100 public Node cloneNode(boolean deep) { 101 if (deep && !fChildrenExpanded) { 103 expandChildren(); 104 } 105 if (!fAttributesExpanded) { 107 expandAttributes(); 108 } 109 110 LazyElementNS newElement = (LazyElementNS)super.cloneNode(deep); 112 newElement.fNodeId = NULL_NODE_ID; 113 newElement.fParentExpanded = true; 114 newElement.fChildrenExpanded = true; 115 newElement.fAttributesExpanded = true; 116 return newElement; 117 } 118 119 123 126 private int fNodeId = NULL_NODE_ID; 127 128 131 private boolean fIsTemplateNode; 132 133 136 public void makeTemplateNode(int nodeId) { 137 fNodeId = nodeId; 138 fIsTemplateNode = true; 139 } 140 141 144 public int getNodeId() { 145 return fNodeId; 146 } 147 148 151 public boolean isTemplateNode() { 152 return fIsTemplateNode; 153 } 154 155 158 public LazyNode getTemplateNode() { 159 return fTemplateNode; 160 } 161 162 165 public LazyNode templateClone(Document ownerDocument) { 166 return new LazyElementNS((LazyDocument)ownerDocument, this, null, null); 167 } 168 169 174 public void setNodeValue(String value) { 175 fNodeId = NULL_NODE_ID; 176 super.setNodeValue(value); 177 } 178 179 183 186 private boolean fParentExpanded = false; 187 private boolean fChildrenExpanded = false; 188 189 192 public boolean isParentExpanded() { 193 return fParentExpanded; 194 } 195 196 199 public void setParentExpanded() { 200 fParentExpanded = true; 201 } 202 203 206 public void setParentWhileExpanding(Node parent) { 207 ownerNode = (NodeImpl)parent; 208 flags |= OWNED; 209 fParentExpanded = true; 210 } 211 212 215 public boolean areChildrenExpanded() { 216 return fChildrenExpanded; 217 } 218 219 222 public void setChildrenExpanded() { 223 fChildrenExpanded = true; 224 } 225 226 229 public void appendChildWhileExpanding(Node child) { 230 super.insertBefore(child, null); 231 } 232 233 236 private void expandParent() { 237 ((LazyDocument)getOwnerDocument()).doExpandParent(this); 238 } 239 240 243 private void expandChildren() { 244 ((LazyDocument)getOwnerDocument()).doExpandChildren(this); 245 } 246 247 250 public Node getParentNode() { 251 if (!fParentExpanded) { 252 expandParent(); 253 } 254 return super.getParentNode(); 255 } 256 257 260 public NodeList getChildNodes() { 261 if (!fChildrenExpanded) { 262 expandChildren(); 263 } 264 return super.getChildNodes(); 265 } 266 267 270 public Node getFirstChild() { 271 if (!fChildrenExpanded) { 272 expandChildren(); 273 } 274 return super.getFirstChild(); 275 } 276 277 280 public Node getLastChild() { 281 if (!fChildrenExpanded) { 282 expandChildren(); 283 } 284 return super.getLastChild(); 285 } 286 287 290 public Node getPreviousSibling() { 291 if (!fParentExpanded) { 292 expandParent(); 293 } 294 return super.getPreviousSibling(); 295 } 296 297 300 public Node getNextSibling() { 301 if (!fParentExpanded) { 302 expandParent(); 303 } 304 return super.getNextSibling(); 305 } 306 307 310 public Node insertBefore(Node newChild, 311 Node refChild) 312 throws DOMException { 313 if (!fChildrenExpanded) { 314 expandChildren(); 315 } 316 return super.insertBefore(newChild, refChild); 317 } 318 319 322 public Node replaceChild(Node newChild, 323 Node oldChild) 324 throws DOMException { 325 if (!fChildrenExpanded) { 326 expandChildren(); 327 } 328 return super.replaceChild(newChild, oldChild); 329 } 330 331 334 public Node removeChild(Node oldChild) 335 throws DOMException { 336 if (!fChildrenExpanded) { 337 expandChildren(); 338 } 339 return super.removeChild(oldChild); 340 } 341 342 345 public Node appendChild(Node newChild) 346 throws DOMException { 347 if (!fChildrenExpanded) { 348 expandChildren(); 349 } 350 return super.appendChild(newChild); 351 } 352 353 356 public boolean hasChildNodes() { 357 if (!fChildrenExpanded) { 358 return fTemplateNode.hasChildNodes(); 359 } else { 360 return super.hasChildNodes(); 361 } 362 } 363 364 367 public void normalize() { 368 if (!fChildrenExpanded) { 369 expandChildren(); 370 } 371 super.normalize(); 372 } 373 374 378 381 private boolean fAttributesExpanded = false; 382 383 386 public boolean areAttributesExpanded() { 387 return fAttributesExpanded; 388 } 389 390 394 private void doExpandAttributes(LazyDocument doc) { 395 doc.enterExpansion(); 396 try { 397 NamedNodeMap templateAttrs = fTemplateNode.getAttributes(); 398 if (templateAttrs != null) { 399 int len = templateAttrs.getLength(); 400 for (int idx = 0; idx < len; idx++) { 401 Attr attr = (Attr )doc.getNodeFromTemplate((LazyNode)templateAttrs.item(idx)); 402 super.setAttributeNode(attr); 403 } 404 } 405 fAttributesExpanded = true; 406 } finally { 407 doc.leaveExpansion(); 408 } 409 } 410 411 415 private void expandAttributes() { 416 LazyDocument doc = (LazyDocument)getOwnerDocument(); 417 synchronized (doc) { 418 if (!fAttributesExpanded) { 419 doExpandAttributes(doc); 420 } 421 } 422 } 423 424 427 public NamedNodeMap getAttributes() { 428 if (!fAttributesExpanded) { 429 expandAttributes(); 430 } 431 return super.getAttributes(); 432 } 433 434 437 public String getAttribute(String name) { 438 if (!fAttributesExpanded) { 439 expandAttributes(); 440 } 441 return super.getAttribute(name); 442 } 443 444 447 public void setAttribute(String name, 448 String value) throws DOMException { 449 if (!fAttributesExpanded) { 450 expandAttributes(); 451 } 452 super.setAttribute(name, value); 453 } 454 455 458 public void removeAttribute(String name) throws DOMException { 459 if (!fAttributesExpanded) { 460 expandAttributes(); 461 } 462 super.removeAttribute(name); 463 } 464 465 468 public Attr getAttributeNode(String name) { 469 if (!fAttributesExpanded) { 470 expandAttributes(); 471 } 472 return super.getAttributeNode(name); 473 } 474 475 478 public Attr setAttributeNode(Attr newAttr) throws DOMException { 479 if (!fAttributesExpanded) { 480 expandAttributes(); 481 } 482 return super.setAttributeNode(newAttr); 483 } 484 485 488 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 489 if (!fAttributesExpanded) { 490 expandAttributes(); 491 } 492 return super.removeAttributeNode(oldAttr); 493 } 494 495 498 public String getAttributeNS(String namespaceURI, 499 String localName) { 500 if (!fAttributesExpanded) { 501 expandAttributes(); 502 } 503 return super.getAttributeNS(namespaceURI, localName); 504 } 505 506 509 public void setAttributeNS(String namespaceURI, 510 String qualifiedName, 511 String value) throws DOMException { 512 if (!fAttributesExpanded) { 513 expandAttributes(); 514 } 515 super.setAttributeNS(namespaceURI, qualifiedName, value); 516 } 517 518 521 public void removeAttributeNS(String namespaceURI, 522 String localName) throws DOMException { 523 if (!fAttributesExpanded) { 524 expandAttributes(); 525 } 526 super.removeAttributeNS(namespaceURI, localName); 527 } 528 529 532 public Attr getAttributeNodeNS(String namespaceURI, 533 String localName) { 534 if (!fAttributesExpanded) { 535 expandAttributes(); 536 } 537 return super.getAttributeNodeNS(namespaceURI, localName); 538 } 539 540 543 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { 544 if (!fAttributesExpanded) { 545 expandAttributes(); 546 } 547 return super.setAttributeNodeNS(newAttr); 548 } 549 550 554 557 private String fPreFormattedText; 558 559 562 public String getPreFormattedText() { 563 return fPreFormattedText; 564 } 565 566 569 public void setPreFormattedText(String text) { 570 fPreFormattedText = text; 571 } 572 } 573 | Popular Tags |