KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > io > xml > JDomReader


1 package com.thoughtworks.xstream.io.xml;
2
3 import org.jdom.Document;
4 import org.jdom.Element;
5 import org.jdom.Parent;
6 import org.jdom.Attribute;
7
8 /**
9  * @author Laurent Bihanic
10  */

11 public class JDomReader extends AbstractDocumentReader {
12
13     private Element currentElement;
14
15     public JDomReader(Element root) {
16         super(root);
17     }
18
19     public JDomReader(Document document) {
20         super(document.getRootElement());
21     }
22
23     protected void reassignCurrentElement(Object JavaDoc current) {
24         currentElement = (Element) current;
25     }
26
27     protected Object JavaDoc getParent() {
28         // JDOM 1.0:
29
return currentElement.getParentElement();
30
31         // JDOM b10:
32
// Parent parent = currentElement.getParent();
33
// return (parent instanceof Element) ? (Element)parent : null;
34

35         // JDOM b9 and earlier:
36
// return currentElement.getParent();
37
}
38
39     protected Object JavaDoc getChild(int index) {
40         return currentElement.getChildren().get(index);
41     }
42
43     protected int getChildCount() {
44         return currentElement.getChildren().size();
45     }
46
47     public String JavaDoc getNodeName() {
48         return currentElement.getName();
49     }
50
51     public String JavaDoc getValue() {
52         return currentElement.getText();
53     }
54
55     public String JavaDoc getAttribute(String JavaDoc name) {
56         return currentElement.getAttributeValue(name);
57     }
58
59     public String JavaDoc getAttribute(int index) {
60         return ((Attribute) currentElement.getAttributes().get(index)).getValue();
61     }
62
63     public int getAttributeCount() {
64         return currentElement.getAttributes().size();
65     }
66
67     public String JavaDoc getAttributeName(int index) {
68         return ((Attribute) currentElement.getAttributes().get(index)).getQualifiedName();
69     }
70 }
71
72
Popular Tags