1 23 24 package org.apache.webdav.lib.util; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.xml.sax.SAXException ; 31 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import java.io.*; 36 import org.xml.sax.InputSource ; 37 38 39 40 import java.util.StringTokenizer ; 41 42 45 public class XMLDebugOutputer { 46 47 49 private final static String INDENT = " "; 51 private int tabWidth = 3; 52 53 54 56 private boolean debug = false; 58 59 60 private DocumentBuilderFactory dbf=null; 62 private DocumentBuilder db=null; 63 64 65 67 70 public XMLDebugOutputer() { 71 72 try { 73 dbf=DocumentBuilderFactory.newInstance(); 74 db =dbf.newDocumentBuilder(); 75 76 } catch (ParserConfigurationException e) { 77 } 78 79 81 }; 82 83 85 89 public void print(Document doc) { 90 91 if (debug) { 92 Node root = doc.getDocumentElement(); 93 94 dispatchNode(root, 0); 95 } 96 } 97 98 102 public void print(String xmlString) { 103 104 if (debug) { 105 try { 106 Document doc = db.parse(new InputSource (new StringReader(xmlString))); 107 print(doc); 108 109 } catch (SAXException e) { 110 } catch (IOException e) { 111 } 112 } 113 114 115 } 116 117 121 public void setDebug(boolean debug) { 122 this.debug = debug; 123 } 124 125 126 128 129 130 private void dispatchNode(Node node, int level) { 131 switch (node.getNodeType()) { 132 case Node.CDATA_SECTION_NODE : 133 printCDATANode(node, level); 134 break; 135 136 case Node.COMMENT_NODE : 137 printCommentNode(node, level); 138 break; 139 140 case Node.TEXT_NODE : 141 printTextNode(node, level); 142 break; 143 144 case Node.ELEMENT_NODE : 145 printElementNode(node, level); 146 break; 147 148 default : 149 break; 150 } 151 } 152 153 private void printCDATANode(Node node, int level) { 154 System.out.print(INDENT.substring(0, level * tabWidth)); 155 System.out.println("<![CDATA["); 156 157 indentBlock(node.getNodeValue(), level + 1); 158 159 System.out.print(INDENT.substring(0, level * tabWidth)); 160 System.out.println("]]>"); 161 } 162 163 private void printTextNode(Node node, int level) { 164 indentBlock(node.getNodeValue(), level + 1); 165 } 166 167 private void printCommentNode(Node node, int level) { 168 System.out.print(INDENT.substring(0, level * tabWidth)); 169 System.out.println("<!-- "); 170 indentBlock(node.getNodeValue(), level + 1); 171 System.out.print(INDENT.substring(0, level * tabWidth)); 172 System.out.println(" -->"); 173 } 174 175 private void printElementNode(Node node, int level) { 176 String name = node.getNodeName(); 177 System.out.print(INDENT.substring(0, level * tabWidth)); 178 System.out.print("<"); 179 System.out.print(name); 180 181 NamedNodeMap attributes = node.getAttributes(); 182 for (int i = 0; attributes != null && i < attributes.getLength(); i++) { 183 if (attributes.getLength() > 1) { 184 System.out.println(); 185 System.out.print(INDENT.substring(0, (2 + level) * tabWidth)); 186 } else { 187 System.out.print(" "); 188 } 189 190 Node attribute = attributes.item(i); 191 System.out.print(attribute.getNodeName()); 192 System.out.print("=\""); 193 System.out.print(attribute.getNodeValue()); 194 System.out.print("\""); 195 } 196 System.out.println(">"); 197 198 if (attributes.getLength() > 1) { 200 System.out.println(); 201 } 202 203 if (node.hasChildNodes()) { 204 NodeList children = node.getChildNodes(); 205 for (int i = 0; i < children.getLength(); i++) { 206 dispatchNode(children.item(i), level + 1); 208 } 209 } 210 211 System.out.print(INDENT.substring(0, level * tabWidth)); 212 System.out.print("</"); 213 System.out.print(name); 214 System.out.println(">"); 215 } 216 217 private void indentBlock(String block, int level) { 218 StringTokenizer linetok = new StringTokenizer (block.replace('\n', ' ')); 219 int pos = level * tabWidth; 220 221 if (linetok.countTokens() > 0) { 222 System.out.print(INDENT.substring(0, level * tabWidth)); 223 } 224 225 while (linetok.hasMoreTokens()) { 226 String token = linetok.nextToken(); 227 pos += (token.length() + 1); 228 if (pos < 80 && token.length() < (80 - (level * tabWidth))) { 229 if (linetok.countTokens() > 0) { 230 System.out.print(token); 231 System.out.print(" "); 232 } else { 233 System.out.println(token); 234 } 235 } else { 236 System.out.println(token); 237 if (linetok.countTokens() > 0) { 238 System.out.print(INDENT.substring(0, level * tabWidth)); 239 } 240 pos = level * tabWidth; 241 } 242 } 243 } 244 245 246 } 247 | Popular Tags |