1 package com.thoughtworks.xstream.io.xml; 2 3 import nu.xom.*; 4 5 public class XomReader extends AbstractDocumentReader { 6 7 private Element currentElement; 8 9 public XomReader(Element rootElement) { 10 super(rootElement); 11 } 12 13 public XomReader(Document document) { 14 super(document.getRootElement()); 15 } 16 17 public String getNodeName() { 18 return currentElement.getLocalName(); 19 } 20 21 public String getValue() { 22 StringBuffer result = new StringBuffer (); 24 int childCount = currentElement.getChildCount(); 25 for(int i = 0; i < childCount; i++) { 26 Node child = currentElement.getChild(i); 27 if (child instanceof Text) { 28 Text text = (Text) child; 29 result.append(text.getValue()); 30 } 31 } 32 return result.toString(); 33 } 34 35 public String getAttribute(String name) { 36 return currentElement.getAttributeValue(name); 37 } 38 39 public String getAttribute(int index) { 40 return currentElement.getAttribute(index).getValue(); 41 } 42 43 public int getAttributeCount() { 44 return currentElement.getAttributeCount(); 45 } 46 47 public String getAttributeName(int index) { 48 return currentElement.getAttribute(index).getQualifiedName(); 49 } 50 51 protected int getChildCount() { 52 return currentElement.getChildElements().size(); 53 } 54 55 protected Object getParent() { 56 return currentElement.getParent(); 57 } 58 59 protected Object getChild(int index) { 60 return currentElement.getChildElements().get(index); 61 } 62 63 protected void reassignCurrentElement(Object current) { 64 currentElement = (Element) current; 65 } 66 } 67 | Popular Tags |