KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.converters.ErrorWriter;
4 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
5 import org.w3c.dom.*;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.List JavaDoc;
10
11 public class DomReader implements HierarchicalStreamReader {
12
13     private Element currentElement;
14     private StringBuffer JavaDoc textBuffer;
15     private NodeList childNodes;
16     private LinkedList JavaDoc pointers = new LinkedList JavaDoc();
17     private List JavaDoc childElements;
18
19     public DomReader(Element rootElement) {
20         textBuffer = new StringBuffer JavaDoc(180);
21         pointers.addLast(new Pointer());
22         setCurrent(rootElement);
23     }
24
25     public DomReader(Document document) {
26         textBuffer = new StringBuffer JavaDoc(180);
27         pointers.addLast(new Pointer());
28         setCurrent(document.getDocumentElement());
29     }
30
31     public String JavaDoc getNodeName() {
32         return currentElement.getTagName();
33     }
34
35     public String JavaDoc getValue() {
36         NodeList childNodes = currentElement.getChildNodes();
37         textBuffer.setLength(0);
38         int length = childNodes.getLength();
39         for (int i = 0; i < length; i++) {
40             Node childNode = childNodes.item(i);
41             if (childNode instanceof Text) {
42                 Text text = (Text) childNode;
43                 textBuffer.append(text.getData());
44             }
45         }
46         return textBuffer.toString();
47     }
48
49     public String JavaDoc getAttribute(String JavaDoc name) {
50         Attr attribute = currentElement.getAttributeNode(name);
51         return attribute == null ? null : attribute.getValue();
52     }
53
54     public Object JavaDoc peekUnderlyingNode() {
55         return currentElement;
56     }
57
58     private void setCurrent(Object JavaDoc currentElementObj) {
59         this.currentElement = (Element) currentElementObj;
60         childNodes = currentElement.getChildNodes();
61         childElements = new ArrayList JavaDoc();
62         for (int i = 0; i < childNodes.getLength(); i++) {
63             Node node = childNodes.item(i);
64             if (node instanceof Element) {
65                 childElements.add(node);
66             }
67         }
68     }
69
70     private class Pointer {
71         public int v;
72     }
73
74     public boolean hasMoreChildren() {
75         Pointer pointer = (Pointer) pointers.getLast();
76
77         if (pointer.v < childElements.size()) {
78             return true;
79         } else {
80             return false;
81         }
82     }
83
84     public void moveUp() {
85         setCurrent(currentElement.getParentNode());
86         pointers.removeLast();
87     }
88
89     public void moveDown() {
90         Pointer pointer = (Pointer) pointers.getLast();
91         pointers.addLast(new Pointer());
92
93         setCurrent(childElements.get(pointer.v));
94
95         pointer.v++;
96
97     }
98
99     public void appendErrors(ErrorWriter errorWriter) {
100     }
101 }
102
Popular Tags