KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > metadata > IIOMetadataNode


1 /*
2  * @(#)IIOMetadataNode.java 1.36 02/03/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.imageio.metadata;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.w3c.dom.Attr JavaDoc;
15 import org.w3c.dom.Document JavaDoc;
16 import org.w3c.dom.Element JavaDoc;
17 import org.w3c.dom.DOMException JavaDoc;
18 import org.w3c.dom.NamedNodeMap JavaDoc;
19 import org.w3c.dom.Node JavaDoc;
20 import org.w3c.dom.NodeList JavaDoc;
21 import org.w3c.dom.TypeInfo JavaDoc;
22 import org.w3c.dom.UserDataHandler JavaDoc;
23
24
25 class IIODOMException extends DOMException JavaDoc {
26
27     public IIODOMException(short code, String JavaDoc message) {
28         super(code, message);
29     }
30 }
31
32 class IIONamedNodeMap implements NamedNodeMap JavaDoc {
33
34     List JavaDoc nodes;
35
36     public IIONamedNodeMap(List JavaDoc nodes) {
37         this.nodes = nodes;
38     }
39
40     public int getLength() {
41         return nodes.size();
42     }
43
44     public Node JavaDoc getNamedItem(String JavaDoc name) {
45         Iterator JavaDoc iter = nodes.iterator();
46         while (iter.hasNext()) {
47             Node JavaDoc node = (Node JavaDoc)iter.next();
48             if (name.equals(node.getNodeName())) {
49                 return node;
50             }
51         }
52
53         return null;
54     }
55
56     public Node JavaDoc item(int index) {
57         Node JavaDoc node = (Node JavaDoc)nodes.get(index);
58         return node;
59     }
60
61     public Node JavaDoc removeNamedItem(java.lang.String JavaDoc name) {
62         throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR,
63                                "This NamedNodeMap is read-only!");
64     }
65
66     public Node JavaDoc setNamedItem(Node JavaDoc arg) {
67         throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR,
68                                "This NamedNodeMap is read-only!");
69     }
70
71     /**
72      * Equivalent to <code>getNamedItem(localName)</code>.
73      */

74     public Node JavaDoc getNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName) {
75         return getNamedItem(localName);
76     }
77
78     /**
79      * Equivalent to <code>setNamedItem(arg)</code>.
80      */

81     public Node JavaDoc setNamedItemNS(Node JavaDoc arg) {
82         return setNamedItem(arg);
83     }
84
85     /**
86      * Equivalent to <code>removeNamedItem(localName)</code>.
87      */

88     public Node JavaDoc removeNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName) {
89         return removeNamedItem(localName);
90     }
91 }
92
93 class IIONodeList implements NodeList JavaDoc {
94     
95     List JavaDoc nodes;
96
97     public IIONodeList(List JavaDoc nodes) {
98         this.nodes = nodes;
99     }
100
101     public int getLength() {
102         return nodes.size();
103     }
104     
105     public Node JavaDoc item(int index) {
106         if (index < 0 || index > nodes.size()) {
107             return null;
108         }
109         return (Node JavaDoc)nodes.get(index);
110     }
111 }
112
113 class IIOAttr extends IIOMetadataNode JavaDoc implements Attr JavaDoc {
114
115     boolean specified = true;
116
117     Element JavaDoc owner;
118     String JavaDoc name;
119     String JavaDoc value;
120
121     public IIOAttr(Element JavaDoc owner, String JavaDoc name, String JavaDoc value) {
122         this.owner = owner;
123         this.name = name;
124         this.value = value;
125     }
126
127     public String JavaDoc getName() {
128         return name;
129     }
130
131     public String JavaDoc 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 JavaDoc getValue() {
144         return value;
145     }
146
147     public String JavaDoc getNodeValue() {
148         return value;
149     }
150
151     public void setValue(String JavaDoc value) {
152         this.value = value;
153     }
154
155     public void setNodeValue(String JavaDoc value) {
156         this.value = value;
157     }
158
159     public Element JavaDoc getOwnerElement() {
160         return owner;
161     }
162
163     public void setOwnerElement(Element JavaDoc owner) {
164         this.owner = owner;
165     }
166
167     // Start of dummy methods for DOM L3. PENDING: Please revisit
168
public boolean isId( ) {
169         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
170     }
171     public TypeInfo JavaDoc getSchemaTypeInfo() {
172         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
173     }
174     public Object JavaDoc setUserData(String JavaDoc key,
175                               Object JavaDoc data,
176                               UserDataHandler JavaDoc handler) {
177         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
178     }
179
180
181     public Object JavaDoc getUserData ( String JavaDoc key ) {
182         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
183     }
184
185     public Object JavaDoc getFeature ( String JavaDoc feature, String JavaDoc version ) {
186         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
187     }
188     public boolean isEqualNode( Node JavaDoc node ) {
189         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
190     }
191     public boolean isSameNode( Node JavaDoc node ) {
192         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
193     }
194
195     public String JavaDoc lookupNamespaceURI( String JavaDoc prefix ) {
196         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
197     }
198     public boolean isDefaultNamespace(String JavaDoc namespaceURI) {
199         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
200     }
201
202     public String JavaDoc lookupPrefix(String JavaDoc namespaceURI) {
203         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
204     }
205
206
207     String JavaDoc textContent;
208     public String JavaDoc getTextContent() throws DOMException JavaDoc {
209         return textContent;
210     }
211     public void setTextContent(String JavaDoc textContent) throws DOMException JavaDoc{
212         this.textContent = textContent; //PENDING
213
}
214     public short compareDocumentPosition(Node JavaDoc other)
215                                          throws DOMException JavaDoc {
216         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
217     }
218
219     public String JavaDoc getBaseURI() {
220         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
221     }
222     // End of dummy methods for DOM L3. PENDING: Please revisit
223

224
225 }
226
227 /**
228  * A class representing a node in a meta-data tree, which implements
229  * the <a
230  * HREF="../../../../api/org/w3c/dom/Element.html">
231  * <code>org.w3c.dom.Element</code></a> interface and additionally allows
232  * for the storage of non-textual objects via the
233  * <code>getUserObject</code> and <code>setUserObject</code> methods.
234  *
235  * <p> This class is not intended to be used for general XML
236  * processing. In particular, <code>Element</code> nodes created
237  * within the Image I/O API are not compatible with those created by
238  * Sun's standard implementation of the <code>org.w3.dom</code> API.
239  * In particular, the implementation is tuned for simple uses and may
240  * not perform well for intensive processing.
241  *
242  * <p> Namespaces are ignored in this implementation. The terms "tag
243  * name" and "node name" are always considered to be synonymous.
244  *
245  * @see IIOMetadata#getAsTree
246  * @see IIOMetadata#setFromTree
247  * @see IIOMetadata#mergeTree
248  *
249  * @version 0.5
250  */

251 public class IIOMetadataNode implements Element JavaDoc, NodeList JavaDoc {
252
253     /**
254      * The name of the node as a <code>String</code>.
255      */

256     private String JavaDoc nodeName = null;
257
258     /**
259      * The value of the node as a <code>String</code>. The Image I/O
260      * API typically does not make use of the node value.
261      */

262     private String JavaDoc nodeValue = null;
263
264     /**
265      * The <code>Object</code> value associated with this node.
266      */

267     private Object JavaDoc userObject = null;
268
269     /**
270      * The parent node of this node, or <code>null</code> if this node
271      * forms the root of its own tree.
272      */

273     private IIOMetadataNode JavaDoc parent = null;
274
275     /**
276      * The number of child nodes.
277      */

278     private int numChildren = 0;
279
280     /**
281      * The first (leftmost) child node of this node, or
282      * <code>null</code> if this node is a leaf node.
283      */

284     private IIOMetadataNode JavaDoc firstChild = null;
285
286     /**
287      * The last (rightmost) child node of this node, or
288      * <code>null</code> if this node is a leaf node.
289      */

290     private IIOMetadataNode JavaDoc lastChild = null;
291     
292     /**
293      * The next (right) sibling node of this node, or
294      * <code>null</code> if this node is its parent's last child node.
295      */

296     private IIOMetadataNode JavaDoc nextSibling = null;
297
298     /**
299      * The previous (left) sibling node of this node, or
300      * <code>null</code> if this node is its parent's first child node.
301      */

302     private IIOMetadataNode JavaDoc previousSibling = null;
303
304     /**
305      * A <code>List</code> of <code>IIOAttr</code> nodes representing
306      * attributes.
307      */

308     private List JavaDoc attributes = new ArrayList JavaDoc();
309
310     /**
311      * Constructs an empty <code>IIOMetadataNode</code>.
312      */

313     public IIOMetadataNode() {}
314
315     /**
316      * Constructs an <code>IIOMetadataNode</code> with a given node
317      * name.
318      *
319      * @param nodeName the name of the node, as a <code>String</code>.
320      */

321     public IIOMetadataNode(String JavaDoc nodeName) {
322         this.nodeName = nodeName;
323     }
324
325     /**
326      * Check that the node is either <code>null</code> or an
327      * <code>IIOMetadataNode</code>.
328      */

329     private void checkNode(Node JavaDoc node) throws DOMException JavaDoc {
330         if (node == null) {
331             return;
332         }
333         if (!(node instanceof IIOMetadataNode JavaDoc)) {
334             throw new IIODOMException(DOMException.WRONG_DOCUMENT_ERR,
335                                       "Node not an IIOMetadataNode!");
336         }
337     }
338
339     // Methods from Node
340

341     /**
342      * Returns the node name associated with this node.
343      *
344      * @return the node name, as a <code>String</code>.
345      */

346     public String JavaDoc getNodeName() {
347         return nodeName;
348     }
349
350     public String JavaDoc getNodeValue() throws DOMException JavaDoc {
351         return nodeValue;
352     }
353
354     public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc {
355         this.nodeValue = nodeValue;
356     }
357
358     /**
359      * Returns the node type, which is always
360      * <code>ELEMENT_NODE</code>.
361      *
362      * @return the <code>short</code> value <code>ELEMENT_NODE</code>.
363      */

364     public short getNodeType() {
365         return ELEMENT_NODE;
366     }
367     
368     /**
369      * Returns the parent of this node. A <code>null</code> value
370      * indicates that the node is the root of its own tree. To add a
371      * node to an existing tree, use one of the
372      * <code>insertBefore</code>, <code>replaceChild</code>, or
373      * <code>appendChild</code> methods.
374      *
375      * @return the parent, as a <code>Node</code>.
376      *
377      * @see #insertBefore
378      * @see #replaceChild
379      * @see #appendChild
380      */

381     public Node JavaDoc getParentNode() {
382         return parent;
383     }
384
385     public NodeList JavaDoc getChildNodes() {
386         return this;
387     }
388
389     /**
390      * Returns the first child of this node, or <code>null</code> if
391      * the node has no children.
392      *
393      * @return the first child, as a <code>Node</code>, or
394      * <code>null</code>
395      */

396     public Node JavaDoc getFirstChild() {
397         return firstChild;
398     }
399
400     /**
401      * Returns the last child of this node, or <code>null</code> if
402      * the node has no children.
403      *
404      * @return the last child, as a <code>Node</code>, or
405      * <code>null</code>.
406      */

407     public Node JavaDoc getLastChild() {
408         return lastChild;
409     }
410
411     /**
412      * Returns the previous sibling of this node, or <code>null</code>
413      * if this node has no previous sibling.
414      *
415      * @return the previous sibling, as a <code>Node</code>, or
416      * <code>null</code>.
417      */

418     public Node JavaDoc getPreviousSibling() {
419         return previousSibling;
420     }
421
422     /**
423      * Returns the next sibling of this node, or <code>null</code> if
424      * the node has no next sibling.
425      *
426      * @return the next sibling, as a <code>Node</code>, or
427      * <code>null</code>.
428      */

429     public Node JavaDoc getNextSibling() {
430         return nextSibling;
431     }
432
433     public NamedNodeMap JavaDoc getAttributes() {
434         return new IIONamedNodeMap(attributes);
435     }
436
437     /**
438      * Returns <code>null</code>, since <code>IIOMetadataNode</code>s
439      * do not belong to any <code>Document</code>.
440      *
441      * @return <code>null</code>.
442      */

443     public Document JavaDoc getOwnerDocument() {
444         return null;
445     }
446
447     /**
448      * Inserts the node <code>newChild</code> before the existing
449      * child node <code>refChild</code>. If <code>refChild</code> is
450      * <code>null</code>, insert <code>newChild</code> at the end of
451      * the list of children.
452      *
453      * @param newChild the <code>Node</code> to insert.
454      * @param refChild the reference <code>Node</code>.
455      *
456      * @return the node being inserted.
457      *
458      * @exception IllegalArgumentException if <code>newChild</code> is
459      * <code>null</code>.
460      */

461     public Node JavaDoc insertBefore(Node JavaDoc newChild,
462                              Node JavaDoc refChild) {
463         if (newChild == null) {
464             throw new IllegalArgumentException JavaDoc("newChild == null!");
465         }
466
467         checkNode(newChild);
468         checkNode(refChild);
469
470         IIOMetadataNode JavaDoc newChildNode = (IIOMetadataNode JavaDoc)newChild;
471         IIOMetadataNode JavaDoc refChildNode = (IIOMetadataNode JavaDoc)refChild;
472
473         // Siblings, can be null.
474
IIOMetadataNode JavaDoc previous = null;
475         IIOMetadataNode JavaDoc 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         // N.B.: O.K. if refChild == null
498
if (this.firstChild == refChildNode) {
499             this.firstChild = newChildNode;
500         }
501
502         ++numChildren;
503         return newChildNode;
504     }
505
506     /**
507      * Replaces the child node <code>oldChild</code> with
508      * <code>newChild</code> in the list of children, and returns the
509      * <code>oldChild</code> node.
510      *
511      * @param newChild the <code>Node</code> to insert.
512      * @param oldChild the <code>Node</code> to be replaced.
513      *
514      * @return the node replaced.
515      *
516      * @exception IllegalArgumentException if <code>newChild</code> is
517      * <code>null</code>.
518      */

519     public Node JavaDoc replaceChild(Node JavaDoc newChild,
520                              Node JavaDoc oldChild) {
521         if (newChild == null) {
522             throw new IllegalArgumentException JavaDoc("newChild == null!");
523         }
524         
525         checkNode(newChild);
526         checkNode(oldChild);
527
528         IIOMetadataNode JavaDoc newChildNode = (IIOMetadataNode JavaDoc)newChild;
529         IIOMetadataNode JavaDoc oldChildNode = (IIOMetadataNode JavaDoc)oldChild;
530
531         IIOMetadataNode JavaDoc previous = oldChildNode.previousSibling;
532         IIOMetadataNode JavaDoc 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     /**
560      * Removes the child node indicated by <code>oldChild</code> from
561      * the list of children, and returns it.
562      *
563      * @param oldChild the <code>Node</code> to be removed.
564      *
565      * @return the node removed.
566      *
567      * @exception IllegalArgumentException if <code>oldChild</code> is
568      * <code>null</code>.
569      */

570     public Node JavaDoc removeChild(Node JavaDoc oldChild) {
571         if (oldChild == null) {
572             throw new IllegalArgumentException JavaDoc("oldChild == null!");
573         }
574         checkNode(oldChild);
575
576         IIOMetadataNode JavaDoc oldChildNode = (IIOMetadataNode JavaDoc)oldChild;
577
578         IIOMetadataNode JavaDoc previous = oldChildNode.previousSibling;
579         IIOMetadataNode JavaDoc 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     /**
604      * Adds the node <code>newChild</code> to the end of the list of
605      * children of this node.
606      *
607      * @param newChild the <code>Node</code> to insert.
608      *
609      * @return the node added.
610      *
611      * @exception IllegalArgumentException if <code>newChild</code> is
612      * <code>null</code>.
613      */

614     public Node JavaDoc appendChild(Node JavaDoc newChild) {
615         if (newChild == null) {
616             throw new IllegalArgumentException JavaDoc("newChild == null!");
617         }
618         checkNode(newChild);
619
620         // insertBefore will increment numChildren
621
return insertBefore(newChild, null);
622     }
623
624     /**
625      * Returns <code>true</code> if this node has child nodes.
626      *
627      * @return <code>true</code> if this node has children.
628      */

629     public boolean hasChildNodes() {
630         return numChildren > 0;
631     }
632
633     /**
634      * Returns a duplicate of this node. The duplicate node has no
635      * parent (<code>getParentNode</code> returns <code>null</code>).
636      * If a shallow clone is being performed (<code>deep</code> is
637      * <code>false</code>), the new node will not have any children or
638      * siblings. If a deep clone is being performed, the new node
639      * will form the root of a complete cloned subtree.
640      *
641      * @param deep if <code>true</code>, recursively clone the subtree
642      * under the specified node; if <code>false</code>, clone only the
643      * node itself.
644      *
645      * @return the duplicate node.
646      */

647     public Node JavaDoc cloneNode(boolean deep) {
648         IIOMetadataNode JavaDoc newNode = new IIOMetadataNode JavaDoc(this.nodeName);
649         newNode.setUserObject(getUserObject());
650         // Attributes
651

652         if (deep) {
653             for (IIOMetadataNode JavaDoc child = firstChild;
654                  child != null;
655                  child = child.nextSibling) {
656                 newNode.appendChild(child.cloneNode(true));
657             }
658         }
659         
660         return newNode;
661     }
662
663     /**
664      * Does nothing, since <code>IIOMetadataNode</code>s do not
665      * contain <code>Text</code> children.
666      */

667     public void normalize() {
668     }
669
670     /**
671      * Returns <code>false</code> since DOM features are not
672      * supported.
673      *
674      * @return <code>false</code>.
675      *
676      * @param feature a <code>String</code>, which is ignored.
677      * @param version a <code>String</code>, which is ignored.
678      */

679     public boolean isSupported(String JavaDoc feature, String JavaDoc version) {
680         return false;
681     }
682
683     /**
684      * Returns <code>null</code>, since namespaces are not supported.
685      */

686     public String JavaDoc getNamespaceURI() throws DOMException JavaDoc {
687         return null;
688     }
689
690     /**
691      * Returns <code>null</code>, since namespaces are not supported.
692      *
693      * @return <code>null</code>.
694      *
695      * @see #setPrefix
696      */

697     public String JavaDoc getPrefix() {
698         return null;
699     }
700
701     /**
702      * Does nothing, since namespaces are not supported.
703      *
704      * @param prefix a <code>String</code>, which is ignored.
705      *
706      * @see #getPrefix
707      */

708     public void setPrefix(String JavaDoc prefix) {
709     }
710
711     /**
712      * Equivalent to <code>getNodeName</code>.
713      *
714      * @return the node name, as a <code>String</code>.
715      */

716     public String JavaDoc getLocalName() {
717         return nodeName;
718     }
719
720     // Methods from Element
721

722     public String JavaDoc getTagName() {
723         return nodeName;
724     }
725     
726     public String JavaDoc getAttribute(String JavaDoc name) {
727         Attr JavaDoc attr = getAttributeNode(name);
728         if (attr == null) {
729             return "";
730         }
731         return attr.getValue();
732     }
733
734     /**
735      * Equivalent to <code>getAttribute(localName)</code>.
736      *
737      * @see #setAttributeNS
738      */

739     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
740         return getAttribute(localName);
741     }
742
743     public void setAttribute(String JavaDoc name, String JavaDoc value) {
744         // Note minor dependency on Crimson package
745
// Steal the code if Crimson ever goes away
746
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     /**
755      * Equivalent to <code>setAttribute(qualifiedName, value)</code>.
756      *
757      * @see #getAttributeNS
758      */

759     public void setAttributeNS(String JavaDoc namespaceURI,
760                                String JavaDoc qualifiedName, String JavaDoc value) {
761         setAttribute(qualifiedName, value);
762     }
763
764     public void removeAttribute(String JavaDoc name) {
765         removeAttribute(name, true);
766     }
767
768     private void removeAttribute(String JavaDoc 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 we get here, the attribute doesn't exist
780
if (checkPresent) {
781             throw new IIODOMException(DOMException.NOT_FOUND_ERR,
782                                       "No such attribute!");
783         }
784     }
785     
786     /**
787      * Equivalent to <code>removeAttribute(localName)</code>.
788      */

789     public void removeAttributeNS(String JavaDoc namespaceURI,
790                                   String JavaDoc localName) {
791         removeAttribute(localName);
792     }
793
794     public Attr JavaDoc getAttributeNode(String JavaDoc name) {
795         Node JavaDoc node = getAttributes().getNamedItem(name);
796         return (Attr JavaDoc)node;
797     }
798
799     /**
800      * Equivalent to <code>getAttributeNode(localName)</code>.
801      *
802      * @see #setAttributeNodeNS
803      */

804    public Attr JavaDoc getAttributeNodeNS(String JavaDoc namespaceURI,
805                                    String JavaDoc localName) {
806         return getAttributeNode(localName);
807     }
808
809     public Attr JavaDoc setAttributeNode(Attr JavaDoc newAttr) throws DOMException JavaDoc {
810         Element JavaDoc owner = newAttr.getOwnerElement();
811         if (owner != null) {
812             if (owner == this) {
813                 return null;
814             } else {
815                 throw new DOMException JavaDoc(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 JavaDoc oldAttr = getAttributeNode(attr.getName());
831         if (oldAttr != null) {
832             removeAttributeNode(oldAttr);
833         }
834
835         attributes.add(attr);
836
837         return oldAttr;
838     }
839     
840     /**
841      * Equivalent to <code>setAttributeNode(newAttr)</code>.
842      *
843      * @see #getAttributeNodeNS
844      */

845     public Attr JavaDoc setAttributeNodeNS(Attr JavaDoc newAttr) {
846         return setAttributeNode(newAttr);
847     }
848
849     public Attr JavaDoc removeAttributeNode(Attr JavaDoc oldAttr) {
850         removeAttribute(oldAttr.getName());
851         return oldAttr;
852     }
853      
854     public NodeList JavaDoc getElementsByTagName(String JavaDoc name) {
855         List JavaDoc l = new ArrayList JavaDoc();
856         getElementsByTagName(name, l);
857         return new IIONodeList(l);
858     }
859
860     private void getElementsByTagName(String JavaDoc name, List JavaDoc l) {
861         if (nodeName.equals(name)) {
862             l.add(this);
863         }
864
865         Node JavaDoc child = getFirstChild();
866         while (child != null) {
867             ((IIOMetadataNode JavaDoc)child).getElementsByTagName(name, l);
868             child = child.getNextSibling();
869         }
870     }
871     
872     /**
873      * Equivalent to <code>getElementsByTagName(localName)</code>.
874      */

875     public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespaceURI,
876                                            String JavaDoc localName) {
877         return getElementsByTagName(localName);
878     }
879
880     public boolean hasAttributes() {
881     return attributes.size() > 0;
882     }
883
884     public boolean hasAttribute(String JavaDoc name) {
885         return getAttributeNode(name) != null;
886     }
887
888     /**
889      * Equivalent to <code>hasAttribute(localName)</code>.
890      */

891     public boolean hasAttributeNS(String JavaDoc namespaceURI,
892                                   String JavaDoc localName) {
893         return hasAttribute(localName);
894     }
895
896     // Methods from NodeList
897

898     public int getLength() {
899         return numChildren;
900     }
901
902     public Node JavaDoc item(int index) {
903         if (index < 0) {
904             return null;
905         }
906
907         Node JavaDoc child = getFirstChild();
908         while (child != null && index-- > 0) {
909             child = child.getNextSibling();
910         }
911         return child;
912     }
913
914     /**
915      * Returns the <code>Object</code> value associated with this node.
916      *
917      * @return the user <code>Object</code>.
918      *
919      * @see #setUserObject
920      */

921     public Object JavaDoc getUserObject() {
922         return userObject;
923     }
924
925     /**
926      * Sets the value associated with this node.
927      *
928      * @param userObject the user <code>Object</code>.
929      *
930      * @see #getUserObject
931      */

932     public void setUserObject(Object JavaDoc userObject) {
933         this.userObject = userObject;
934     }
935
936     // Start of dummy methods for DOM L3. PENDING: Please revisit
937
public void setIdAttribute(String JavaDoc name,
938                                boolean isId)
939                                throws DOMException JavaDoc {
940         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
941     }
942
943     public void setIdAttributeNS(String JavaDoc namespaceURI,
944                                  String JavaDoc localName,
945                                  boolean isId)
946                                  throws DOMException JavaDoc {
947         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
948     }
949
950     public void setIdAttributeNode(Attr JavaDoc idAttr,
951                                    boolean isId)
952                                    throws DOMException JavaDoc {
953         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
954     }
955
956     public TypeInfo JavaDoc getSchemaTypeInfo() {
957         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
958     }
959
960     public Object JavaDoc setUserData(String JavaDoc key,
961                               Object JavaDoc data,
962                               UserDataHandler JavaDoc handler) {
963         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
964     }
965
966
967     public Object JavaDoc getUserData ( String JavaDoc key ) {
968         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
969     }
970     public Object JavaDoc getFeature ( String JavaDoc feature, String JavaDoc version ) {
971         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
972     }
973
974     public boolean isSameNode( Node JavaDoc node ) {
975         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
976     }
977
978     public boolean isEqualNode( Node JavaDoc node ) {
979         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
980     }
981     public String JavaDoc lookupNamespaceURI( String JavaDoc prefix ) {
982         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
983     }
984     public boolean isDefaultNamespace(String JavaDoc namespaceURI) {
985         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
986     }
987
988     public String JavaDoc lookupPrefix(String JavaDoc namespaceURI) {
989         throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
990     }
991     String JavaDoc textContent;
992     public String JavaDoc getTextContent() throws DOMException JavaDoc {
993         return textContent;
994     }
995     public void setTextContent(String JavaDoc textContent) throws DOMException JavaDoc{
996         this.textContent = textContent; //PENDING
997
}
998
999
1000    public short compareDocumentPosition(Node JavaDoc other)
1001                                         throws DOMException JavaDoc {
1002        throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
1003    }
1004    public String JavaDoc getBaseURI() {
1005        throw new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
1006    }
1007    //End of dummy methods for DOM L3. Please revisit
1008

1009
1010}
1011
Popular Tags