1 23 24 package org.enhydra.xml.dom; 25 26 import java.io.PrintWriter ; 27 28 import org.enhydra.xml.xmlc.codegen.JavaLang; 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.CDATASection ; 31 import org.w3c.dom.CharacterData ; 32 import org.w3c.dom.Comment ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.DocumentFragment ; 35 import org.w3c.dom.DocumentType ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Entity ; 38 import org.w3c.dom.EntityReference ; 39 import org.w3c.dom.NamedNodeMap ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.Notation ; 42 import org.w3c.dom.ProcessingInstruction ; 43 import org.w3c.dom.Text ; 44 45 48 public class DOMInfoPrinter implements DOMTraversal.Handler { 49 50 private static final int TEXT_TRUNCATE_SIZE = 12; 51 52 53 protected PrintWriter fOut; 54 55 56 private boolean fSymbolicLineBreaks; 57 58 59 private boolean fTruncateText; 60 61 62 protected boolean fVerboseAttributes; 63 64 65 protected boolean fAllAttributes; 66 67 72 protected boolean fAttrSkipPrint; 73 74 77 private DOMTraversal fTraverser; 78 79 82 protected int level; 83 84 87 protected DOMInfoPrinter(int options, 88 DOMTraversal traverser, 89 PrintWriter out) { 90 fOut = out; 91 fSymbolicLineBreaks = ((options & DOMInfo.SYM_TEXT_LINEBREAKS) != 0); 92 fTruncateText = ((options & DOMInfo.TRUNCATE_TEXT) != 0); 93 fVerboseAttributes = ((options & DOMInfo.PRINT_ATTR_DETAILS) != 0); 94 fAllAttributes = ((options & DOMInfo.ALL_ATTRS) != 0); 95 fTraverser = traverser; 96 traverser.setHandler(this); 97 } 98 99 102 protected void printIndent() { 103 for (int i = 0; i < level + 1; i++) { 104 fOut.print(" "); 105 } 106 } 107 108 111 private boolean isPrintable(char ch) { 112 return ((ch <= 0x7f) 113 && (Character.isWhitespace(ch) || !Character.isISOControl(ch))); 114 } 115 116 120 private void printString(String text) { 121 int len = text.length(); 122 for (int idx = 0; idx < len; idx++) { 123 char ch = text.charAt(idx); 124 if (fSymbolicLineBreaks && ((ch == '\n') || (ch == '\r'))) { 125 fOut.write((ch == '\n') ? "\\n" : "\\r"); 126 } else if (isPrintable(ch)) { 127 fOut.write(ch); 128 } else { 129 String hex = Integer.toHexString(ch); 130 fOut.write("\\u"); 131 for (int cnt = hex.length(); cnt < 4; cnt++) { 132 fOut.write('0'); 133 } 134 fOut.write(hex); 135 } 136 } 137 } 138 139 142 private void printLabelValue(String label, 143 String value) { 144 if ((value != null) && (value.length() > 0)) { 145 fOut.print(" "); 146 fOut.print(label); 147 fOut.print("="); 148 printString(value); 149 } 150 } 151 152 155 private void printName(String name) { 156 if (name != null) { 157 fOut.print(" "); 158 fOut.print(name); 159 } 160 } 161 162 165 protected void printNodeName(Node node) { 166 fOut.print(JavaLang.simpleClassName(node.getClass().getName())); 167 } 168 169 172 private void printNodeInfo(Node node) { 173 printIndent(); 175 printNodeName(node); 176 fOut.print(':'); 177 String namespaceURI = node.getNamespaceURI(); 178 if ((namespaceURI != null) && (namespaceURI.length() > 0)) { 179 printLabelValue("NS", namespaceURI); 180 fOut.print(":"); 181 } 182 } 183 184 187 private void processChildren(Node node) { 188 level++; 189 fTraverser.processChildren(node); 190 level--; 191 } 192 193 196 protected boolean hasPrintAttributes(Element element) { 197 NamedNodeMap attrs = element.getAttributes(); 198 if (attrs == null) { 199 return false; 200 } 201 if (fAllAttributes) { 202 return attrs.getLength() > 0; 203 } 204 for (int i = 0; i < attrs.getLength(); i++) { 206 if (((Attr )attrs.item(i)).getSpecified()) { 207 return true; 208 } 209 } 210 return false; 211 } 212 213 216 public void handleDocument(Document document) { 217 printNodeInfo(document); 218 fOut.println(); 219 220 level++; 221 fTraverser.processDocumentType(document); 222 level--; 223 processChildren(document); 224 } 225 226 229 public void handleDocumentType(DocumentType documentType) { 230 printNodeInfo(documentType); 231 printLabelValue("name", documentType.getNodeName()); 232 fOut.println(); 233 234 String internalSubset = documentType.getInternalSubset(); 235 if ((internalSubset != null) && (internalSubset.length() > 0)) { 236 printIndent(); 237 printLabelValue("internalSubset", internalSubset); 238 fOut.println(); 239 } 240 241 level++; 242 fTraverser.processDocumentTypeContents(documentType); 243 processChildren(documentType); 246 level--; 247 } 248 249 252 public void handleDocumentFragment(DocumentFragment documentFragment) { 253 printNodeInfo(documentFragment); 254 printLabelValue("name", documentFragment.getNodeName()); 255 fOut.println(); 256 257 processChildren(documentFragment); 258 } 259 260 263 private void printTerseAttrInfo(Node attr) { 264 printName(attr.getNodeName()); 265 fOut.print("=\""); 266 printString(attr.getNodeValue()); 267 fOut.print("\""); 268 } 269 270 273 private void printVerboseAttrInfo(Node attr) { 274 printNodeInfo(attr); 275 printName(attr.getNodeName()); 276 if (!((Attr )attr).getSpecified()) { 277 fOut.print(" (unspecified)"); 278 } 279 fOut.println(); 280 } 281 282 285 public void handleAttr(Attr attr) { 286 if (fVerboseAttributes) { 287 printVerboseAttrInfo(attr); 288 } else { 289 printTerseAttrInfo(attr); 290 } 291 292 fAttrSkipPrint = !fVerboseAttributes; 293 processChildren(attr); 294 fAttrSkipPrint = false; 295 } 296 297 300 public void handleEntity(Entity entity) { 301 printNodeInfo(entity); 302 printLabelValue("name", entity.getNodeName()); 303 printLabelValue("notationName", entity.getNotationName()); 304 printLabelValue("systemId", entity.getPublicId()); 305 printLabelValue("publicId", entity.getPublicId()); 306 fOut.println(); 307 308 processChildren(entity); 309 } 310 311 314 public void handleEntityReference(EntityReference entityRef) { 315 if (!fAttrSkipPrint) { 316 printNodeInfo(entityRef); 317 printLabelValue("name", entityRef.getNodeName()); 318 fOut.println(); 319 } 320 processChildren(entityRef); 321 } 322 323 326 private void processAttributes(Element element) { 327 if (fVerboseAttributes) { 328 fOut.println(); 329 if (hasPrintAttributes(element)) { 330 level++; 331 printIndent(); 332 fOut.println("Attributes:"); 333 level++; 334 fTraverser.processAttributes(element); 335 level -= 2; 336 } 337 } else { 338 if (hasPrintAttributes(element)) { 339 fOut.print(":"); 340 fTraverser.processAttributes(element); 341 } 342 fOut.println(); 343 } 344 } 345 346 349 public void handleElement(Element element) { 350 printNodeInfo(element); 351 printName(element.getNodeName()); 352 353 processAttributes(element); 354 processChildren(element); 355 } 356 357 360 public void handleNotation(Notation notation) { 361 if (!fAttrSkipPrint) { 362 printNodeInfo(notation); 363 printLabelValue("systemId", notation.getSystemId()); 364 printLabelValue("publicId", notation.getPublicId()); 365 } 366 } 367 368 371 public void handleProcessingInstruction(ProcessingInstruction pi) { 372 if (!fAttrSkipPrint) { 373 printNodeInfo(pi); 374 printLabelValue("target", pi.getTarget()); 375 printLabelValue("data", pi.getData()); 376 fOut.println(); 377 } 378 } 379 380 383 private void printCharacterDataInfo(CharacterData charData) { 384 if (!fAttrSkipPrint) { 385 printNodeInfo(charData); 386 fOut.print(" "); 387 String data = charData.getData(); 388 if (fTruncateText && (data.length() > TEXT_TRUNCATE_SIZE)) { 389 data = data.substring(0, TEXT_TRUNCATE_SIZE); 390 } 391 printString(data); 392 fOut.println(); 393 } 394 } 395 396 399 public void handleCDATASection(CDATASection cdata) { 400 printCharacterDataInfo(cdata); 401 } 402 403 406 public void handleComment(Comment comment) { 407 printCharacterDataInfo(comment); 408 } 409 410 413 public void handleText(Text text) { 414 printCharacterDataInfo(text); 415 } 416 } 417 | Popular Tags |