KickJava   Java API By Example, From Geeks To Geeks.

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


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.dom4j.Document;
6 import org.dom4j.Element;
7
8 import java.util.LinkedList JavaDoc;
9
10 public class Dom4JReader implements HierarchicalStreamReader {
11
12     private Element currentElement;
13     private LinkedList JavaDoc pointers = new LinkedList JavaDoc();
14
15     public Dom4JReader(Element rootElement) {
16         currentElement = rootElement;
17         pointers.addLast(new Pointer());
18     }
19
20     public Dom4JReader(Document document) {
21         currentElement = document.getRootElement();
22         pointers.addLast(new Pointer());
23     }
24
25     public String JavaDoc getNodeName() {
26         return currentElement.getName();
27     }
28
29     public String JavaDoc getValue() {
30         return currentElement.getText();
31     }
32
33     public String JavaDoc getAttribute(String JavaDoc name) {
34         return currentElement.attributeValue(name);
35     }
36
37     public Object JavaDoc peekUnderlyingNode() {
38         return currentElement;
39     }
40
41     private class Pointer {
42         public int v;
43     }
44
45     public boolean hasMoreChildren() {
46         Pointer pointer = (Pointer) pointers.getLast();
47
48         if (pointer.v < currentElement.elements().size()) {
49             return true;
50         } else {
51             return false;
52         }
53     }
54
55     public void moveUp() {
56         currentElement = currentElement.getParent();
57         pointers.removeLast();
58     }
59
60     public void moveDown() {
61         Pointer pointer = (Pointer) pointers.getLast();
62         pointers.addLast(new Pointer());
63
64         currentElement = (Element) currentElement.elements().get(pointer.v);
65
66         pointer.v++;
67     }
68
69     public void appendErrors(ErrorWriter errorWriter) {
70         errorWriter.add("xpath", currentElement.getPath());
71     }
72
73 }
74
Popular Tags