KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.io.xml;
2
3 import com.thoughtworks.xstream.converters.ErrorWriter;
4 import com.thoughtworks.xstream.core.util.FastStack;
5 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
6 import com.thoughtworks.xstream.io.AttributeNameIterator;
7
8 import java.util.Iterator JavaDoc;
9
10 public abstract class AbstractDocumentReader implements HierarchicalStreamReader {
11
12     private FastStack pointers = new FastStack(16);
13     private Object JavaDoc current;
14
15     protected AbstractDocumentReader(Object JavaDoc rootElement) {
16         this.current = rootElement;
17         pointers.push(new Pointer());
18         reassignCurrentElement(current);
19     }
20
21     protected abstract void reassignCurrentElement(Object JavaDoc current);
22     protected abstract Object JavaDoc getParent();
23     protected abstract Object JavaDoc getChild(int index);
24     protected abstract int getChildCount();
25
26     private static class Pointer {
27         public int v;
28     }
29
30     public boolean hasMoreChildren() {
31         Pointer pointer = (Pointer) pointers.peek();
32
33         if (pointer.v < getChildCount()) {
34             return true;
35         } else {
36             return false;
37         }
38     }
39
40     public void moveUp() {
41         current = getParent();
42         pointers.popSilently();
43         reassignCurrentElement(current);
44     }
45
46     public void moveDown() {
47         Pointer pointer = (Pointer) pointers.peek();
48         pointers.push(new Pointer());
49
50         current = getChild(pointer.v);
51
52         pointer.v++;
53         reassignCurrentElement(current);
54     }
55
56     public Iterator getAttributeNames() {
57         return new AttributeNameIterator(this);
58     }
59
60     public void appendErrors(ErrorWriter errorWriter) {
61     }
62
63     public Object JavaDoc peekUnderlyingNode() {
64         return current;
65     }
66
67     public void close() {
68         // don't need to do anything
69
}
70
71     public HierarchicalStreamReader underlyingReader() {
72         return this;
73     }
74 }
75
Popular Tags