001: /*
002: * Copyright (C) 2004, 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 07. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import org.w3c.dom.Attr;
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019: import org.w3c.dom.Text;
020:
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: public class DomReader extends AbstractDocumentReader {
025:
026: private Element currentElement;
027: private StringBuffer textBuffer;
028: private List childElements;
029:
030: public DomReader(Element rootElement) {
031: this (rootElement, new XmlFriendlyReplacer());
032: }
033:
034: public DomReader(Document document) {
035: this (document.getDocumentElement());
036: }
037:
038: /**
039: * @since 1.2
040: */
041: public DomReader(Element rootElement, XmlFriendlyReplacer replacer) {
042: super (rootElement, replacer);
043: textBuffer = new StringBuffer();
044: }
045:
046: /**
047: * @since 1.2
048: */
049: public DomReader(Document document, XmlFriendlyReplacer replacer) {
050: this (document.getDocumentElement(), replacer);
051: }
052:
053: public String getNodeName() {
054: return unescapeXmlName(currentElement.getTagName());
055: }
056:
057: public String getValue() {
058: NodeList childNodes = currentElement.getChildNodes();
059: textBuffer.setLength(0);
060: int length = childNodes.getLength();
061: for (int i = 0; i < length; i++) {
062: Node childNode = childNodes.item(i);
063: if (childNode instanceof Text) {
064: Text text = (Text) childNode;
065: textBuffer.append(text.getData());
066: }
067: }
068: return textBuffer.toString();
069: }
070:
071: public String getAttribute(String name) {
072: Attr attribute = currentElement.getAttributeNode(name);
073: return attribute == null ? null : attribute.getValue();
074: }
075:
076: public String getAttribute(int index) {
077: return ((Attr) currentElement.getAttributes().item(index))
078: .getValue();
079: }
080:
081: public int getAttributeCount() {
082: return currentElement.getAttributes().getLength();
083: }
084:
085: public String getAttributeName(int index) {
086: return unescapeXmlName(((Attr) currentElement.getAttributes()
087: .item(index)).getName());
088: }
089:
090: protected Object getParent() {
091: return currentElement.getParentNode();
092: }
093:
094: protected Object getChild(int index) {
095: return childElements.get(index);
096: }
097:
098: protected int getChildCount() {
099: return childElements.size();
100: }
101:
102: protected void reassignCurrentElement(Object current) {
103: currentElement = (Element) current;
104: NodeList childNodes = currentElement.getChildNodes();
105: childElements = new ArrayList();
106: for (int i = 0; i < childNodes.getLength(); i++) {
107: Node node = childNodes.item(i);
108: if (node instanceof Element) {
109: childElements.add(node);
110: }
111: }
112: }
113:
114: }
|