01: /*
02: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 07. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import com.thoughtworks.xstream.converters.ErrorWriter;
15:
16: import org.dom4j.Document;
17: import org.dom4j.Element;
18:
19: public class Dom4JReader extends AbstractDocumentReader {
20:
21: private Element currentElement;
22:
23: public Dom4JReader(Element rootElement) {
24: this (rootElement, new XmlFriendlyReplacer());
25: }
26:
27: public Dom4JReader(Document document) {
28: this (document.getRootElement());
29: }
30:
31: /**
32: * @since 1.2
33: */
34: public Dom4JReader(Element rootElement, XmlFriendlyReplacer replacer) {
35: super (rootElement, replacer);
36: }
37:
38: /**
39: * @since 1.2
40: */
41: public Dom4JReader(Document document, XmlFriendlyReplacer replacer) {
42: this (document.getRootElement(), replacer);
43: }
44:
45: public String getNodeName() {
46: return unescapeXmlName(currentElement.getName());
47: }
48:
49: public String getValue() {
50: return currentElement.getText();
51: }
52:
53: public String getAttribute(String name) {
54: return currentElement.attributeValue(name);
55: }
56:
57: public String getAttribute(int index) {
58: return currentElement.attribute(index).getValue();
59: }
60:
61: public int getAttributeCount() {
62: return currentElement.attributeCount();
63: }
64:
65: public String getAttributeName(int index) {
66: return unescapeXmlName(currentElement.attribute(index)
67: .getQualifiedName());
68: }
69:
70: protected Object getParent() {
71: return currentElement.getParent();
72: }
73:
74: protected Object getChild(int index) {
75: return currentElement.elements().get(index);
76: }
77:
78: protected int getChildCount() {
79: return currentElement.elements().size();
80: }
81:
82: protected void reassignCurrentElement(Object current) {
83: currentElement = (Element) current;
84: }
85:
86: public void appendErrors(ErrorWriter errorWriter) {
87: errorWriter.add("xpath", currentElement.getPath());
88: }
89:
90: }
|