1 23 24 package org.enhydra.xml.dom; 25 26 import java.io.OutputStream ; 27 import java.io.PrintWriter ; 28 29 import org.enhydra.xml.lazydom.LazyDOMInfoPrinter; 30 import org.enhydra.xml.lazydom.LazyDocument; 31 import org.w3c.dom.Node ; 32 33 37 38 42 46 public class DOMInfo { 47 50 public static final int PRINT_ATTR_DETAILS = 0x01; 51 52 55 public static final int ALL_ATTRS = 0x02; 56 57 61 public static final int SYM_TEXT_LINEBREAKS = 0x04; 62 63 66 private static final int NO_RECURSION = 0x08; 68 71 public static final int TRUNCATE_TEXT = 0x10; 72 73 76 public static final int PRINT_ALL = 0xffff & ~NO_RECURSION; 77 78 81 public static final int PRINT_DEFAULT = 0; 82 83 86 protected int fOptions; 87 88 91 protected PrintWriter fOut; 92 93 96 protected DOMTraversal fTraverser; 97 protected DOMInfoPrinter fInfoPrinter; 98 99 103 private static DOMInfo createDOMInfo(int options, 104 Node node, 105 PrintWriter out) { 106 107 DOMTraversal traverser 110 = DOMTraversal.getTraverser(null, 111 getTraverserOptions(options), 112 node); 113 114 DOMInfoPrinter handler; 115 if (DOMOps.getDocument(node) instanceof LazyDocument) { 116 handler = new LazyDOMInfoPrinter(options, traverser, out); 117 } else { 118 handler = new DOMInfoPrinter(options, traverser, out); 119 } 120 traverser.setHandler(handler); 121 return new DOMInfo(options, traverser, handler, out); 122 } 123 124 127 protected DOMInfo(int options, 128 DOMTraversal traverser, 129 DOMInfoPrinter infoPrinter, 130 PrintWriter out) { 131 fOptions = options; 132 fTraverser = traverser; 133 fInfoPrinter = infoPrinter; 134 fOut = out; 135 } 136 137 140 private void traverse(Node node) { 141 fTraverser.traverse(node); 142 } 143 144 147 public static int getTraverserOptions(int options) { 148 int travOptions = DOMTraversal.SORT_ATTRIBUTES; 149 if ((options & ALL_ATTRS) != 0) { 150 travOptions |= DOMTraversal.ALL_ATTRIBUTES; 151 } 152 return travOptions; 153 } 154 155 156 159 public static String nodeTypeToName(short type) { 160 switch (type) { 161 case Node.ELEMENT_NODE: 162 return "Element"; 163 case Node.ATTRIBUTE_NODE: 164 return "Attr"; 165 case Node.TEXT_NODE: 166 return "Text"; 167 case Node.CDATA_SECTION_NODE: 168 return "CDATASection"; 169 case Node.ENTITY_REFERENCE_NODE: 170 return "EntityReference"; 171 case Node.ENTITY_NODE: 172 return "Entity"; 173 case Node.PROCESSING_INSTRUCTION_NODE: 174 return "ProcessingInstruction"; 175 case Node.COMMENT_NODE: 176 return "Comment"; 177 case Node.DOCUMENT_NODE: 178 return "Document"; 179 case Node.DOCUMENT_TYPE_NODE: 180 return "DocumentType"; 181 case Node.DOCUMENT_FRAGMENT_NODE: 182 return "DocumentFragment"; 183 case Node.NOTATION_NODE: 184 return "Notation"; 185 default: 186 throw new IllegalArgumentException ("Unknown node type: " + type); 187 } 188 } 189 190 199 public static void printTree(String msg, 200 Node root, 201 int options, 202 PrintWriter out) { 203 if((options & TRUNCATE_TEXT) != 0) { 205 options |= SYM_TEXT_LINEBREAKS; 206 } 207 208 if (out == null) { 209 out = new PrintWriter (System.err, true); 210 } 211 if ((msg != null) && (msg.length() > 0)) { 212 out.println(msg + ":"); 213 } 214 if (root == null) { 215 out.println(" null node"); 216 } else { 217 DOMInfo domInfo = createDOMInfo(options, root, out); 218 domInfo.traverse(root); 219 } 220 out.flush(); 221 } 222 223 231 public static void printTree(String msg, 232 Node root, 233 PrintWriter out) { 234 printTree(msg, root, PRINT_DEFAULT, out); 235 } 236 237 245 public static void printTree(String msg, 246 Node root, 247 OutputStream out) { 248 PrintWriter writer = new PrintWriter ((out != null) ? out : System.err, true); 249 printTree(msg, root, PRINT_DEFAULT, writer); 250 } 251 252 259 public static void printTree(String msg, 260 Node root) { 261 PrintWriter writer = new PrintWriter (System.err, true); 262 printTree(msg, root, PRINT_DEFAULT, writer); 263 } 264 } 265 | Popular Tags |