1 16 17 package ui; 18 19 20 import java.io.Serializable ; 21 import java.util.Hashtable ; 22 23 import javax.swing.JTree ; 24 import javax.swing.tree.DefaultMutableTreeNode ; 25 import javax.swing.tree.DefaultTreeModel ; 26 import javax.swing.tree.MutableTreeNode ; 27 import javax.swing.tree.TreeNode ; 28 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.Notation ; 34 35 41 public class DOMTreeFull 42 extends JTree 43 { 44 45 private static final long serialVersionUID = 3978144335541975344L; 46 47 51 52 public DOMTreeFull() { 53 this(null); 54 } 55 56 57 public DOMTreeFull(Node root) { 58 super(new Model ()); 59 60 setRootVisible(false); 62 63 setRootNode(root); 65 66 } 68 72 73 public void setRootNode(Node root) { 74 ((Model )getModel()).setRootNode(root); 75 expandRow(0); 76 } 77 78 79 public Node getRootNode() { 80 return ((Model )getModel()).getRootNode(); 81 } 82 83 84 public Node getNode(Object treeNode) { 85 return ((Model )getModel()).getNode(treeNode); 86 } 87 88 89 public TreeNode getTreeNode(Object node) { 90 return ((Model )getModel()).getTreeNode(node); 91 } 92 93 97 101 public static class Model 102 extends DefaultTreeModel 103 implements Serializable 104 { 105 106 private static final long serialVersionUID = 3258131375181018673L; 107 108 112 113 private Node root; 114 115 private Hashtable nodeMap = new Hashtable (); 116 117 private Hashtable treeNodeMap = new Hashtable (); 118 119 123 124 public Model() { 125 this(null); 126 } 127 128 129 public Model(Node node) { 130 super(new DefaultMutableTreeNode ()); 131 if (node!=null) 132 setRootNode(node); 133 } 134 135 139 140 public synchronized void setRootNode(Node root) { 141 142 this.root = root; 144 145 DefaultMutableTreeNode where = (DefaultMutableTreeNode )getRoot(); 147 where.removeAllChildren(); 148 nodeMap.clear(); 149 treeNodeMap.clear(); 150 151 buildTree(root, where); 152 153 fireTreeStructureChanged(this, new Object [] { getRoot() }, new int[0], new Object [0]); 154 155 } 157 158 public Node getRootNode() { 159 return root; 160 } 161 162 163 public Node getNode(Object treeNode) { 164 return (Node )nodeMap.get(treeNode); 165 } 166 public Hashtable getAllNodes() { 167 return nodeMap; 168 } 169 170 public TreeNode getTreeNode(Object node) { 171 Object object = treeNodeMap.get(node); 172 return (TreeNode )object; 173 } 174 175 179 180 private void buildTree(Node node, MutableTreeNode where) { 181 182 if (node == null) { return; } 184 185 MutableTreeNode treeNode = insertNode(node, where); 186 187 NodeList nodes = node.getChildNodes(); 189 int len = (nodes != null) ? nodes.getLength() : 0; 190 for (int i = 0; i < len; i++) { 191 Node child = nodes.item(i); 192 buildTree(child, treeNode); 193 194 } 195 196 197 } 199 200 private MutableTreeNode insertNode(String what, MutableTreeNode where) { 201 202 MutableTreeNode node = new DefaultMutableTreeNode (what); 203 insertNodeInto(node, where, where.getChildCount()); 204 return node; 205 206 } 208 209 210 public MutableTreeNode insertNode(Node what, MutableTreeNode where) { 211 MutableTreeNode treeNode = insertNode(DOMTreeFull.toString(what), where); 212 nodeMap.put(treeNode, what); 213 treeNodeMap.put(what, treeNode); 214 return treeNode; 215 } 216 } 218 public static String whatArray[] = new String [] { 219 "ALL", 220 "ELEMENT", 221 "ATTRIBUTE", 222 "TEXT", 223 "CDATA_SECTION", 224 "ENTITY_REFERENCE", 225 "ENTITY", 226 "PROCESSING_INSTRUCTION", 227 "COMMENT", 228 "DOCUMENT", 229 "DOCUMENT_TYPE", 230 "DOCUMENT_FRAGMENT", 231 "NOTATION" 232 }; 233 237 public static String toString(Node node) { 238 StringBuffer sb = new StringBuffer (); 239 240 if (node == null) { 242 return ""; 243 } 244 245 int type = node.getNodeType(); 246 sb.append(whatArray[type]); 247 sb.append(" : "); 248 sb.append(node.getNodeName()); 249 String value = node.getNodeValue(); 250 if (value != null) { 251 sb.append(" Value: \""); 252 sb.append(value); 253 sb.append("\""); 254 } 255 256 switch (type) { 257 258 case Node.DOCUMENT_NODE: { 260 break; 261 } 262 263 case Node.ELEMENT_NODE: { 265 Attr attrs[] = sortAttributes(node.getAttributes()); 266 if (attrs.length > 0) 267 sb.append(" ATTRS:"); 268 for (int i = 0; i < attrs.length; i++) { 269 Attr attr = attrs[i]; 270 271 sb.append(' '); 272 sb.append(attr.getNodeName()); 273 sb.append("=\""); 274 sb.append(normalize(attr.getNodeValue())); 275 sb.append('"'); 276 } 277 sb.append('>'); 278 break; 279 } 280 281 case Node.ENTITY_REFERENCE_NODE: { 283 break; 284 } 285 286 case Node.CDATA_SECTION_NODE: { 288 break; 289 } 290 291 case Node.TEXT_NODE: { 293 break; 294 } 295 296 case Node.PROCESSING_INSTRUCTION_NODE: { 298 break; 299 } 300 301 case Node.COMMENT_NODE: { 303 break; 304 } 305 case Node.DOCUMENT_TYPE_NODE: { 307 break; 308 } 309 case Node.NOTATION_NODE: { 311 sb.append("public:"); 312 String id = ((Notation )node).getPublicId(); 313 if (id == null) { 314 sb.append("PUBLIC "); 315 sb.append(id); 316 sb.append(" "); 317 } 318 id = ((Notation )node).getSystemId(); 319 if (id == null) { 320 sb.append("system: "); 321 sb.append(id); 322 sb.append(" "); 323 } 324 break; 325 } 326 } 327 return sb.toString(); 328 } 329 330 331 static protected String normalize(String s) { 332 StringBuffer str = new StringBuffer (); 333 334 int len = (s != null) ? s.length() : 0; 335 for (int i = 0; i < len; i++) { 336 char ch = s.charAt(i); 337 switch (ch) { 338 case '<': { 339 str.append("<"); 340 break; 341 } 342 case '>': { 343 str.append(">"); 344 break; 345 } 346 case '&': { 347 str.append("&"); 348 break; 349 } 350 case '"': { 351 str.append("""); 352 break; 353 } 354 case '\r': 355 case '\n': 356 default: { 357 str.append(ch); 358 } 359 } 360 } 361 362 return str.toString(); 363 364 } 366 367 static protected Attr [] sortAttributes(NamedNodeMap attrs) { 368 369 int len = (attrs != null) ? attrs.getLength() : 0; 370 Attr array[] = new Attr [len]; 371 for (int i = 0; i < len; i++) { 372 array[i] = (Attr )attrs.item(i); 373 } 374 for (int i = 0; i < len - 1; i++) { 375 String name = array[i].getNodeName(); 376 int index = i; 377 for (int j = i + 1; j < len; j++) { 378 String curName = array[j].getNodeName(); 379 if (curName.compareTo(name) < 0) { 380 name = curName; 381 index = j; 382 } 383 } 384 if (index != i) { 385 Attr temp = array[i]; 386 array[i] = array[index]; 387 array[index] = temp; 388 } 389 } 390 391 return array; 392 393 } 395 } 397 | Popular Tags |