1 7 package org.enhydra.xml; 8 9 import java.util.ArrayList ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.w3c.dom.Attr ; 15 import org.w3c.dom.DOMException ; 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Element ; 18 import org.w3c.dom.NamedNodeMap ; 19 import org.w3c.dom.Node ; 20 import org.w3c.dom.NodeList ; 21 import org.w3c.dom.TypeInfo ; 22 import org.w3c.dom.UserDataHandler ; 23 24 35 public class ElementImpl extends NodeImpl implements Element { 36 37 41 protected HashMap attributes = null; 42 43 44 47 public ElementImpl() { 48 attributes = new HashMap (); 49 type = ELEMENT_NODE; 50 } 51 52 53 59 public ElementImpl(ElementImpl element) { 60 super(element); 61 attributes = element.attributes; 62 type = ELEMENT_NODE; 63 } 64 65 66 73 public ElementImpl(Document ownerDoc, String name) { 74 super(ownerDoc,name,ELEMENT_NODE); 75 this.attributes = new HashMap (); 76 } 77 78 79 88 protected ElementImpl(Document ownerDoc, String nodeName, short type, String value) { 89 super(ownerDoc, nodeName, type, value); 90 } 91 92 93 99 public ElementImpl(Node node) { 100 this(node,true); 101 } 102 103 104 113 public ElementImpl(Node node, boolean deep) { 114 super(node,false); 115 attributes = new HashMap (); 116 NamedNodeMap attrs = node.getAttributes(); 117 if (attrs != null) { 118 for (int i = 0; i < attrs.getLength(); i++) { 119 Attr attr = new AttrImpl((Attr ) attrs.item(i)); 120 attributes.put(attr.getName(), attr); 121 } 122 } 123 if (deep) 124 initNodeImplChildren(node); 125 } 126 127 128 136 public static Element newInstance(Document document) { 137 Node root = document.getDocumentElement(); 138 return new ElementImpl(root); 139 } 140 141 142 156 public Node insertBefore(Node newChild, Node refChild) { 157 super.insertBefore(newChild,refChild); 158 return newChild; 159 } 160 161 162 175 public Node replaceChild(Node newChild, Node oldChild) { 176 super.replaceChild(newChild,oldChild); 177 return oldChild; 178 } 179 180 181 192 public Node removeChild(Node oldChild) { 193 super.removeChild(oldChild); 194 return oldChild; 195 } 196 197 198 212 public Node cloneNode(boolean deep) { 213 return new ElementImpl(this,deep); 214 } 215 216 217 218 219 220 222 223 228 public String getTagName() { 229 return nodeName; 230 } 231 232 233 238 public NamedNodeMap getAttributes() { 239 return new HashMapNamedNodeMap(attributes); 240 } 241 242 243 250 public String getAttribute(String name) { 251 Attr attr = getAttributeNode(name); 252 if (attr == null) { 253 return ""; 254 } 255 return attr.getValue(); 256 } 257 258 259 264 public String getAttributeNS(String namespaceURI, String localName) { 265 return getAttribute(localName); 266 } 267 268 269 275 public void setAttribute(String name, String value) { 276 if (!org.apache.crimson.util.XmlNames.isName(name)) { 279 throw new NodeDOMException( 280 DOMException.INVALID_CHARACTER_ERR, 281 "Attribute name is illegal!"); 282 } 283 attributes.put(name, new AttrImpl(this, name, value)); 284 } 285 286 287 292 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) { 293 setAttribute(qualifiedName, value); 294 } 295 296 297 302 public void removeAttribute(String name) { 303 if (type != ELEMENT_NODE) 304 throw new NodeDOMException( 305 DOMException.NOT_SUPPORTED_ERR, 306 "Node doesn't have attributes"); 307 removeAttribute(name, true); 308 } 309 310 311 private void removeAttribute(String name, boolean checkPresent) { 312 if (attributes.remove(name) != null) 313 return; 314 if (checkPresent) { 316 throw new NodeDOMException( 317 DOMException.NOT_FOUND_ERR, 318 "No such attribute!"); 319 } 320 } 321 322 323 329 public boolean hasAttributes() { 330 return attributes.size() > 0; 331 } 332 333 334 340 public boolean hasAttribute(String name) { 341 return getAttributeNode(name) != null; 342 } 343 344 345 348 public void removeAttributeNS(String namespaceURI, String localName) { 349 removeAttribute(localName); 350 } 351 352 353 360 public Attr getAttributeNode(String name) { 361 return (Attr ) attributes.get(name); 362 } 363 364 365 370 public Attr getAttributeNodeNS(String namespaceURI, String localName) { 371 return getAttributeNode(localName); 372 } 373 374 375 382 public Attr setAttributeNode(Attr newAttr) throws DOMException { 383 AttrImpl attr; 384 if (newAttr instanceof AttrImpl) { 385 attr = (AttrImpl) newAttr; 386 } else { 387 attr = new AttrImpl(newAttr); 388 } 389 attributes.put(attr.getName(), attr); 390 return attr; 391 } 392 393 394 399 public Attr setAttributeNodeNS(Attr newAttr) { 400 return setAttributeNode(newAttr); 401 } 402 403 404 411 public Attr removeAttributeNode(Attr oldAttr) { 412 removeAttribute(oldAttr.getName()); 413 return oldAttr; 414 } 415 416 417 420 public boolean hasAttributeNS(String namespaceURI, String localName) { 421 return hasAttribute(localName); 422 } 423 424 425 433 public NodeList getElementsByTagName(String name) { 434 List list = new ArrayList (); 435 getElementsByTagName(name, list); 436 return new NodeListImpl(list); 437 } 438 439 440 private void getElementsByTagName(String name, List list) { 441 if (nodeName.equals(name)) { 442 list.add(this); 443 } 444 445 Node child = getFirstChild(); 446 while (child != null) { 447 if (child.getNodeType() == Node.ELEMENT_NODE) 448 ((ElementImpl)child).getElementsByTagName(name, list); 449 child = child.getNextSibling(); 450 } 451 } 452 453 454 457 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) { 458 return getElementsByTagName(localName); 459 } 460 461 462 467 public boolean hasElementChildNodes() { 468 Node child = getFirstChild(); 469 while (child != null) { 470 if (child.getNodeType() == Node.ELEMENT_NODE) 471 return true; 472 child = child.getNextSibling(); 473 } 474 return false; 475 } 476 477 478 485 protected void beginToString(StringBuffer sb, Indent indent) { 486 sb.append("\n" + indent + "<" + this.nodeName); 487 488 for (Iterator iter = attributes.values().iterator(); iter.hasNext();) { 489 Attr attr = (Attr ) iter.next(); 490 sb.append(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\""); 491 } 492 if (hasChildNodes()) { 493 sb.append(">"); 494 indent.increment(); 495 } else 496 sb.append("/>"); 497 } 498 499 500 507 protected void endToString(StringBuffer sb, Indent indent) { 508 if (hasChildNodes()) { 509 indent.decrement(); 510 if (hasElementChildNodes()) 511 sb.append("\n" + indent + "</" + this.nodeName + ">"); 512 else 513 sb.append("</" + this.nodeName + ">"); 514 } 515 } 516 517 518 521 public TypeInfo getSchemaTypeInfo() { 522 return null; 524 } 525 526 527 530 public void setIdAttribute(String arg0, boolean arg1) throws DOMException { 531 533 } 534 535 536 539 public void setIdAttributeNS(String arg0, String arg1, boolean arg2) throws DOMException { 540 542 } 543 544 545 548 public void setIdAttributeNode(Attr arg0, boolean arg1) throws DOMException { 549 551 } 552 553 554 557 public String getNamespaceURI() { 558 return null; 560 } 561 562 563 566 public String getBaseURI() { 567 return null; 569 } 570 571 572 575 public short compareDocumentPosition(Node arg0) throws DOMException { 576 return 0; 578 } 579 580 581 584 public String getTextContent() throws DOMException { 585 return null; 587 } 588 589 590 593 public void setTextContent(String arg0) throws DOMException { 594 596 } 597 598 599 602 public boolean isSameNode(Node arg0) { 603 return false; 605 } 606 607 608 611 public String lookupPrefix(String arg0) { 612 return null; 614 } 615 616 617 620 public boolean isDefaultNamespace(String arg0) { 621 return false; 623 } 624 625 626 629 public String lookupNamespaceURI(String arg0) { 630 return null; 632 } 633 634 635 638 public boolean isEqualNode(Node arg0) { 639 return false; 641 } 642 643 644 647 public Object getFeature(String arg0, String arg1) { 648 return null; 650 } 651 652 653 656 public Object setUserData(String arg0, Object arg1, UserDataHandler arg2) { 657 return null; 659 } 660 661 662 665 public Object getUserData(String arg0) { 666 return null; 668 } 669 } 670 | Popular Tags |