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 03. September 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import org.jdom.Attribute;
15: import org.jdom.Document;
16: import org.jdom.Element;
17:
18: /**
19: * @author Laurent Bihanic
20: */
21: public class JDomReader extends AbstractDocumentReader {
22:
23: private Element currentElement;
24:
25: public JDomReader(Element root) {
26: super (root);
27: }
28:
29: public JDomReader(Document document) {
30: super (document.getRootElement());
31: }
32:
33: /**
34: * @since 1.2
35: */
36: public JDomReader(Element root, XmlFriendlyReplacer replacer) {
37: super (root, replacer);
38: }
39:
40: /**
41: * @since 1.2
42: */
43: public JDomReader(Document document, XmlFriendlyReplacer replacer) {
44: super (document.getRootElement(), replacer);
45: }
46:
47: protected void reassignCurrentElement(Object current) {
48: currentElement = (Element) current;
49: }
50:
51: protected Object getParent() {
52: // JDOM 1.0:
53: return currentElement.getParentElement();
54:
55: // JDOM b10:
56: // Parent parent = currentElement.getParent();
57: // return (parent instanceof Element) ? (Element)parent : null;
58:
59: // JDOM b9 and earlier:
60: // return currentElement.getParent();
61: }
62:
63: protected Object getChild(int index) {
64: return currentElement.getChildren().get(index);
65: }
66:
67: protected int getChildCount() {
68: return currentElement.getChildren().size();
69: }
70:
71: public String getNodeName() {
72: return unescapeXmlName(currentElement.getName());
73: }
74:
75: public String getValue() {
76: return currentElement.getText();
77: }
78:
79: public String getAttribute(String name) {
80: return currentElement.getAttributeValue(name);
81: }
82:
83: public String getAttribute(int index) {
84: return ((Attribute) currentElement.getAttributes().get(index))
85: .getValue();
86: }
87:
88: public int getAttributeCount() {
89: return currentElement.getAttributes().size();
90: }
91:
92: public String getAttributeName(int index) {
93: return unescapeXmlName(((Attribute) currentElement
94: .getAttributes().get(index)).getQualifiedName());
95: }
96:
97: }
|