1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 4 import org.w3c.dom.Document ; 5 import org.w3c.dom.Element ; 6 import org.w3c.dom.Node ; 7 8 11 public class DomWriter implements HierarchicalStreamWriter { 12 private final Document document; 13 private Element current; 14 15 public DomWriter(Document document) { 16 this.document = document; 17 this.current = document.getDocumentElement(); 18 } 19 20 public DomWriter(Element rootElement) { 21 document = rootElement.getOwnerDocument(); 22 current = rootElement; 23 } 24 25 public void startNode(String name) { 26 final Element child = document.createElement(name); 27 if (current == null) { 28 document.appendChild(child); 29 } else { 30 current.appendChild(child); 31 } 32 current = child; 33 } 34 35 public void addAttribute(String name, String value) { 36 current.setAttribute(name, value); 37 } 38 39 public void setValue(String text) { 40 current.appendChild(document.createTextNode(text)); 41 } 42 43 public void endNode() { 44 Node parent = current.getParentNode(); 45 current = parent instanceof Element ? (Element )parent : null; 46 } 47 48 public void flush() { 49 } 51 52 public void close() { 53 } 55 56 public HierarchicalStreamWriter underlyingWriter() { 57 return this; 58 } 59 } | Popular Tags |