1 23 24 package org.enhydra.xml.lazydom; 25 26 import org.enhydra.apache.xerces.dom.ElementImpl; 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 43 public class LazyElementNoNS extends ElementImpl implements LazyElement { 44 51 protected LazyElementNoNS(LazyDocument ownerDoc, 52 LazyElement template, 53 String qualifiedName) { 54 super(ownerDoc, 55 (template != null) ? template.getNodeName() : qualifiedName); 56 if (template != null) { 57 fTemplateNode = template; 58 fNodeId = template.getNodeId(); 59 } else { 60 fParentExpanded = true; 62 fChildrenExpanded = true; 63 fAttributesExpanded = true; 64 } 65 } 66 67 71 74 private LazyElement fTemplateNode = null; 75 76 81 public void makeTemplateNode(int nodeId, 82 String text) { 83 fNodeId = nodeId; 84 fIsTemplateNode = true; 85 fPreFormattedText = text; 86 } 87 88 92 public LazyElement getTemplateElement() { 93 return fTemplateNode; 94 } 95 96 99 public Node cloneNode(boolean deep) { 100 if (deep && !fChildrenExpanded) { 102 expandChildren(); 103 } 104 if (!fAttributesExpanded) { 106 expandAttributes(); 107 } 108 109 LazyElementNoNS newElement = (LazyElementNoNS)super.cloneNode(deep); 111 newElement.fNodeId = NULL_NODE_ID; 112 newElement.fParentExpanded = true; 113 newElement.fChildrenExpanded = true; 114 newElement.fAttributesExpanded = true; 115 return newElement; 116 } 117 118 122 125 private int fNodeId = NULL_NODE_ID; 126 127 130 private boolean fIsTemplateNode; 131 132 135 public void makeTemplateNode(int nodeId) { 136 fNodeId = nodeId; 137 fIsTemplateNode = true; 138 } 139 140 143 public int getNodeId() { 144 return fNodeId; 145 } 146 147 150 public boolean isTemplateNode() { 151 return fIsTemplateNode; 152 } 153 154 157 public LazyNode getTemplateNode() { 158 return fTemplateNode; 159 } 160 161 164 public LazyNode templateClone(Document ownerDocument) { 165 return new LazyElementNoNS((LazyDocument)ownerDocument, this, null); 166 } 167 168 173 public void setNodeValue(String value) { 174 fNodeId = NULL_NODE_ID; 175 super.setNodeValue(value); 176 } 177 178 182 185 private boolean fParentExpanded = false; 186 private boolean fChildrenExpanded = false; 187 188 191 public boolean isParentExpanded() { 192 return fParentExpanded; 193 } 194 195 198 public void setParentExpanded() { 199 fParentExpanded = true; 200 } 201 202 205 public void setParentWhileExpanding(Node parent) { 206 ownerNode = (NodeImpl)parent; 207 flags |= OWNED; 208 fParentExpanded = true; 209 } 210 211 214 public boolean areChildrenExpanded() { 215 return fChildrenExpanded; 216 } 217 218 221 public void setChildrenExpanded() { 222 fChildrenExpanded = true; 223 } 224 225 228 public void appendChildWhileExpanding(Node child) { 229 super.insertBefore(child, null); 230 } 231 232 235 private void expandParent() { 236 ((LazyDocument)getOwnerDocument()).doExpandParent(this); 237 } 238 239 242 protected void expandChildren() { 243 ((LazyDocument)getOwnerDocument()).doExpandChildren(this); 244 } 245 246 249 public Node getParentNode() { 250 if (!fParentExpanded) { 251 expandParent(); 252 } 253 return super.getParentNode(); 254 } 255 256 259 public NodeList getChildNodes() { 260 if (!fChildrenExpanded) { 261 expandChildren(); 262 } 263 return super.getChildNodes(); 264 } 265 266 269 public Node getFirstChild() { 270 if (!fChildrenExpanded) { 271 expandChildren(); 272 } 273 return super.getFirstChild(); 274 } 275 276 279 public Node getLastChild() { 280 if (!fChildrenExpanded) { 281 expandChildren(); 282 } 283 return super.getLastChild(); 284 } 285 286 289 public Node getPreviousSibling() { 290 if (!fParentExpanded) { 291 expandParent(); 292 } 293 return super.getPreviousSibling(); 294 } 295 296 299 public Node getNextSibling() { 300 if (!fParentExpanded) { 301 expandParent(); 302 } 303 return super.getNextSibling(); 304 } 305 306 309 public Node insertBefore(Node newChild, 310 Node refChild) 311 throws DOMException { 312 if (!fChildrenExpanded) { 313 expandChildren(); 314 } 315 return super.insertBefore(newChild, refChild); 316 } 317 318 321 public Node replaceChild(Node newChild, 322 Node oldChild) 323 throws DOMException { 324 if (!fChildrenExpanded) { 325 expandChildren(); 326 } 327 return super.replaceChild(newChild, oldChild); 328 } 329 330 333 public Node removeChild(Node oldChild) 334 throws DOMException { 335 if (!fChildrenExpanded) { 336 expandChildren(); 337 } 338 return super.removeChild(oldChild); 339 } 340 341 344 public Node appendChild(Node newChild) 345 throws DOMException { 346 if (!fChildrenExpanded) { 347 expandChildren(); 348 } 349 return super.appendChild(newChild); 350 } 351 352 355 public boolean hasChildNodes() { 356 if (!fChildrenExpanded) { 357 return fTemplateNode.hasChildNodes(); 358 } else { 359 return super.hasChildNodes(); 360 } 361 } 362 363 366 public void normalize() { 367 if (!fChildrenExpanded) { 368 expandChildren(); 369 } 370 super.normalize(); 371 } 372 373 377 380 private boolean fAttributesExpanded = false; 381 382 385 public boolean areAttributesExpanded() { 386 return fAttributesExpanded; 387 } 388 389 393 private void doExpandAttributes(LazyDocument doc) { 394 doc.enterExpansion(); 395 try { 396 NamedNodeMap templateAttrs = fTemplateNode.getAttributes(); 397 if (templateAttrs != null) { 398 int len = templateAttrs.getLength(); 399 for (int idx = 0; idx < len; idx++) { 400 Attr attr = (Attr )doc.getNodeFromTemplate((LazyNode)templateAttrs.item(idx)); 401 super.setAttributeNode(attr); 402 } 403 } 404 fAttributesExpanded = true; 405 } finally { 406 doc.leaveExpansion(); 407 } 408 } 409 410 414 private void expandAttributes() { 415 LazyDocument doc = (LazyDocument)getOwnerDocument(); 416 synchronized (doc) { 417 if (!fAttributesExpanded) { 418 doExpandAttributes(doc); 419 } 420 } 421 } 422 423 426 public NamedNodeMap getAttributes() { 427 if (!fAttributesExpanded) { 428 expandAttributes(); 429 } 430 return super.getAttributes(); 431 } 432 433 436 public String getAttribute(String name) { 437 if (!fAttributesExpanded) { 438 expandAttributes(); 439 } 440 return super.getAttribute(name); 441 } 442 443 446 public void setAttribute(String name, 447 String value) throws DOMException { 448 if (!fAttributesExpanded) { 449 expandAttributes(); 450 } 451 super.setAttribute(name, value); 452 } 453 454 457 public void removeAttribute(String name) throws DOMException { 458 if (!fAttributesExpanded) { 459 expandAttributes(); 460 } 461 super.removeAttribute(name); 462 } 463 464 467 public Attr getAttributeNode(String name) { 468 if (!fAttributesExpanded) { 469 expandAttributes(); 470 } 471 return super.getAttributeNode(name); 472 } 473 474 477 public Attr setAttributeNode(Attr newAttr) throws DOMException { 478 if (!fAttributesExpanded) { 479 expandAttributes(); 480 } 481 return super.setAttributeNode(newAttr); 482 } 483 484 487 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 488 if (!fAttributesExpanded) { 489 expandAttributes(); 490 } 491 return super.removeAttributeNode(oldAttr); 492 } 493 494 497 public String getAttributeNS(String namespaceURI, 498 String localName) { 499 if (!fAttributesExpanded) { 500 expandAttributes(); 501 } 502 return super.getAttributeNS(namespaceURI, localName); 503 } 504 505 508 public void setAttributeNS(String namespaceURI, 509 String qualifiedName, 510 String value) throws DOMException { 511 if (!fAttributesExpanded) { 512 expandAttributes(); 513 } 514 super.setAttributeNS(namespaceURI, qualifiedName, value); 515 } 516 517 520 public void removeAttributeNS(String namespaceURI, 521 String localName) throws DOMException { 522 if (!fAttributesExpanded) { 523 expandAttributes(); 524 } 525 super.removeAttributeNS(namespaceURI, localName); 526 } 527 528 531 public Attr getAttributeNodeNS(String namespaceURI, 532 String localName) { 533 if (!fAttributesExpanded) { 534 expandAttributes(); 535 } 536 return super.getAttributeNodeNS(namespaceURI, localName); 537 } 538 539 542 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { 543 if (!fAttributesExpanded) { 544 expandAttributes(); 545 } 546 return super.setAttributeNodeNS(newAttr); 547 } 548 549 553 556 private String fPreFormattedText; 557 558 561 public String getPreFormattedText() { 562 return fPreFormattedText; 563 } 564 565 568 public void setPreFormattedText(String text) { 569 fPreFormattedText = text; 570 } 571 } 572 | Popular Tags |