1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 4 import org.dom4j.Branch; 5 import org.dom4j.DocumentFactory; 6 import org.dom4j.Element; 7 8 import java.util.LinkedList ; 9 10 public class Dom4JWriter implements HierarchicalStreamWriter { 11 12 private DocumentFactory documentFactory = new DocumentFactory(); 13 private LinkedList elementStack = new LinkedList (); 14 15 public Dom4JWriter(Branch container) { 16 elementStack.addLast(container); 17 } 18 19 public void startNode(String name) { 20 Element element = documentFactory.createElement(name); 21 top().add(element); 22 elementStack.addLast(element); 23 } 24 25 public void setValue(String text) { 26 top().setText(text); 27 } 28 29 public void addAttribute(String key, String value) { 30 ((Element) top()).addAttribute(key, value); 31 } 32 33 public void endNode() { 34 elementStack.removeLast(); 35 } 36 37 private Branch top() { 38 return (Branch) elementStack.getLast(); 39 } 40 41 } 42 | Popular Tags |