1 57 package dom.traversal; 58 59 import java.awt.BorderLayout ; 60 import java.awt.Dimension ; 61 import java.awt.Font ; 62 import java.awt.event.ActionEvent ; 63 import java.awt.event.ActionListener ; 64 import java.util.Enumeration ; 65 import java.util.Hashtable ; 66 67 import javax.swing.BorderFactory ; 68 import javax.swing.JButton ; 69 import javax.swing.JCheckBox ; 70 import javax.swing.JFrame ; 71 import javax.swing.JList ; 72 import javax.swing.JPanel ; 73 import javax.swing.JScrollPane ; 74 import javax.swing.JTextArea ; 75 import javax.swing.JTextField ; 76 import javax.swing.event.ListSelectionEvent ; 77 import javax.swing.event.ListSelectionListener ; 78 import javax.swing.event.TreeSelectionEvent ; 79 import javax.swing.event.TreeSelectionListener ; 80 import javax.swing.tree.DefaultTreeModel ; 81 import javax.swing.tree.MutableTreeNode ; 82 import javax.swing.tree.TreeNode ; 83 import javax.swing.tree.TreePath ; 84 import javax.swing.tree.TreeSelectionModel ; 85 86 import org.enhydra.apache.xerces.parsers.DOMParser; 87 import org.w3c.dom.DOMException ; 88 import org.w3c.dom.Document ; 89 import org.w3c.dom.Node ; 90 import org.w3c.dom.traversal.DocumentTraversal; 91 import org.w3c.dom.traversal.NodeFilter; 92 import org.w3c.dom.traversal.TreeWalker; 93 import org.xml.sax.ErrorHandler ; 94 import org.xml.sax.SAXException ; 95 import org.xml.sax.SAXParseException ; 96 97 import ui.DOMTreeFull; 98 99 103 public class TreeWalkerView 104 extends JFrame 105 implements ActionListener { 106 107 Document document; 108 TreeNode lastSelected; 109 DOMParser parser; 110 JTextArea messageText; 111 JScrollPane messageScroll; 112 DOMTreeFull jtree; 113 TreeWalker treeWalker; 114 NameNodeFilter nameNodeFilter; 115 116 JButton nextButton; 117 JButton prevButton; 118 JButton removeButton; 119 JButton addButton; 120 JTextField addText; 121 JButton newIterator; 122 JList whatToShow; 123 JCheckBox match; 124 JTextField nameFilter; 125 126 JButton parentButton; 128 JButton nextSiblingButton; 129 JButton previousSiblingButton; 130 JButton firstChildButton; 131 JButton lastChildButton; 132 JButton currentButton; 133 134 String whatArray[] = new String [] { 135 "ALL", 136 "ELEMENT", 137 "ATTRIBUTE", 138 "TEXT", 139 "CDATA_SECTION", 140 "ENTITY_REFERENCE", 141 "ENTITY", 142 "PROCESSING_INSTRUCTION", 143 "COMMENT", 144 "DOCUMENT", 145 "DOCUMENT_TYPE", 146 "DOCUMENT_FRAGMENT", 147 "NOTATION" 148 }; 149 JCheckBox expandERs; 150 151 152 153 public static void main (String args[]) { 154 155 if (args.length > 0) { 156 String filename = args[0]; 157 try { 158 TreeWalkerView frame = new TreeWalkerView(filename) ; 159 frame.addWindowListener(new java.awt.event.WindowAdapter () { 160 public void windowClosing(java.awt.event.WindowEvent e) { 161 System.exit(0); 162 } 163 }); 164 frame.setSize(640, 700); 165 frame.setVisible(true); 166 } catch (Exception e) { 167 e.printStackTrace(System.err); 168 } 169 } 170 } 171 172 Hashtable treeNodeMap = new Hashtable (); 173 174 175 public TreeWalkerView (String filename) { 176 super("TreeWalkerView: "+filename); 177 try { 178 parser = new DOMParser(); 179 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true); 180 parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); 181 Errors errors = new Errors(); 182 parser.setErrorHandler(errors); 183 parser.parse(filename); 184 document = parser.getDocument(); 185 186 if (!document.isSupported("Traversal", "2.0")) { 187 throw new RuntimeException ("This DOM Document does not support Traversal"); 189 190 } 191 192 jtree = new DOMTreeFull((Node )document); 194 jtree.getSelectionModel().setSelectionMode 195 (TreeSelectionModel.SINGLE_TREE_SELECTION); 196 197 jtree.addTreeSelectionListener( 199 new TreeSelectionListener () { 200 public void valueChanged(TreeSelectionEvent e) { 201 TreePath path = (TreePath )e.getPath(); 202 TreeNode treeNode = (TreeNode )path.getLastPathComponent(); 203 if(jtree.getSelectionModel().isPathSelected(path)) 204 nodeSelected(treeNode); 205 } 206 } 207 ); 208 209 213 BorderLayout borderLayout = new BorderLayout (); 214 215 JPanel iteratePanel = new JPanel (); 217 iteratePanel.setBorder(BorderFactory.createCompoundBorder( 218 BorderFactory.createTitledBorder("Document Order Traversal"), 219 BorderFactory.createEmptyBorder(4, 4, 4, 4) 220 )); 221 222 prevButton = new JButton ("Previous"); 223 iteratePanel.add(prevButton); 224 prevButton.addActionListener(this); 225 226 nextButton = new JButton ("Next"); 227 iteratePanel.add(nextButton); 228 nextButton.addActionListener(this); 229 230 JPanel walkerPanel = new JPanel (); 232 walkerPanel.setLayout(new BorderLayout ()); 233 walkerPanel.setBorder(BorderFactory.createCompoundBorder( 234 BorderFactory.createTitledBorder("Walk"), 235 BorderFactory.createEmptyBorder(4, 4, 4, 4) 236 )); 237 238 parentButton = new JButton ("Parent"); 239 walkerPanel.add(parentButton, BorderLayout.NORTH); 240 parentButton.addActionListener(this); 241 242 243 JPanel childPanel = new JPanel (); 244 firstChildButton = new JButton ("First Child"); 245 childPanel.add(firstChildButton); 246 firstChildButton.addActionListener(this); 247 248 lastChildButton = new JButton ("Last Child"); 249 childPanel.add(lastChildButton); 250 lastChildButton.addActionListener(this); 251 walkerPanel.add(childPanel, BorderLayout.SOUTH); 252 253 nextSiblingButton = new JButton ("Next Sibling"); 254 walkerPanel.add(nextSiblingButton, BorderLayout.EAST); 255 nextSiblingButton.addActionListener(this); 256 257 previousSiblingButton = new JButton ("Previous Sibling"); 258 walkerPanel.add(previousSiblingButton, BorderLayout.WEST); 259 previousSiblingButton.addActionListener(this); 260 261 JPanel domPanel = new JPanel (); 263 domPanel.setLayout(new BorderLayout ()); 264 domPanel.setBorder(BorderFactory.createCompoundBorder( 265 BorderFactory.createTitledBorder("Selected Node"), 266 BorderFactory.createEmptyBorder(4, 4, 4, 4) 267 )); 268 269 JPanel buttonPanel = new JPanel (); 270 currentButton = new JButton ("Current"); 271 buttonPanel.add(currentButton); 272 currentButton.addActionListener(this); 273 274 removeButton = new JButton ("Remove"); 275 buttonPanel.add(removeButton); 276 removeButton.addActionListener(this); 277 278 addButton = new JButton ("Append Text"); 279 addText = new JTextField (10); 280 buttonPanel.add(addButton); 281 domPanel.add(buttonPanel, BorderLayout.NORTH); 282 domPanel.add(addText, BorderLayout.CENTER); 283 addButton.addActionListener(this); 284 285 JPanel settingsPanel = new JPanel (); 287 settingsPanel.setLayout(new BorderLayout ()); 288 settingsPanel.setBorder(BorderFactory.createCompoundBorder( 289 BorderFactory.createTitledBorder("Filter Settings"), 290 BorderFactory.createEmptyBorder(4, 4, 4, 4) 291 )); 292 JPanel treeWalkerPanel = new JPanel (); 293 treeWalkerPanel.setLayout(new BorderLayout ()); 294 295 newIterator = new JButton ("createTreeWalker"); 296 treeWalkerPanel.add(newIterator, BorderLayout.NORTH); 297 expandERs = new JCheckBox ("expandEntityReferences"); 298 expandERs.setSelected(true); 299 treeWalkerPanel.add(expandERs, BorderLayout.SOUTH); 300 settingsPanel.add(treeWalkerPanel, BorderLayout.NORTH); 301 newIterator.addActionListener(this); 302 303 JPanel whatPanel = new JPanel (); 304 whatPanel.setBorder(BorderFactory.createCompoundBorder( 305 BorderFactory.createTitledBorder("whatToShow"), 306 BorderFactory.createEmptyBorder(0, 0, 0, 0) 307 )); 308 whatToShow = new JList (whatArray); 309 JScrollPane whatScroll = 310 new JScrollPane (whatToShow) { 311 public Dimension getPreferredSize(){ 312 return new Dimension (200, 65 ); 313 } 314 }; 315 316 whatPanel.add(whatScroll); 317 318 319 JPanel filterPanel = new JPanel (); 320 filterPanel.setBorder(BorderFactory.createCompoundBorder( 321 BorderFactory.createTitledBorder("NodeNameFilter"), 322 BorderFactory.createEmptyBorder(4, 4, 4, 4) 323 )); 324 filterPanel.setLayout(new BorderLayout ()); 325 match = new JCheckBox ("match/ignore node name", true); 326 nameFilter = new JTextField (10); 327 filterPanel.add(match, BorderLayout.NORTH); 328 filterPanel.add(nameFilter, BorderLayout.SOUTH); 329 330 settingsPanel.add(treeWalkerPanel, BorderLayout.NORTH); 331 settingsPanel.add(whatPanel, BorderLayout.CENTER); 332 settingsPanel.add(filterPanel, BorderLayout.SOUTH); 333 334 335 whatToShow.addListSelectionListener( 337 new ListSelectionListener () { 338 public void valueChanged(ListSelectionEvent e) { 339 } 341 } 342 ); 343 344 345 JPanel controlsPanel = new JPanel (new BorderLayout ()); 346 controlsPanel.setFont(new Font ("Dialog", Font.PLAIN, 8)); 347 JPanel buttonsPanel = new JPanel (new BorderLayout ()); 348 buttonsPanel.add(iteratePanel, BorderLayout.NORTH); 349 buttonsPanel.add(walkerPanel, BorderLayout.CENTER); 350 buttonsPanel.add(domPanel, BorderLayout.SOUTH); 351 controlsPanel.add(buttonsPanel, BorderLayout.NORTH); 352 controlsPanel.add(settingsPanel, BorderLayout.CENTER); 353 354 controlsPanel.setBorder(BorderFactory.createCompoundBorder( 355 BorderFactory.createTitledBorder("Controls"), 356 BorderFactory.createEmptyBorder(4, 4, 4, 4) 357 )); 358 359 360 JPanel treePanel = new JPanel (new BorderLayout ()); 362 363 JScrollPane treeScroll = new JScrollPane (jtree) ; 364 treeScroll.setBorder(BorderFactory.createCompoundBorder( 365 BorderFactory.createTitledBorder("Tree View"), 366 BorderFactory.createEmptyBorder(4, 4, 4, 4) 367 )); 368 369 messageText = new JTextArea (3,5); 371 372 JPanel messagePanel = new JPanel (new BorderLayout ()); 373 messageScroll = new JScrollPane (messageText); 374 messagePanel.add(messageScroll); 375 messagePanel.setBorder(BorderFactory.createCompoundBorder( 376 BorderFactory.createTitledBorder("Messages"), 377 BorderFactory.createEmptyBorder(4, 4, 4, 4) 378 )); 379 380 JPanel mainPanel = new JPanel (); 381 mainPanel.setLayout(new BorderLayout ()); 382 mainPanel.add(controlsPanel, BorderLayout.EAST); 383 mainPanel.add(treeScroll, BorderLayout.CENTER); 384 mainPanel.add(messagePanel, BorderLayout.SOUTH); 385 getContentPane().add(mainPanel); 386 387 Hashtable errorNodes = errors.getErrorNodes(); 388 Enumeration elements = errorNodes.elements(); 389 while (elements.hasMoreElements()) { 390 messageText.append( (String )elements.nextElement()); 392 } 393 394 boolean expand = expandERs.isSelected(); 395 treeWalker = ((DocumentTraversal)document). 396 createTreeWalker( 397 document, 398 NodeFilter.SHOW_ALL, 399 new NameNodeFilter(), 400 expand); 401 402 } catch (Exception e) { 403 e.printStackTrace(System.err); 404 } 405 } 406 407 public void actionPerformed(ActionEvent e) { 408 409 if (e.getSource() == newIterator) { 410 411 TreeNode treeNode = (TreeNode )jtree.getLastSelectedPathComponent(); 412 if (treeNode == null) { 413 messageText.append("Must select a tree component."); 414 return; 415 } 416 417 Node node = jtree.getNode(treeNode); 418 if (node == null) { 419 setMessage("No current Node in TreeNode: "+node); 420 } 421 422 int [] indices = whatToShow.getSelectedIndices(); 424 int mask = 0x0; 425 for (int i = 0; i < indices.length; i++) { 426 if (indices[i] == 0) { 427 mask = 0xFFFF; 428 break; 429 } else { 430 mask = mask | 1<<indices[i]-1; 431 } 432 } 433 434 String nameText = nameFilter.getText(); 436 boolean matched = match.isSelected(); 437 if (nameNodeFilter==null) { 438 nameNodeFilter = new NameNodeFilter(); 439 } 440 if (nameText.equals("")) { 441 setMessage("NodeNameFilter name is \"\". Assuming null."); 442 nameText = null; 443 } 444 nameNodeFilter.setName(nameText); 445 nameNodeFilter.setMatch(matched); 446 447 boolean expand = expandERs.isSelected(); 449 450 treeWalker = ((DocumentTraversal)document). 451 createTreeWalker( 452 node, 453 mask, 454 nameNodeFilter, 455 expand); 456 setMessage("createTreeWalker:"+ 457 " root="+node+ 458 ", whatToShow="+mask+ 459 ", match="+matched+ 460 ", name="+nameText 461 ); 462 return; 463 464 } 465 466 if (e.getSource() == currentButton) { 467 468 TreeNode treeNode = (TreeNode )jtree.getLastSelectedPathComponent(); 469 if (treeNode == null) { 470 messageText.append("Must select a tree component."); 471 return; 472 } 473 474 Node node = jtree.getNode(treeNode); 475 if (node == null) { 476 setMessage("No current Node in TreeNode: "+node); 477 } 478 treeWalker.setCurrentNode(node); 479 return; 480 481 } 482 if (e.getSource() == addButton) { 483 484 String text = addText.getText(); 485 486 if (text==null) return; 487 488 TreeNode treeNode = (TreeNode )jtree.getLastSelectedPathComponent(); 489 if (treeNode == null) { 490 messageText.append("Must select a tree component to add a child to it."); 491 return; 492 } 493 TreePath path = new TreePath ( 494 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 495 if (path == null) { 496 setMessage("Could not create a path."); 497 return; 498 } 499 if(!jtree.getSelectionModel().isPathSelected(path)) 500 return; 501 Node node = jtree.getNode(treeNode); 502 Node textNode = document.createTextNode(text); 503 try { 504 node.appendChild(textNode); 505 } catch (DOMException dome) { 506 setMessage("DOMException:"+dome.code+", "+dome); 507 return; 508 } 509 ((DOMTreeFull.Model)jtree.getModel()).insertNode(textNode, (MutableTreeNode )treeNode); 510 511 return; 512 } 513 514 if (e.getSource() == removeButton) { 515 516 517 TreeNode treeNode = (TreeNode )jtree.getLastSelectedPathComponent(); 518 if (treeNode == null) { 519 messageText.append("Must select a tree component to remove it."); 520 return; 521 } 522 TreePath path = new TreePath ( 523 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 524 if (path == null) { 525 setMessage("Could not create a path."); 526 return; 527 } 528 if(!jtree.getSelectionModel().isPathSelected(path)) 529 return; 530 Node node = jtree.getNode(treeNode); 531 if (node == null) return; 532 Node parent = node.getParentNode(); 533 if (parent == null) return; 534 535 parent.removeChild(node); 536 537 ((DefaultTreeModel )jtree.getModel()).removeNodeFromParent((MutableTreeNode )treeNode); 538 return; 539 } 540 541 if (e.getSource() == previousSiblingButton) { 542 Node node = treeWalker.previousSibling(); 543 handleButton(node, "previousSibling()"); 544 return; 545 } 546 547 if (e.getSource() == firstChildButton) { 548 Node node = treeWalker.firstChild(); 549 handleButton(node, "firstChild()"); 550 return; 551 } 552 553 if (e.getSource() == lastChildButton) { 554 Node node = treeWalker.lastChild(); 555 handleButton(node, "lastChild()"); 556 return; 557 } 558 559 if (e.getSource() == nextSiblingButton) { 560 Node node = treeWalker.nextSibling(); 561 handleButton(node, "nextSibling()"); 562 return; 563 } 564 565 if (e.getSource() == parentButton) { 566 Node node = treeWalker.parentNode(); 567 handleButton(node, "parentNode()"); 568 return; 569 } 570 571 if (e.getSource() == nextButton) { 572 Node node = treeWalker.nextNode(); 573 handleButton(node, "nextNode()"); 574 return; 575 } 576 577 if (e.getSource() == prevButton) { 578 Node node = treeWalker.previousNode(); 579 handleButton(node, "previousNode()"); 580 return; 581 } 582 583 } 584 585 586 void handleButton( Node node, String function) { 587 588 setMessage("treeWalker."+function+" == "+node); 589 590 if (node==null) return; 591 592 TreeNode treeNode = jtree.getTreeNode(node); 593 if (treeNode == null) { 594 setMessage("No JTree TreeNode for Node name:" + node.getNodeName()); 595 return; 596 } 597 598 TreePath path = new TreePath ( 599 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 600 jtree.requestFocus(); 601 jtree.setSelectionPath(path); 602 jtree.scrollPathToVisible(path); 603 } 604 605 606 void setMessage(String string) { 607 messageText.selectAll(); 608 messageText.cut(); 609 messageText.append(string); 610 messageText.setCaretPosition(0); 611 } 612 613 615 void nodeSelected(TreeNode treeNode) { 616 617 lastSelected = treeNode; 618 Node node = jtree.getNode(treeNode); 619 620 if (node == null) return; 621 622 setMessage(DOMTreeFull.toString(node)); 623 } 624 625 626 void expandTree() { 627 for (int i = 0; i < jtree.getRowCount(); i++) { 628 jtree.expandRow(i); 629 } 630 } 631 632 class Errors implements ErrorHandler { 633 634 Hashtable errorNodes = new Hashtable (); 635 636 public void warning(SAXParseException ex) { 637 store(ex, "[Warning]"); 638 } 639 640 public void error(SAXParseException ex) { 641 store(ex, "[Error]"); 642 } 643 644 public void fatalError(SAXParseException ex) throws SAXException { 645 store(ex, "[Fatal Error]"); 646 } 647 648 public Hashtable getErrorNodes() { 649 return errorNodes; 650 } 651 652 public Object getError(Node node) { 653 return errorNodes.get(node); 654 } 655 656 public void clearErrors() { 657 errorNodes.clear(); 658 } 659 660 void store(SAXParseException ex, String type) { 661 662 String errorString= type+" at line number, "+ex.getLineNumber() 664 +": "+ex.getMessage()+"\n"; 665 666 Node currentNode = null; 667 try { 668 currentNode = (Node )parser.getProperty("http://apache.org/xml/properties/dom-node"); 669 } catch (SAXException se) { 670 System.err.println(se.getMessage()); 671 return; 672 } 673 if (currentNode == null) return; 674 675 String previous = (String ) errorNodes.get(currentNode); 677 if (previous != null) 678 errorNodes.put(currentNode, previous +errorString); 679 else 680 errorNodes.put(currentNode, errorString); 681 } 682 } 683 684 } 685 | Popular Tags |