KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > ElementImpl


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

7 package org.enhydra.xml;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.HashMap 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.DOMException JavaDoc;
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element 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  * @author Tweety
26  *
27  * A class representing a node in a meta-data tree, which implements
28  * the <a HREF="../../../../api/org/w3c/dom/Element.html">
29  *
30  * <p> Namespaces are ignored in this implementation. The terms "tag
31  * name" and "node name" are always considered to be synonymous.
32  *
33  * @version 1.0
34  */

35 public class ElementImpl extends NodeImpl implements Element JavaDoc {
36
37     /**
38      * A <code>HashMap</code> of <code>AttrImpl</code> nodes representing
39      * attributes.
40      */

41     protected HashMap JavaDoc attributes = null;
42
43
44     /**
45      * Constructs an empty <code>ElementImpl</code>.
46      */

47     public ElementImpl() {
48         attributes = new HashMap JavaDoc();
49         type = ELEMENT_NODE;
50     }
51
52
53     /**
54      * Constructs a <code>ElementImpl</code> from the given node,
55      * without creating entire children subtree.
56      *
57      * @param element, as a <code>ElementImpl</code>.
58      */

59     public ElementImpl(ElementImpl element) {
60         super(element);
61         attributes = element.attributes;
62         type = ELEMENT_NODE;
63     }
64
65
66     /**
67      * Constructs an <code>ElementImpl</code> with the given
68      * document owner and node name.
69      *
70      * @param ownerDoc the document owner of the node, as a <code>Document</code>.
71      * @param nodeName the name of the node, as a <code>String</code>.
72      */

73     public ElementImpl(Document JavaDoc ownerDoc, String JavaDoc name) {
74         super(ownerDoc,name,ELEMENT_NODE);
75         this.attributes = new HashMap JavaDoc();
76     }
77
78
79     /**
80      * Constructs an <code>ElementImpl</code> with the given
81      * document owner, node name, node type and node value.
82      *
83      * @param ownerDoc the document owner of the node, as a <code>Document</code>.
84      * @param nodeName the name of the node, as a <code>String</code>.
85      * @param type the type of the node, as a <code>short</code>.
86      * @param value the value of the node, as a <code>String</code>.
87      */

88     protected ElementImpl(Document JavaDoc ownerDoc, String JavaDoc nodeName, short type, String JavaDoc value) {
89         super(ownerDoc, nodeName, type, value);
90     }
91
92
93     /**
94      * Constructs an <code>ElementImpl</code> from a given node (creates the children subtree too),
95      * as a <code>Node</code>
96      *
97      * @param node, as a <code>Node</code>.
98      */

99     public ElementImpl(Node JavaDoc node) {
100         this(node,true);
101     }
102
103
104     /**
105      * Constructs an <code>ElementImpl</code> from a given node, as a <code>Node</code>,
106      * and deep as <code>boolean</code>.
107      *
108      * @param node, as a <code>Node</code>.
109      * @param deep if <code>true</code>, recursively clone the subtree
110      * under the specified node; if <code>false</code>, clone only the
111      * node itself.
112      */

113     public ElementImpl(Node JavaDoc node, boolean deep) {
114         super(node,false);
115         attributes = new HashMap JavaDoc();
116         NamedNodeMap JavaDoc attrs = node.getAttributes();
117         if (attrs != null) {
118             for (int i = 0; i < attrs.getLength(); i++) {
119                 Attr JavaDoc attr = new AttrImpl((Attr JavaDoc) attrs.item(i));
120                 attributes.put(attr.getName(), attr);
121             }
122         }
123         if (deep)
124             initNodeImplChildren(node);
125     }
126
127
128     /**
129      * Creates new instance of <code>ElementImpl</code> from a given document
130      * as a <code>Document</code>.
131      *
132      * @param document document.
133      *
134      * @return new <code>Element</code> node as a root of the <code>Document</code>.
135      */

136     public static Element JavaDoc newInstance(Document JavaDoc document) {
137         Node JavaDoc root = document.getDocumentElement();
138         return new ElementImpl(root);
139     }
140
141
142     /**
143      * Inserts the node <code>newChild</code> before the existing
144      * child node <code>refChild</code>. If <code>refChild</code> is
145      * <code>null</code>, insert <code>newChild</code> at the end of
146      * the list of children.
147      *
148      * @param newChild the <code>Node</code> to insert.
149      * @param refChild the reference <code>Node</code>.
150      *
151      * @return the node being inserted.
152      *
153      * @exception IllegalArgumentException if <code>newChild</code> is
154      * <code>null</code>.
155      */

156     public Node JavaDoc insertBefore(Node JavaDoc newChild, Node JavaDoc refChild) {
157         super.insertBefore(newChild,refChild);
158         return newChild;
159     }
160
161
162     /**
163      * Replaces the child node <code>oldChild</code> with
164      * <code>newChild</code> in the list of children, and returns the
165      * <code>oldChild</code> node.
166      *
167      * @param newChild the <code>Node</code> to insert.
168      * @param oldChild the <code>Node</code> to be replaced.
169      *
170      * @return the node replaced.
171      *
172      * @exception IllegalArgumentException if <code>newChild</code> is
173      * <code>null</code>.
174      */

175     public Node JavaDoc replaceChild(Node JavaDoc newChild, Node JavaDoc oldChild) {
176         super.replaceChild(newChild,oldChild);
177         return oldChild;
178     }
179
180
181     /**
182      * Removes the child node indicated by <code>oldChild</code> from
183      * the list of children, and returns it.
184      *
185      * @param oldChild the <code>Node</code> to be removed.
186      *
187      * @return the node removed.
188      *
189      * @exception IllegalArgumentException if <code>oldChild</code> is
190      * <code>null</code>.
191      */

192     public Node JavaDoc removeChild(Node JavaDoc oldChild) {
193         super.removeChild(oldChild);
194         return oldChild;
195     }
196
197
198     /**
199      * Returns a duplicate of this node. The duplicate node has no
200      * parent (<code>getParentNode</code> returns <code>null</code>).
201      * If a shallow clone is being performed (<code>deep</code> is
202      * <code>false</code>), the new node will not have any children or
203      * siblings. If a deep clone is being performed, the new node
204      * will form the root of a complete cloned subtree.
205      *
206      * @param deep if <code>true</code>, recursively clone the subtree
207      * under the specified node; if <code>false</code>, clone only the
208      * node itself.
209      *
210      * @return the duplicate node.
211      */

212     public Node JavaDoc cloneNode(boolean deep) {
213         return new ElementImpl(this,deep);
214     }
215
216
217
218
219
220     // Methods from Element
221

222
223     /**
224      * Returns tag name of this node.
225      *
226      * @return tag name of this node as a <code>String</code>.
227      */

228     public String JavaDoc getTagName() {
229         return nodeName;
230     }
231
232
233     /**
234      * Returns all attribute nodes of this node.
235      *
236      * @return all attribute nodes of this node as a <code>NamedNodeMap</code>.
237      */

238     public NamedNodeMap JavaDoc getAttributes() {
239         return new HashMapNamedNodeMap(attributes);
240     }
241
242
243     /**
244      * Returns the value of the attribute with given name.
245      *
246      * @param name name of attribute.
247      *
248      * @return value of attribute.
249      */

250     public String JavaDoc getAttribute(String JavaDoc name) {
251         Attr JavaDoc attr = getAttributeNode(name);
252         if (attr == null) {
253             return "";
254         }
255         return attr.getValue();
256     }
257
258
259     /**
260      * Equivalent to <code>getAttribute(localName)</code>.
261      *
262      * @see #setAttributeNS
263      */

264     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
265         return getAttribute(localName);
266     }
267
268
269     /**
270      * To the <code>name</code> attribute set value to <code>value</code>.
271      *
272      * @param name attribute value.
273      * @param value new attribute value.
274      */

275     public void setAttribute(String JavaDoc name, String JavaDoc value) {
276         // Note minor dependency on Crimson package
277
// Steal the code if Crimson ever goes away
278
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     /**
288      * Equivalent to <code>setAttribute(qualifiedName, value)</code>.
289      *
290      * @see #getAttributeNS
291      */

292     public void setAttributeNS(String JavaDoc namespaceURI, String JavaDoc qualifiedName, String JavaDoc value) {
293         setAttribute(qualifiedName, value);
294     }
295
296
297     /**
298      * Removes attribute with the given name.
299      *
300      * @param name attribute name.
301      */

302     public void removeAttribute(String JavaDoc 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 JavaDoc name, boolean checkPresent) {
312         if (attributes.remove(name) != null)
313             return;
314         // If we get here, the attribute doesn't exist
315
if (checkPresent) {
316             throw new NodeDOMException(
317                 DOMException.NOT_FOUND_ERR,
318                 "No such attribute!");
319         }
320     }
321
322
323     /**
324      * Returns <code>true</code>, if this node has attributes, otherwise
325      * <code>false</code>.
326      *
327      * @return <code>true</code> if node has attributes, otherwise <code>false</code>..
328      */

329     public boolean hasAttributes() {
330         return attributes.size() > 0;
331     }
332
333
334     /**
335      * Returns <code>true</code>, if this node has attribute with given name,
336      * otherwise <code>false</code>.
337      *
338      * @return <code>true</code> if node has given attribute, otherwise <code>false</code>..
339      */

340     public boolean hasAttribute(String JavaDoc name) {
341         return getAttributeNode(name) != null;
342     }
343
344
345     /**
346      * Equivalent to <code>removeAttribute(localName)</code>.
347      */

348     public void removeAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
349         removeAttribute(localName);
350     }
351
352
353     /**
354      * Returns attribute value with given name of this node.
355      *
356      * @param name name of attribute.
357      *
358      * @return value of attribute.
359      */

360     public Attr JavaDoc getAttributeNode(String JavaDoc name) {
361         return (Attr JavaDoc) attributes.get(name);
362     }
363
364
365     /**
366      * Equivalent to <code>getAttributeNode(localName)</code>.
367      *
368      * @see #setAttributeNodeNS
369      */

370     public Attr JavaDoc getAttributeNodeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
371         return getAttributeNode(localName);
372     }
373
374
375     /**
376      * Add new attribute to this node.
377      *
378      * @param newAttr new attribute.
379      *
380      * @return new attribute as <code>AttrImpl</code>.
381      */

382     public Attr JavaDoc setAttributeNode(Attr JavaDoc newAttr) throws DOMException JavaDoc {
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     /**
395      * Equivalent to <code>setAttributeNode(newAttr)</code>.
396      *
397      * @see #getAttributeNodeNS
398      */

399     public Attr JavaDoc setAttributeNodeNS(Attr JavaDoc newAttr) {
400         return setAttributeNode(newAttr);
401     }
402
403
404     /**
405      * Remove attribute from this node.
406      *
407      * @param oldAttr attribute that will be removed.
408      *
409      * @return old attribute as <code>AttrImpl</code>.
410      */

411     public Attr JavaDoc removeAttributeNode(Attr JavaDoc oldAttr) {
412         removeAttribute(oldAttr.getName());
413         return oldAttr;
414     }
415
416
417     /**
418      * Equivalent to <code>hasAttribute(localName)</code>.
419      */

420     public boolean hasAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
421         return hasAttribute(localName);
422     }
423
424
425     /**
426      * Returns all <code>Element</code> nodes with given name,
427      * searching by all sub nodes from this node.
428      *
429      * @param name tag name.
430      *
431      * @return all <code>Element</code> vith given name as <code>NodeList</code>.
432      */

433     public NodeList JavaDoc getElementsByTagName(String JavaDoc name) {
434         List JavaDoc list = new ArrayList JavaDoc();
435         getElementsByTagName(name, list);
436         return new NodeListImpl(list);
437     }
438
439
440     private void getElementsByTagName(String JavaDoc name, List JavaDoc list) {
441         if (nodeName.equals(name)) {
442             list.add(this);
443         }
444
445         Node JavaDoc 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     /**
455      * Equivalent to <code>getElementsByTagName(localName)</code>.
456      */

457     public NodeList JavaDoc getElementsByTagNameNS(String JavaDoc namespaceURI, String JavaDoc localName) {
458         return getElementsByTagName(localName);
459     }
460
461
462     /**
463      * Returns <code>true</code> if this node has children nodes.
464      *
465      * @return <code>true</code> if this node has children.
466      */

467     public boolean hasElementChildNodes() {
468         Node JavaDoc 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     /**
479      * Method beginToString for this class writes the xml
480      * begining tag string and all attributes.
481      *
482      * @param sb string buffer to add resulting string.
483      * @param indent used in formating the output.
484      */

485     protected void beginToString(StringBuffer JavaDoc sb, Indent indent) {
486         sb.append("\n" + indent + "<" + this.nodeName);
487
488         for (Iterator JavaDoc iter = attributes.values().iterator(); iter.hasNext();) {
489             Attr JavaDoc attr = (Attr JavaDoc) 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     /**
501      * Method endToString for this class writes the xml
502      * ending tag string.
503      *
504      * @param sb string buffer to add resulting string.
505      * @param indent used in formating the output.
506      */

507     protected void endToString(StringBuffer JavaDoc 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     /* (non-Javadoc)
519      * @see org.w3c.dom.Element#getSchemaTypeInfo()
520      */

521     public TypeInfo JavaDoc getSchemaTypeInfo() {
522         // TODO Auto-generated method stub
523
return null;
524     }
525
526
527     /* (non-Javadoc)
528      * @see org.w3c.dom.Element#setIdAttribute(java.lang.String, boolean)
529      */

530     public void setIdAttribute(String JavaDoc arg0, boolean arg1) throws DOMException JavaDoc {
531         // TODO Auto-generated method stub
532

533     }
534
535
536     /* (non-Javadoc)
537      * @see org.w3c.dom.Element#setIdAttributeNS(java.lang.String, java.lang.String, boolean)
538      */

539     public void setIdAttributeNS(String JavaDoc arg0, String JavaDoc arg1, boolean arg2) throws DOMException JavaDoc {
540         // TODO Auto-generated method stub
541

542     }
543
544
545     /* (non-Javadoc)
546      * @see org.w3c.dom.Element#setIdAttributeNode(org.w3c.dom.Attr, boolean)
547      */

548     public void setIdAttributeNode(Attr JavaDoc arg0, boolean arg1) throws DOMException JavaDoc {
549         // TODO Auto-generated method stub
550

551     }
552
553
554     /* (non-Javadoc)
555      * @see org.w3c.dom.Node#getNamespaceURI()
556      */

557     public String JavaDoc getNamespaceURI() {
558         // TODO Auto-generated method stub
559
return null;
560     }
561
562
563     /* (non-Javadoc)
564      * @see org.w3c.dom.Node#getBaseURI()
565      */

566     public String JavaDoc getBaseURI() {
567         // TODO Auto-generated method stub
568
return null;
569     }
570
571
572     /* (non-Javadoc)
573      * @see org.w3c.dom.Node#compareDocumentPosition(org.w3c.dom.Node)
574      */

575     public short compareDocumentPosition(Node JavaDoc arg0) throws DOMException JavaDoc {
576         // TODO Auto-generated method stub
577
return 0;
578     }
579
580
581     /* (non-Javadoc)
582      * @see org.w3c.dom.Node#getTextContent()
583      */

584     public String JavaDoc getTextContent() throws DOMException JavaDoc {
585         // TODO Auto-generated method stub
586
return null;
587     }
588
589
590     /* (non-Javadoc)
591      * @see org.w3c.dom.Node#setTextContent(java.lang.String)
592      */

593     public void setTextContent(String JavaDoc arg0) throws DOMException JavaDoc {
594         // TODO Auto-generated method stub
595

596     }
597
598
599     /* (non-Javadoc)
600      * @see org.w3c.dom.Node#isSameNode(org.w3c.dom.Node)
601      */

602     public boolean isSameNode(Node JavaDoc arg0) {
603         // TODO Auto-generated method stub
604
return false;
605     }
606
607
608     /* (non-Javadoc)
609      * @see org.w3c.dom.Node#lookupPrefix(java.lang.String)
610      */

611     public String JavaDoc lookupPrefix(String JavaDoc arg0) {
612         // TODO Auto-generated method stub
613
return null;
614     }
615
616
617     /* (non-Javadoc)
618      * @see org.w3c.dom.Node#isDefaultNamespace(java.lang.String)
619      */

620     public boolean isDefaultNamespace(String JavaDoc arg0) {
621         // TODO Auto-generated method stub
622
return false;
623     }
624
625
626     /* (non-Javadoc)
627      * @see org.w3c.dom.Node#lookupNamespaceURI(java.lang.String)
628      */

629     public String JavaDoc lookupNamespaceURI(String JavaDoc arg0) {
630         // TODO Auto-generated method stub
631
return null;
632     }
633
634
635     /* (non-Javadoc)
636      * @see org.w3c.dom.Node#isEqualNode(org.w3c.dom.Node)
637      */

638     public boolean isEqualNode(Node JavaDoc arg0) {
639         // TODO Auto-generated method stub
640
return false;
641     }
642
643
644     /* (non-Javadoc)
645      * @see org.w3c.dom.Node#getFeature(java.lang.String, java.lang.String)
646      */

647     public Object JavaDoc getFeature(String JavaDoc arg0, String JavaDoc arg1) {
648         // TODO Auto-generated method stub
649
return null;
650     }
651
652
653     /* (non-Javadoc)
654      * @see org.w3c.dom.Node#setUserData(java.lang.String, java.lang.Object, org.w3c.dom.UserDataHandler)
655      */

656     public Object JavaDoc setUserData(String JavaDoc arg0, Object JavaDoc arg1, UserDataHandler JavaDoc arg2) {
657         // TODO Auto-generated method stub
658
return null;
659     }
660
661
662     /* (non-Javadoc)
663      * @see org.w3c.dom.Node#getUserData(java.lang.String)
664      */

665     public Object JavaDoc getUserData(String JavaDoc arg0) {
666         // TODO Auto-generated method stub
667
return null;
668     }
669 }
670
Popular Tags