1 /*
2 Copyright (C) 2003 Together
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package org.enhydra.xml;
20
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.HashMap;
27 import java.util.HashMap;
28
29 import org.w3c.dom.Attr;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.DOMException;
33 import org.w3c.dom.NamedNodeMap;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36 import org.w3c.dom.TypeInfo;
37 import org.w3c.dom.UserDataHandler;
38
39 /***
40 * @author Tweety
41 *
42 * A class representing a node in a meta-data tree, which implements
43 * the <a href="../../../../api/org/w3c/dom/Element.html">
44 *
45 * <p> Namespaces are ignored in this implementation. The terms "tag
46 * name" and "node name" are always considered to be synonymous.
47 *
48 * @version 1.0
49 */
50 public class ElementImpl extends NodeImpl implements Element {
51
52 /***
53 * A <code>HashMap</code> of <code>AttrImpl</code> nodes representing
54 * attributes.
55 */
56 protected HashMap attributes = null;
57
58
59 /***
60 * Constructs an empty <code>ElementImpl</code>.
61 */
62 public ElementImpl() {
63 attributes = new HashMap();
64 type = ELEMENT_NODE;
65 }
66
67
68 /***
69 * Constructs a <code>ElementImpl</code> from the given node,
70 * without creating entire children subtree.
71 *
72 * @param element , as a <code>ElementImpl</code>.
73 */
74 public ElementImpl(ElementImpl element) {
75 super(element);
76 attributes = element.attributes;
77 type = ELEMENT_NODE;
78 }
79
80
81 /***
82 * Constructs an <code>ElementImpl</code> with the given
83 * document owner and node name.
84 *
85 * @param ownerDoc the document owner of the node, as a <code>Document</code>.
86 * @param name is the name of the node, as a <code>String</code>.
87 */
88 public ElementImpl(Document ownerDoc, String name) {
89 super(ownerDoc,name,ELEMENT_NODE);
90 this.attributes = new HashMap();
91 }
92
93
94 /***
95 * Constructs an <code>ElementImpl</code> with the given
96 * document owner, node name, node type and node value.
97 *
98 * @param ownerDoc the document owner of the node, as a <code>Document</code>.
99 * @param nodeName the name of the node, as a <code>String</code>.
100 * @param type the type of the node, as a <code>short</code>.
101 * @param value the value of the node, as a <code>String</code>.
102 */
103 protected ElementImpl(Document ownerDoc, String nodeName, short type, String value) {
104 super(ownerDoc, nodeName, type, value);
105 }
106
107
108 /***
109 * Constructs an <code>ElementImpl</code> from a given node (creates the children subtree too),
110 * as a <code>Node</code>
111 *
112 * @param node , as a <code>Node</code>.
113 */
114 public ElementImpl(Node node) {
115 this(node,true);
116 }
117
118
119 /***
120 * Constructs an <code>ElementImpl</code> from a given node, as a <code>Node</code>,
121 * and deep as <code>boolean</code>.
122 *
123 * @param node , as a <code>Node</code>.
124 * @param deep if <code>true</code>, recursively clone the subtree
125 * under the specified node; if <code>false</code>, clone only the
126 * node itself.
127 */
128 public ElementImpl(Node node, boolean deep) {
129 super(node,false);
130 attributes = new HashMap();
131 NamedNodeMap attrs = node.getAttributes();
132 if (attrs != null) {
133 for (int i = 0; i < attrs.getLength(); i++) {
134 Attr attr = new AttrImpl((Attr) attrs.item(i));
135 attributes.put(attr.getName(), attr);
136 }
137 }
138 if (deep)
139 initNodeImplChildren(node);
140 }
141
142
143 /***
144 * Creates new instance of <code>ElementImpl</code> from a given document
145 * as a <code>Document</code>.
146 *
147 * @param document document.
148 *
149 * @return new <code>Element</code> node as a root of the <code>Document</code>.
150 */
151 public static Element newInstance(Document document) {
152 Node root = document.getDocumentElement();
153 return new ElementImpl(root);
154 }
155
156
157 /***
158 * Inserts the node <code>newChild</code> before the existing
159 * child node <code>refChild</code>. If <code>refChild</code> is
160 * <code>null</code>, insert <code>newChild</code> at the end of
161 * the list of children.
162 *
163 * @param newChild the <code>Node</code> to insert.
164 * @param refChild the reference <code>Node</code>.
165 *
166 * @return the node being inserted.
167 *
168 * @exception IllegalArgumentException if <code>newChild</code> is
169 * <code>null</code>.
170 */
171 public Node insertBefore(Node newChild, Node refChild) {
172 super.insertBefore(newChild,refChild);
173 return newChild;
174 }
175
176
177 /***
178 * Replaces the child node <code>oldChild</code> with
179 * <code>newChild</code> in the list of children, and returns the
180 * <code>oldChild</code> node.
181 *
182 * @param newChild the <code>Node</code> to insert.
183 * @param oldChild the <code>Node</code> to be replaced.
184 *
185 * @return the node replaced.
186 *
187 * @exception IllegalArgumentException if <code>newChild</code> is
188 * <code>null</code>.
189 */
190 public Node replaceChild(Node newChild, Node oldChild) {
191 super.replaceChild(newChild,oldChild);
192 return oldChild;
193 }
194
195
196 /***
197 * Removes the child node indicated by <code>oldChild</code> from
198 * the list of children, and returns it.
199 *
200 * @param oldChild the <code>Node</code> to be removed.
201 *
202 * @return the node removed.
203 *
204 * @exception IllegalArgumentException if <code>oldChild</code> is
205 * <code>null</code>.
206 */
207 public Node removeChild(Node oldChild) {
208 super.removeChild(oldChild);
209 return oldChild;
210 }
211
212
213 /***
214 * Returns a duplicate of this node. The duplicate node has no
215 * parent (<code>getParentNode</code> returns <code>null</code>).
216 * If a shallow clone is being performed (<code>deep</code> is
217 * <code>false</code>), the new node will not have any children or
218 * siblings. If a deep clone is being performed, the new node
219 * will form the root of a complete cloned subtree.
220 *
221 * @param deep if <code>true</code>, recursively clone the subtree
222 * under the specified node; if <code>false</code>, clone only the
223 * node itself.
224 *
225 * @return the duplicate node.
226 */
227 public Node cloneNode(boolean deep) {
228 return new ElementImpl(this,deep);
229 }
230
231
232
233
234
235 // Methods from Element
236
237
238 /***
239 * Returns tag name of this node.
240 *
241 * @return tag name of this node as a <code>String</code>.
242 */
243 public String getTagName() {
244 return nodeName;
245 }
246
247
248 /***
249 * Returns all attribute nodes of this node.
250 *
251 * @return all attribute nodes of this node as a <code>NamedNodeMap</code>.
252 */
253 public NamedNodeMap getAttributes() {
254 return new HashMapNamedNodeMap(attributes);
255 }
256
257
258 /***
259 * Returns the value of the attribute with given name.
260 *
261 * @param name name of attribute.
262 *
263 * @return value of attribute.
264 */
265 public String getAttribute(String name) {
266 Attr attr = getAttributeNode(name);
267 if (attr == null) {
268 return "";
269 }
270 return attr.getValue();
271 }
272
273
274 /***
275 * Equivalent to <code>getAttribute(localName)</code>.
276 *
277 * @see #setAttributeNS
278 * @param namespaceURI is name space
279 * @param localName is string
280 * @return node
281 */
282 public String getAttributeNS(String namespaceURI, String localName) {
283 return getAttribute(localName);
284 }
285
286
287 /***
288 * To the <code>name</code> attribute set value to <code>value</code>.
289 *
290 * @param name attribute value.
291 * @param value new attribute value.
292 */
293 public void setAttribute(String name, String value) {
294 // Note minor dependency on Crimson package
295 // Steal the code if Crimson ever goes away
296
297 // Code below does not work with jdk1.3
298 /*
299 if (!org.apache.crimson.util.XmlNames.isName(name)) {
300 throw new NodeDOMException(
301 DOMException.INVALID_CHARACTER_ERR,
302 "Attribute name is illegal!");
303 }
304 */
305 attributes.put(name, new AttrImpl(this, name, value));
306 }
307
308
309 /***
310 * Equivalent to <code>setAttribute(qualifiedName, value)</code>.
311 *
312 * @see #getAttributeNS
313 * @param namespaceURI is name space of the node
314 * @param qualifiedName is string
315 * @param value is value of the node
316 */
317 public void setAttributeNS(String namespaceURI, String qualifiedName, String value) {
318 setAttribute(qualifiedName, value);
319 }
320
321
322 /***
323 * Removes attribute with the given name.
324 *
325 * @param name attribute name.
326 */
327 public void removeAttribute(String name) {
328 if (type != ELEMENT_NODE)
329 throw new NodeDOMException(
330 DOMException.NOT_SUPPORTED_ERR,
331 "Node doesn't have attributes");
332 removeAttribute(name, true);
333 }
334
335
336 private void removeAttribute(String name, boolean checkPresent) {
337 if (attributes.remove(name) != null)
338 return;
339 // If we get here, the attribute doesn't exist
340 if (checkPresent) {
341 throw new NodeDOMException(
342 DOMException.NOT_FOUND_ERR,
343 "No such attribute!");
344 }
345 }
346
347
348 /***
349 * Returns <code>true</code>, if this node has attributes, otherwise
350 * <code>false</code>.
351 *
352 * @return <code>true</code> if node has attributes, otherwise <code>false</code>..
353 */
354 public boolean hasAttributes() {
355 return attributes.size() > 0;
356 }
357
358
359 /***
360 * Returns <code>true</code>, if this node has attribute with given name,
361 * otherwise <code>false</code>.
362 *
363 * @return <code>true</code> if node has given attribute, otherwise <code>false</code>..
364 * @param name is name of the node
365 */
366 public boolean hasAttribute(String name) {
367 return getAttributeNode(name) != null;
368 }
369
370
371 /***
372 * Equivalent to <code>removeAttribute(localName)</code>.
373 * @param namespaceURI is name space of the node
374 * @param localName is name of the node
375 */
376 public void removeAttributeNS(String namespaceURI, String localName) {
377 removeAttribute(localName);
378 }
379
380
381 /***
382 * Returns attribute value with given name of this node.
383 *
384 * @param name name of attribute.
385 *
386 * @return value of attribute.
387 */
388 public Attr getAttributeNode(String name) {
389 return (Attr) attributes.get(name);
390 }
391
392
393 /***
394 * Equivalent to <code>getAttributeNode(localName)</code>.
395 *
396 * @see #setAttributeNodeNS
397 * @param namespaceURI is name space of the node
398 * @param localName is name of the node
399 * @return node
400 */
401 public Attr getAttributeNodeNS(String namespaceURI, String localName) {
402 return getAttributeNode(localName);
403 }
404
405
406 /***
407 * Add new attribute to this node.
408 *
409 * @param newAttr new attribute.
410 *
411 * @return new attribute as <code>AttrImpl</code>
412 * @throws DOMException
413 */
414 public Attr setAttributeNode(Attr newAttr) throws DOMException {
415 AttrImpl attr;
416 if (newAttr instanceof AttrImpl) {
417 attr = (AttrImpl) newAttr;
418 } else {
419 attr = new AttrImpl(newAttr);
420 }
421 attributes.put(attr.getName(), attr);
422 return attr;
423 }
424
425
426 /***
427 * Equivalent to <code>setAttributeNode(newAttr)</code>.
428 *
429 * @see #getAttributeNodeNS
430 * @param newAttr is attribute of the node
431 * @return node
432 */
433 public Attr setAttributeNodeNS(Attr newAttr) {
434 return setAttributeNode(newAttr);
435 }
436
437
438 /***
439 * Remove attribute from this node.
440 *
441 * @param oldAttr attribute that will be removed.
442 *
443 * @return old attribute as <code>AttrImpl</code>.
444 */
445 public Attr removeAttributeNode(Attr oldAttr) {
446 removeAttribute(oldAttr.getName());
447 return oldAttr;
448 }
449
450
451 /***
452 * Equivalent to <code>hasAttribute(localName)</code>.
453 * @param namespaceURI is name space of the node
454 * @param localName is name of the node
455 * @return node
456 */
457 public boolean hasAttributeNS(String namespaceURI, String localName) {
458 return hasAttribute(localName);
459 }
460
461
462 /***
463 * Returns all <code>Element</code> nodes with given name,
464 * searching by all sub nodes from this node.
465 *
466 * @param name tag name.
467 *
468 * @return all <code>Element</code> vith given name as <code>NodeList</code>.
469 */
470 public NodeList getElementsByTagName(String name) {
471 List list = new ArrayList();
472 getElementsByTagName(name, list);
473 return new NodeListImpl(list);
474 }
475
476
477 private void getElementsByTagName(String name, List list) {
478 if (nodeName.equals(name)) {
479 list.add(this);
480 }
481
482 Node child = getFirstChild();
483 while (child != null) {
484 if (child.getNodeType() == Node.ELEMENT_NODE)
485 ((ElementImpl)child).getElementsByTagName(name, list);
486 child = child.getNextSibling();
487 }
488 }
489
490
491 /***
492 * Equivalent to <code>getElementsByTagName(localName)</code>.
493 * @param namespaceURI is name space of the node
494 * @param localName is name of the node
495 * @return node
496 */
497 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
498 return getElementsByTagName(localName);
499 }
500
501
502 /***
503 * Returns <code>true</code> if this node has children nodes.
504 *
505 * @return <code>true</code> if this node has children.
506 */
507 public boolean hasElementChildNodes() {
508 Node child = getFirstChild();
509 while (child != null) {
510 if (child.getNodeType() == Node.ELEMENT_NODE)
511 return true;
512 child = child.getNextSibling();
513 }
514 return false;
515 }
516
517
518 /***
519 * Method beginToString for this class writes the xml
520 * begining tag string and all attributes.
521 *
522 * @param sb string buffer to add resulting string.
523 * @param indent used in formating the output.
524 */
525 protected void beginToString(StringBuffer sb, Indent indent) {
526 sb.append("\n" + indent + "<" + this.nodeName);
527
528 for (Iterator iter = attributes.values().iterator(); iter.hasNext();) {
529 Attr attr = (Attr) iter.next();
530 sb.append(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
531 }
532 // if (hasChildNodes()) {
533 sb.append(">");
534 indent.increment();
535 // } else
536 // sb.append("/>");
537 }
538
539 /***
540 * Check that the node is either <code>null</code> or an
541 * <code>NodeImpl</code>.
542 *
543 * @exception DOMException if node is not an instance of <code>NodeImpl</code>.
544 */
545 // protected void checkNode(Node node) throws DOMException {
546 // if (node == null) {
547 // return;
548 // }
549 // if (!(node instanceof ElementImpl))
550 // throw new NodeDOMException(DOMException.WRONG_DOCUMENT_ERR, "Node is not an instance of ElementImpl!");
551 // }
552
553 /***
554 * Method endToString for this class writes the xml
555 * ending tag string.
556 *
557 * @param sb string buffer to add resulting string.
558 * @param indent used in formating the output.
559 */
560 protected void endToString(StringBuffer sb, Indent indent) {
561 // if (hasChildNodes()) {
562 indent.decrement();
563 if (hasElementChildNodes())
564 sb.append("\n" + indent + "</" + this.nodeName + ">");
565 else
566 sb.append("</" + this.nodeName + ">");
567 // }
568 }
569
570
571 /* METHODS FROM INTERFACE IN JDK1.5 */
572
573
574 public TypeInfo getSchemaTypeInfo() {
575 // TODO Auto-generated method stub
576 return null;
577 }
578 public void setIdAttribute(String name, boolean isId) throws DOMException {
579 // TODO Auto-generated method stub
580
581 }
582 public void setIdAttributeNode(Attr idAttr, boolean isId)
583 throws DOMException {
584 // TODO Auto-generated method stub
585
586 }
587 public void setIdAttributeNS(String namespaceURI, String localName,
588 boolean isId) throws DOMException {
589 // TODO Auto-generated method stub
590
591 }
592 public Node appendChild(Node newChild) {
593 // TODO Auto-generated method stub
594 return super.appendChild(newChild);
595 }
596 protected void checkNode(Node node) throws DOMException {
597 // TODO Auto-generated method stub
598 super.checkNode(node);
599 }
600 public short compareDocumentPosition(Node other) throws DOMException {
601 // TODO Auto-generated method stub
602 return super.compareDocumentPosition(other);
603 }
604 public String getBaseURI() {
605 // TODO Auto-generated method stub
606 return super.getBaseURI();
607 }
608 public NodeList getChildNodes() {
609 // TODO Auto-generated method stub
610 return super.getChildNodes();
611 }
612 public Object getFeature(String feature, String version) {
613 // TODO Auto-generated method stub
614 return super.getFeature(feature, version);
615 }
616 public Node getFirstChild() {
617 // TODO Auto-generated method stub
618 return super.getFirstChild();
619 }
620 public Node getLastChild() {
621 // TODO Auto-generated method stub
622 return super.getLastChild();
623 }
624 public int getLength() {
625 // TODO Auto-generated method stub
626 return super.getLength();
627 }
628 public String getLocalName() {
629 // TODO Auto-generated method stub
630 return super.getLocalName();
631 }
632 public String getNamespaceURI() throws DOMException {
633 // TODO Auto-generated method stub
634 return super.getNamespaceURI();
635 }
636 public Node getNextSibling() {
637 // TODO Auto-generated method stub
638 return super.getNextSibling();
639 }
640 public String getNodeName() {
641 // TODO Auto-generated method stub
642 return super.getNodeName();
643 }
644 public short getNodeType() {
645 // TODO Auto-generated method stub
646 return super.getNodeType();
647 }
648 public String getNodeValue() {
649 // TODO Auto-generated method stub
650 return super.getNodeValue();
651 }
652 public Document getOwnerDocument() {
653 // TODO Auto-generated method stub
654 return super.getOwnerDocument();
655 }
656 public Node getParentNode() {
657 // TODO Auto-generated method stub
658 return super.getParentNode();
659 }
660 public String getPrefix() {
661 // TODO Auto-generated method stub
662 return super.getPrefix();
663 }
664 public Node getPreviousSibling() {
665 // TODO Auto-generated method stub
666 return super.getPreviousSibling();
667 }
668 public String getTextContent() throws DOMException {
669 // TODO Auto-generated method stub
670 return super.getTextContent();
671 }
672 public Object getUserData(String key) {
673 // TODO Auto-generated method stub
674 return super.getUserData(key);
675 }
676 public boolean hasChildNodes() {
677 // TODO Auto-generated method stub
678 return super.hasChildNodes();
679 }
680 protected void initNodeImplChildren(Node node) {
681 // TODO Auto-generated method stub
682 super.initNodeImplChildren(node);
683 }
684 public boolean isDefaultNamespace(String namespaceURI) {
685 // TODO Auto-generated method stub
686 return super.isDefaultNamespace(namespaceURI);
687 }
688 public boolean isEqualNode(Node arg) {
689 // TODO Auto-generated method stub
690 return super.isEqualNode(arg);
691 }
692 public boolean isSameNode(Node other) {
693 // TODO Auto-generated method stub
694 return super.isSameNode(other);
695 }
696 public boolean isSupported(String feature, String version) {
697 // TODO Auto-generated method stub
698 return super.isSupported(feature, version);
699 }
700 public Node item(int index) {
701 // TODO Auto-generated method stub
702 return super.item(index);
703 }
704 public String lookupNamespaceURI(String prefix) {
705 // TODO Auto-generated method stub
706 return super.lookupNamespaceURI(prefix);
707 }
708 public String lookupPrefix(String namespaceURI) {
709 // TODO Auto-generated method stub
710 return super.lookupPrefix(namespaceURI);
711 }
712 protected Node newCommentInstance(Node node) {
713 // TODO Auto-generated method stub
714 return super.newCommentInstance(node);
715 }
716 protected Node newDefaultInstance(Node node) {
717 // TODO Auto-generated method stub
718 return super.newDefaultInstance(node);
719 }
720 protected Node newElementInstance(Node node) {
721 // TODO Auto-generated method stub
722 return super.newElementInstance(node);
723 }
724 protected Node newTextInstance(Node node) {
725 // TODO Auto-generated method stub
726 return super.newTextInstance(node);
727 }
728 public void normalize() {
729 // TODO Auto-generated method stub
730 super.normalize();
731 }
732 public void setNodeValue(String nodeValue) {
733 // TODO Auto-generated method stub
734 super.setNodeValue(nodeValue);
735 }
736 public void setPrefix(String prefix) {
737 // TODO Auto-generated method stub
738 super.setPrefix(prefix);
739 }
740 public void setTextContent(String textContent) throws DOMException {
741 // TODO Auto-generated method stub
742 super.setTextContent(textContent);
743 }
744 public Object setUserData(String key, Object data, UserDataHandler handler) {
745 // TODO Auto-generated method stub
746 return super.setUserData(key, data, handler);
747 }
748 public String toString() {
749 // TODO Auto-generated method stub
750 return super.toString();
751 }
752 public String toString(String tab) {
753 // TODO Auto-generated method stub
754 return super.toString(tab);
755 }
756 }
This page was automatically generated by Maven