1 21 22 package nu.xom.samples; 23 24 import java.io.IOException ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Writer ; 27 28 import nu.xom.Attribute; 29 import nu.xom.Document; 30 import nu.xom.Element; 31 import nu.xom.Node; 32 33 34 45 public class PropertyPrinter { 46 47 private Writer out; 48 49 public PropertyPrinter(Writer out) { 50 if (out == null) { 51 throw new NullPointerException ("Writer must be non-null."); 52 } 53 this.out = out; 54 } 55 56 public PropertyPrinter() { 57 this(new OutputStreamWriter (System.out)); 58 } 59 60 private int nodeCount = 0; 61 62 public void writeNode(Node node) throws IOException { 63 64 if (node == null) { 65 throw new NullPointerException ("Node must be non-null."); 66 } 67 if (node instanceof Document) { 68 nodeCount = 1; 70 } 71 72 String type = node.getClass().getName(); String value = node.getValue(); 74 75 String name = null; 76 String localName = null; 77 String uri = null; 78 String prefix = null; 79 85 if (node instanceof Element) { 86 Element element = (Element) node; 87 name = element.getQualifiedName(); 88 localName = element.getLocalName(); 89 uri = element.getNamespaceURI(); 90 prefix = element.getNamespacePrefix(); 91 } 92 else if (node instanceof Attribute) { 93 Element element = (Element) node; 94 name = element.getQualifiedName(); 95 localName = element.getLocalName(); 96 uri = element.getNamespaceURI(); 97 prefix = element.getNamespacePrefix(); 98 } 99 100 101 StringBuffer result = new StringBuffer (); 102 result.append("Node " + nodeCount + ":\r\n"); 103 result.append(" Type: " + type + "\r\n"); 104 if (name != null) { 105 result.append(" Name: " + name + "\r\n"); 106 } 107 if (localName != null) { 108 result.append(" Local Name: " + localName + "\r\n"); 109 } 110 if (prefix != null) { 111 result.append(" Prefix: " + prefix + "\r\n"); 112 } 113 if (uri != null) { 114 result.append(" Namespace URI: " + uri + "\r\n"); 115 } 116 if (value != null) { 117 result.append(" Value: " + value + "\r\n"); 118 } 119 120 out.write(result.toString()); 121 out.write("\r\n"); 122 out.flush(); 123 124 nodeCount++; 125 126 } 127 128 } 129 | Popular Tags |