1 19 20 package edu.umd.cs.findbugs.xml; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 26 import org.dom4j.Branch; 27 import org.dom4j.Element; 28 29 35 public class Dom4JXMLOutput implements XMLOutput { 36 private LinkedList <Branch> stack; 37 38 44 public Dom4JXMLOutput(Branch topLevel) { 45 this.stack = new LinkedList <Branch>(); 46 stack.addLast(topLevel); 47 } 48 49 public void beginDocument() { 50 } 51 52 public void openTag(String tagName) { 53 Branch top = stack.getLast(); 54 Element element = top.addElement(tagName); 55 stack.addLast(element); 56 } 57 58 public void openTag(String tagName, XMLAttributeList attributeList) { 59 Branch top = stack.getLast(); 60 Element element = top.addElement(tagName); 61 stack.addLast(element); 62 63 for (Iterator <XMLAttributeList.NameValuePair> i= attributeList.iterator(); 64 i.hasNext(); ) { 65 XMLAttributeList.NameValuePair pair = i.next(); 66 element.addAttribute(pair.getName(), pair.getValue()); 67 } 68 } 69 70 public void openCloseTag(String tagName) { 71 openTag(tagName); 72 closeTag(tagName); 73 } 74 75 public void openCloseTag(String tagName, XMLAttributeList attributeList) { 76 openTag(tagName, attributeList); 77 closeTag(tagName); 78 } 79 80 public void startTag(String tagName) { 81 Branch top = stack.getLast(); 82 Element element = top.addElement(tagName); 83 stack.addLast(element); 84 } 85 86 public void addAttribute(String name, String value) { 87 Element element = (Element) stack.getLast(); 88 element.addAttribute(name, value); 89 } 90 91 public void stopTag(boolean close) { 92 if ( close ) { 93 closeTag(null); 94 } 95 } 96 97 public void closeTag(String tagName) { 98 stack.removeLast(); 99 } 100 101 public void writeText(String text) { 102 Element top = (Element) stack.getLast(); 103 top.addText(text); 104 } 105 106 public void writeCDATA(String cdata) { 107 Element top = (Element) stack.getLast(); 108 top.addCDATA(cdata); 109 } 110 111 118 public void writeElementList(String tagName, Collection <String > listValues) { 119 for (String listValue : listValues) { 120 openTag(tagName); 121 writeText(listValue); 122 closeTag(tagName); 123 } 124 } 125 126 131 public void write(XMLWriteable obj) { 132 try { 133 obj.writeXML(this); 134 } catch (java.io.IOException e) { 135 } 137 } 138 139 144 public void writeCollection(Collection <? extends XMLWriteable> collection) { 145 for (XMLWriteable obj : collection) { 146 write(obj); 147 } 148 } 149 150 public void finish() { 151 } 152 } 153 154 | Popular Tags |