001: /*
002: * Copyright (C) 2005, 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 24. April 2005 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import java.util.Iterator;
015:
016: import com.thoughtworks.xstream.converters.ErrorWriter;
017: import com.thoughtworks.xstream.core.util.FastStack;
018: import com.thoughtworks.xstream.io.AttributeNameIterator;
019: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
020:
021: public abstract class AbstractDocumentReader extends AbstractXmlReader
022: implements DocumentReader {
023:
024: private FastStack pointers = new FastStack(16);
025: private Object current;
026:
027: protected AbstractDocumentReader(Object rootElement) {
028: this (rootElement, new XmlFriendlyReplacer());
029: }
030:
031: /**
032: * @since 1.2
033: */
034: protected AbstractDocumentReader(Object rootElement,
035: XmlFriendlyReplacer replacer) {
036: super (replacer);
037: this .current = rootElement;
038: pointers.push(new Pointer());
039: reassignCurrentElement(current);
040: }
041:
042: protected abstract void reassignCurrentElement(Object current);
043:
044: protected abstract Object getParent();
045:
046: protected abstract Object getChild(int index);
047:
048: protected abstract int getChildCount();
049:
050: private static class Pointer {
051: public int v;
052: }
053:
054: public boolean hasMoreChildren() {
055: Pointer pointer = (Pointer) pointers.peek();
056:
057: if (pointer.v < getChildCount()) {
058: return true;
059: } else {
060: return false;
061: }
062: }
063:
064: public void moveUp() {
065: current = getParent();
066: pointers.popSilently();
067: reassignCurrentElement(current);
068: }
069:
070: public void moveDown() {
071: Pointer pointer = (Pointer) pointers.peek();
072: pointers.push(new Pointer());
073:
074: current = getChild(pointer.v);
075:
076: pointer.v++;
077: reassignCurrentElement(current);
078: }
079:
080: public Iterator getAttributeNames() {
081: return new AttributeNameIterator(this );
082: }
083:
084: public void appendErrors(ErrorWriter errorWriter) {
085: }
086:
087: /**
088: * @deprecated As of 1.2, use {@link #getCurrent() }
089: */
090: public Object peekUnderlyingNode() {
091: return current;
092: }
093:
094: public Object getCurrent() {
095: return this .current;
096: }
097:
098: public void close() {
099: // don't need to do anything
100: }
101:
102: public HierarchicalStreamReader underlyingReader() {
103: return this;
104: }
105: }
|