1 7 8 package javax.imageio.metadata; 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.w3c.dom.Attr ; 15 import org.w3c.dom.Document ; 16 import org.w3c.dom.Element ; 17 import org.w3c.dom.DOMException ; 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 25 class IIODOMException extends DOMException { 26 27 public IIODOMException(short code, String message) { 28 super(code, message); 29 } 30 } 31 32 class IIONamedNodeMap implements NamedNodeMap { 33 34 List nodes; 35 36 public IIONamedNodeMap(List nodes) { 37 this.nodes = nodes; 38 } 39 40 public int getLength() { 41 return nodes.size(); 42 } 43 44 public Node getNamedItem(String name) { 45 Iterator iter = nodes.iterator(); 46 while (iter.hasNext()) { 47 Node node = (Node )iter.next(); 48 if (name.equals(node.getNodeName())) { 49 return node; 50 } 51 } 52 53 return null; 54 } 55 56 public Node item(int index) { 57 Node node = (Node )nodes.get(index); 58 return node; 59 } 60 61 public Node removeNamedItem(java.lang.String name) { 62 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 63 "This NamedNodeMap is read-only!"); 64 } 65 66 public Node setNamedItem(Node arg) { 67 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, 68 "This NamedNodeMap is read-only!"); 69 } 70 71 74 public Node getNamedItemNS(String namespaceURI, String localName) { 75 return getNamedItem(localName); 76 } 77 78 81 public Node setNamedItemNS(Node arg) { 82 return setNamedItem(arg); 83 } 84 85 88 public Node removeNamedItemNS(String namespaceURI, String localName) { 89 return removeNamedItem(localName); 90 } 91 } 92 93 class IIONodeList implements NodeList { 94 95 List nodes; 96 97 public IIONodeList(List nodes) { 98 this.nodes = nodes; 99 } 100 101 public int getLength() { 102 return nodes.size(); 103 } 104 105 public Node item(int index) { 106 if (index < 0 || index > nodes.size()) { 107 return null; 108 } 109 return (Node )nodes.get(index); 110 } 111 } 112 113 class IIOAttr extends IIOMetadataNode implements Attr { 114 115 boolean specified = true; 116 117 Element owner; 118 String name; 119 String value; 120 121 public IIOAttr(Element owner, String name, String value) { 122 this.owner = owner; 123 this.name = name; 124 this.value = value; 125 } 126 127 public String getName() { 128 return name; 129 } 130 131 public String getNodeName() { 132 return name; 133 } 134 135 public short getNodeType() { 136 return ATTRIBUTE_NODE; 137 } 138 139 public boolean getSpecified() { 140 return specified; 141 } 142 143 public String getValue() { 144 return value; 145 } 146 147 public String getNodeValue() { 148 return value; 149 } 150 151 public void setValue(String value) { 152 this.value = value; 153 } 154 155 public void setNodeValue(String value) { 156 this.value = value; 157 } 158 159 public Element getOwnerElement() { 160 return owner; 161 } 162 163 public void setOwnerElement(Element owner) { 164 this.owner = owner; 165 } 166 167 public boolean isId( ) { 169 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 170 } 171 public TypeInfo getSchemaTypeInfo() { 172 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 173 } 174 public Object setUserData(String key, 175 Object data, 176 UserDataHandler handler) { 177 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 178 } 179 180 181 public Object getUserData ( String key ) { 182 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 183 } 184 185 public Object getFeature ( String feature, String version ) { 186 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 187 } 188 public boolean isEqualNode( Node node ) { 189 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 190 } 191 public boolean isSameNode( Node node ) { 192 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 193 } 194 195 public String lookupNamespaceURI( String prefix ) { 196 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 197 } 198 public boolean isDefaultNamespace(String namespaceURI) { 199 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 200 } 201 202 public String lookupPrefix(String namespaceURI) { 203 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 204 } 205 206 207 String textContent; 208 public String getTextContent() throws DOMException { 209 return textContent; 210 } 211 public void setTextContent(String textContent) throws DOMException { 212 this.textContent = textContent; } 214 public short compareDocumentPosition(Node other) 215 throws DOMException { 216 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 217 } 218 219 public String getBaseURI() { 220 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 221 } 222 224 225 } 226 227 251 public class IIOMetadataNode implements Element , NodeList { 252 253 256 private String nodeName = null; 257 258 262 private String nodeValue = null; 263 264 267 private Object userObject = null; 268 269 273 private IIOMetadataNode parent = null; 274 275 278 private int numChildren = 0; 279 280 284 private IIOMetadataNode firstChild = null; 285 286 290 private IIOMetadataNode lastChild = null; 291 292 296 private IIOMetadataNode nextSibling = null; 297 298 302 private IIOMetadataNode previousSibling = null; 303 304 308 private List attributes = new ArrayList (); 309 310 313 public IIOMetadataNode() {} 314 315 321 public IIOMetadataNode(String nodeName) { 322 this.nodeName = nodeName; 323 } 324 325 329 private void checkNode(Node node) throws DOMException { 330 if (node == null) { 331 return; 332 } 333 if (!(node instanceof IIOMetadataNode )) { 334 throw new IIODOMException(DOMException.WRONG_DOCUMENT_ERR, 335 "Node not an IIOMetadataNode!"); 336 } 337 } 338 339 341 346 public String getNodeName() { 347 return nodeName; 348 } 349 350 public String getNodeValue() throws DOMException { 351 return nodeValue; 352 } 353 354 public void setNodeValue(String nodeValue) throws DOMException { 355 this.nodeValue = nodeValue; 356 } 357 358 364 public short getNodeType() { 365 return ELEMENT_NODE; 366 } 367 368 381 public Node getParentNode() { 382 return parent; 383 } 384 385 public NodeList getChildNodes() { 386 return this; 387 } 388 389 396 public Node getFirstChild() { 397 return firstChild; 398 } 399 400 407 public Node getLastChild() { 408 return lastChild; 409 } 410 411 418 public Node getPreviousSibling() { 419 return previousSibling; 420 } 421 422 429 public Node getNextSibling() { 430 return nextSibling; 431 } 432 433 public NamedNodeMap getAttributes() { 434 return new IIONamedNodeMap(attributes); 435 } 436 437 443 public Document getOwnerDocument() { 444 return null; 445 } 446 447 461 public Node insertBefore(Node newChild, 462 Node refChild) { 463 if (newChild == null) { 464 throw new IllegalArgumentException ("newChild == null!"); 465 } 466 467 checkNode(newChild); 468 checkNode(refChild); 469 470 IIOMetadataNode newChildNode = (IIOMetadataNode )newChild; 471 IIOMetadataNode refChildNode = (IIOMetadataNode )refChild; 472 473 IIOMetadataNode previous = null; 475 IIOMetadataNode next = null; 476 477 if (refChild == null) { 478 previous = this.lastChild; 479 next = null; 480 this.lastChild = newChildNode; 481 } else { 482 previous = refChildNode.previousSibling; 483 next = refChildNode; 484 } 485 486 if (previous != null) { 487 previous.nextSibling = newChildNode; 488 } 489 if (next != null) { 490 next.previousSibling = newChildNode; 491 } 492 493 newChildNode.parent = this; 494 newChildNode.previousSibling = previous; 495 newChildNode.nextSibling = next; 496 497 if (this.firstChild == refChildNode) { 499 this.firstChild = newChildNode; 500 } 501 502 ++numChildren; 503 return newChildNode; 504 } 505 506 519 public Node replaceChild(Node newChild, 520 Node oldChild) { 521 if (newChild == null) { 522 throw new IllegalArgumentException ("newChild == null!"); 523 } 524 525 checkNode(newChild); 526 checkNode(oldChild); 527 528 IIOMetadataNode newChildNode = (IIOMetadataNode )newChild; 529 IIOMetadataNode oldChildNode = (IIOMetadataNode )oldChild; 530 531 IIOMetadataNode previous = oldChildNode.previousSibling; 532 IIOMetadataNode next = oldChildNode.nextSibling; 533 534 if (previous != null) { 535 previous.nextSibling = newChildNode; 536 } 537 if (next != null) { 538 next.previousSibling = newChildNode; 539 } 540 541 newChildNode.parent = this; 542 newChildNode.previousSibling = previous; 543 newChildNode.nextSibling = next; 544 545 if (firstChild == oldChildNode) { 546 firstChild = newChildNode; 547 } 548 if (lastChild == oldChildNode) { 549 lastChild = newChildNode; 550 } 551 552 oldChildNode.parent = null; 553 oldChildNode.previousSibling = null; 554 oldChildNode.nextSibling = null; 555 556 return oldChildNode; 557 } 558 559 570 public Node removeChild(Node oldChild) { 571 if (oldChild == null) { 572 throw new IllegalArgumentException ("oldChild == null!"); 573 } 574 checkNode(oldChild); 575 576 IIOMetadataNode oldChildNode = (IIOMetadataNode )oldChild; 577 578 IIOMetadataNode previous = oldChildNode.previousSibling; 579 IIOMetadataNode next = oldChildNode.nextSibling; 580 581 if (previous != null) { 582 previous.nextSibling = next; 583 } 584 if (next != null) { 585 next.previousSibling = previous; 586 } 587 588 if (this.firstChild == oldChildNode) { 589 this.firstChild = next; 590 } 591 if (this.lastChild == oldChildNode) { 592 this.lastChild = previous; 593 } 594 595 oldChildNode.parent = null; 596 oldChildNode.previousSibling = null; 597 oldChildNode.nextSibling = null; 598 599 --numChildren; 600 return oldChildNode; 601 } 602 603 614 public Node appendChild(Node newChild) { 615 if (newChild == null) { 616 throw new IllegalArgumentException ("newChild == null!"); 617 } 618 checkNode(newChild); 619 620 return insertBefore(newChild, null); 622 } 623 624 629 public boolean hasChildNodes() { 630 return numChildren > 0; 631 } 632 633 647 public Node cloneNode(boolean deep) { 648 IIOMetadataNode newNode = new IIOMetadataNode (this.nodeName); 649 newNode.setUserObject(getUserObject()); 650 652 if (deep) { 653 for (IIOMetadataNode child = firstChild; 654 child != null; 655 child = child.nextSibling) { 656 newNode.appendChild(child.cloneNode(true)); 657 } 658 } 659 660 return newNode; 661 } 662 663 667 public void normalize() { 668 } 669 670 679 public boolean isSupported(String feature, String version) { 680 return false; 681 } 682 683 686 public String getNamespaceURI() throws DOMException { 687 return null; 688 } 689 690 697 public String getPrefix() { 698 return null; 699 } 700 701 708 public void setPrefix(String prefix) { 709 } 710 711 716 public String getLocalName() { 717 return nodeName; 718 } 719 720 722 public String getTagName() { 723 return nodeName; 724 } 725 726 public String getAttribute(String name) { 727 Attr attr = getAttributeNode(name); 728 if (attr == null) { 729 return ""; 730 } 731 return attr.getValue(); 732 } 733 734 739 public String getAttributeNS(String namespaceURI, String localName) { 740 return getAttribute(localName); 741 } 742 743 public void setAttribute(String name, String value) { 744 if (!com.sun.imageio.metadata.XmlNames.isName(name)) { 747 throw new IIODOMException(DOMException.INVALID_CHARACTER_ERR, 748 "Attribute name is illegal!"); 749 } 750 removeAttribute(name, false); 751 attributes.add(new IIOAttr(this, name, value)); 752 } 753 754 759 public void setAttributeNS(String namespaceURI, 760 String qualifiedName, String value) { 761 setAttribute(qualifiedName, value); 762 } 763 764 public void removeAttribute(String name) { 765 removeAttribute(name, true); 766 } 767 768 private void removeAttribute(String name, boolean checkPresent) { 769 int numAttributes = attributes.size(); 770 for (int i = 0; i < numAttributes; i++) { 771 IIOAttr attr = (IIOAttr)attributes.get(i); 772 if (name.equals(attr.getName())) { 773 attr.setOwnerElement(null); 774 attributes.remove(i); 775 return; 776 } 777 } 778 779 if (checkPresent) { 781 throw new IIODOMException(DOMException.NOT_FOUND_ERR, 782 "No such attribute!"); 783 } 784 } 785 786 789 public void removeAttributeNS(String namespaceURI, 790 String localName) { 791 removeAttribute(localName); 792 } 793 794 public Attr getAttributeNode(String name) { 795 Node node = getAttributes().getNamedItem(name); 796 return (Attr )node; 797 } 798 799 804 public Attr getAttributeNodeNS(String namespaceURI, 805 String localName) { 806 return getAttributeNode(localName); 807 } 808 809 public Attr setAttributeNode(Attr newAttr) throws DOMException { 810 Element owner = newAttr.getOwnerElement(); 811 if (owner != null) { 812 if (owner == this) { 813 return null; 814 } else { 815 throw new DOMException (DOMException.INUSE_ATTRIBUTE_ERR, 816 "Attribute is already in use"); 817 } 818 } 819 820 IIOAttr attr; 821 if (newAttr instanceof IIOAttr) { 822 attr = (IIOAttr)newAttr; 823 attr.setOwnerElement(this); 824 } else { 825 attr = new IIOAttr(this, 826 newAttr.getName(), 827 newAttr.getValue()); 828 } 829 830 Attr oldAttr = getAttributeNode(attr.getName()); 831 if (oldAttr != null) { 832 removeAttributeNode(oldAttr); 833 } 834 835 attributes.add(attr); 836 837 return oldAttr; 838 } 839 840 845 public Attr setAttributeNodeNS(Attr newAttr) { 846 return setAttributeNode(newAttr); 847 } 848 849 public Attr removeAttributeNode(Attr oldAttr) { 850 removeAttribute(oldAttr.getName()); 851 return oldAttr; 852 } 853 854 public NodeList getElementsByTagName(String name) { 855 List l = new ArrayList (); 856 getElementsByTagName(name, l); 857 return new IIONodeList(l); 858 } 859 860 private void getElementsByTagName(String name, List l) { 861 if (nodeName.equals(name)) { 862 l.add(this); 863 } 864 865 Node child = getFirstChild(); 866 while (child != null) { 867 ((IIOMetadataNode )child).getElementsByTagName(name, l); 868 child = child.getNextSibling(); 869 } 870 } 871 872 875 public NodeList getElementsByTagNameNS(String namespaceURI, 876 String localName) { 877 return getElementsByTagName(localName); 878 } 879 880 public boolean hasAttributes() { 881 return attributes.size() > 0; 882 } 883 884 public boolean hasAttribute(String name) { 885 return getAttributeNode(name) != null; 886 } 887 888 891 public boolean hasAttributeNS(String namespaceURI, 892 String localName) { 893 return hasAttribute(localName); 894 } 895 896 898 public int getLength() { 899 return numChildren; 900 } 901 902 public Node item(int index) { 903 if (index < 0) { 904 return null; 905 } 906 907 Node child = getFirstChild(); 908 while (child != null && index-- > 0) { 909 child = child.getNextSibling(); 910 } 911 return child; 912 } 913 914 921 public Object getUserObject() { 922 return userObject; 923 } 924 925 932 public void setUserObject(Object userObject) { 933 this.userObject = userObject; 934 } 935 936 public void setIdAttribute(String name, 938 boolean isId) 939 throws DOMException { 940 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 941 } 942 943 public void setIdAttributeNS(String namespaceURI, 944 String localName, 945 boolean isId) 946 throws DOMException { 947 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 948 } 949 950 public void setIdAttributeNode(Attr idAttr, 951 boolean isId) 952 throws DOMException { 953 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 954 } 955 956 public TypeInfo getSchemaTypeInfo() { 957 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 958 } 959 960 public Object setUserData(String key, 961 Object data, 962 UserDataHandler handler) { 963 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 964 } 965 966 967 public Object getUserData ( String key ) { 968 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 969 } 970 public Object getFeature ( String feature, String version ) { 971 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 972 } 973 974 public boolean isSameNode( Node node ) { 975 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 976 } 977 978 public boolean isEqualNode( Node node ) { 979 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 980 } 981 public String lookupNamespaceURI( String prefix ) { 982 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 983 } 984 public boolean isDefaultNamespace(String namespaceURI) { 985 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 986 } 987 988 public String lookupPrefix(String namespaceURI) { 989 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 990 } 991 String textContent; 992 public String getTextContent() throws DOMException { 993 return textContent; 994 } 995 public void setTextContent(String textContent) throws DOMException { 996 this.textContent = textContent; } 998 999 1000 public short compareDocumentPosition(Node other) 1001 throws DOMException { 1002 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 1003 } 1004 public String getBaseURI() { 1005 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Method not supported"); 1006 } 1007 1009 1010} 1011 | Popular Tags |