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 02. September 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import nu.xom.Document;
15: import nu.xom.Element;
16: import nu.xom.Node;
17: import nu.xom.Text;
18:
19: public class XomReader extends AbstractDocumentReader {
20:
21: private Element currentElement;
22:
23: public XomReader(Element rootElement) {
24: super (rootElement);
25: }
26:
27: public XomReader(Document document) {
28: super (document.getRootElement());
29: }
30:
31: /**
32: * @since 1.2
33: */
34: public XomReader(Element rootElement, XmlFriendlyReplacer replacer) {
35: super (rootElement, replacer);
36: }
37:
38: /**
39: * @since 1.2
40: */
41: public XomReader(Document document, XmlFriendlyReplacer replacer) {
42: super (document.getRootElement(), replacer);
43: }
44:
45: public String getNodeName() {
46: return unescapeXmlName(currentElement.getLocalName());
47: }
48:
49: public String getValue() {
50: // currentElement.getValue() not used as this includes text of child elements, which we don't want.
51: StringBuffer result = new StringBuffer();
52: int childCount = currentElement.getChildCount();
53: for (int i = 0; i < childCount; i++) {
54: Node child = currentElement.getChild(i);
55: if (child instanceof Text) {
56: Text text = (Text) child;
57: result.append(text.getValue());
58: }
59: }
60: return result.toString();
61: }
62:
63: public String getAttribute(String name) {
64: return currentElement.getAttributeValue(name);
65: }
66:
67: public String getAttribute(int index) {
68: return currentElement.getAttribute(index).getValue();
69: }
70:
71: public int getAttributeCount() {
72: return currentElement.getAttributeCount();
73: }
74:
75: public String getAttributeName(int index) {
76: return unescapeXmlName(currentElement.getAttribute(index)
77: .getQualifiedName());
78: }
79:
80: protected int getChildCount() {
81: return currentElement.getChildElements().size();
82: }
83:
84: protected Object getParent() {
85: return currentElement.getParent();
86: }
87:
88: protected Object getChild(int index) {
89: return currentElement.getChildElements().get(index);
90: }
91:
92: protected void reassignCurrentElement(Object current) {
93: currentElement = (Element) current;
94: }
95: }
|