1 16 package dom.traversal; 17 18 import java.awt.BorderLayout ; 19 import java.awt.Dimension ; 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 25 import javax.swing.BorderFactory ; 26 import javax.swing.JButton ; 27 import javax.swing.JCheckBox ; 28 import javax.swing.JFrame ; 29 import javax.swing.JList ; 30 import javax.swing.JPanel ; 31 import javax.swing.JScrollPane ; 32 import javax.swing.JTextArea ; 33 import javax.swing.JTextField ; 34 import javax.swing.event.ListSelectionEvent ; 35 import javax.swing.event.ListSelectionListener ; 36 import javax.swing.event.TreeSelectionEvent ; 37 import javax.swing.event.TreeSelectionListener ; 38 import javax.swing.tree.DefaultTreeModel ; 39 import javax.swing.tree.MutableTreeNode ; 40 import javax.swing.tree.TreeNode ; 41 import javax.swing.tree.TreePath ; 42 import javax.swing.tree.TreeSelectionModel ; 43 44 import org.apache.xerces.parsers.DOMParser; 45 import org.w3c.dom.DOMException ; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Node ; 48 import org.w3c.dom.traversal.DocumentTraversal; 49 import org.w3c.dom.traversal.NodeFilter; 50 import org.w3c.dom.traversal.NodeIterator; 51 import org.xml.sax.ErrorHandler ; 52 import org.xml.sax.SAXException ; 53 import org.xml.sax.SAXParseException ; 54 55 import ui.DOMTreeFull; 56 57 61 public class IteratorView 62 extends JFrame 63 implements ActionListener { 64 65 private static final long serialVersionUID = 3256726186452662580L; 66 67 Document document; 68 TreeNode lastSelected; 69 DOMParser parser; 70 JTextArea messageText; 71 JScrollPane messageScroll; 72 DOMTreeFull jtree; 73 NodeIterator iterator; 74 NameNodeFilter nameNodeFilter; 75 76 JButton nextButton; 77 JButton prevButton; 78 JButton removeButton; 79 JButton addButton; 80 JTextField addText; 81 JButton newIterator; 82 JList whatToShow; 83 JCheckBox match; 84 JTextField nameFilter; 85 String whatArray[] = new String [] { 86 "ALL", 87 "ELEMENT", 88 "ATTRIBUTE", 89 "TEXT", 90 "CDATA_SECTION", 91 "ENTITY_REFERENCE", 92 "ENTITY", 93 "PROCESSING_INSTRUCTION", 94 "COMMENT", 95 "DOCUMENT", 96 "DOCUMENT_TYPE", 97 "DOCUMENT_FRAGMENT", 98 "NOTATION" 99 }; 100 JCheckBox expandERs; 101 102 Hashtable treeNodeMap = new Hashtable (); 103 104 105 106 public static void main (String args[]) { 107 108 if (args.length > 0) { 109 String filename = args[0]; 110 try { 111 IteratorView frame = new IteratorView(filename) ; 112 frame.addWindowListener(new java.awt.event.WindowAdapter () { 113 public void windowClosing(java.awt.event.WindowEvent e) { 114 System.exit(0); 115 } 116 }); 117 frame.setSize(640, 480); 118 frame.setVisible(true); 119 } catch (Exception e) { 120 e.printStackTrace(System.err); 121 } 122 } 123 } 124 125 126 public IteratorView (String filename) { 127 super("IteratorView: "+filename); 128 try { 129 parser = new DOMParser(); 130 parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true); 131 parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); 132 Errors errors = new Errors(); 133 parser.setErrorHandler(errors); 134 parser.parse(filename); 135 document = parser.getDocument(); 136 137 if (!document.isSupported("Traversal", "2.0")) { 138 throw new RuntimeException ("This DOM Document does not support Traversal"); 140 } 141 142 jtree = new DOMTreeFull((Node )document); 144 jtree.getSelectionModel().setSelectionMode 145 (TreeSelectionModel.SINGLE_TREE_SELECTION); 146 jtree.setRootVisible(false); 147 148 jtree.addTreeSelectionListener( 150 new TreeSelectionListener () { 151 public void valueChanged(TreeSelectionEvent e) { 152 TreePath path = (TreePath )e.getPath(); 153 TreeNode treeNode = (TreeNode )path.getLastPathComponent(); 154 if(jtree.getSelectionModel().isPathSelected(path)) 155 nodeSelected(treeNode); 156 } 157 } 158 ); 159 160 161 163 164 166 JPanel iteratePanel = new JPanel (); 168 iteratePanel.setBorder(BorderFactory.createCompoundBorder( 169 BorderFactory.createTitledBorder("Iterate"), 170 BorderFactory.createEmptyBorder(4, 4, 4, 4) 171 )); 172 173 prevButton = new JButton ("Previous"); 174 iteratePanel.add(prevButton); 175 prevButton.addActionListener(this); 176 177 nextButton = new JButton ("Next"); 178 iteratePanel.add(nextButton); 179 nextButton.addActionListener(this); 180 181 JPanel domPanel = new JPanel (); 183 domPanel.setLayout(new BorderLayout ()); 184 domPanel.setBorder(BorderFactory.createCompoundBorder( 185 BorderFactory.createTitledBorder("Selected Node"), 186 BorderFactory.createEmptyBorder(4, 4, 4, 4) 187 )); 188 189 removeButton = new JButton ("Remove Selected Node"); 190 domPanel.add(removeButton, BorderLayout.NORTH); 191 removeButton.addActionListener(this); 192 193 addButton = new JButton ("Add Text Node"); 194 addText = new JTextField (10); 195 domPanel.add(addButton, BorderLayout.CENTER); 196 domPanel.add(addText, BorderLayout.SOUTH); 197 addButton.addActionListener(this); 198 199 JPanel settingsPanel = new JPanel (); 201 settingsPanel.setLayout(new BorderLayout ()); 202 settingsPanel.setBorder(BorderFactory.createCompoundBorder( 203 BorderFactory.createTitledBorder("Iterator Settings"), 204 BorderFactory.createEmptyBorder(4, 4, 4, 4) 205 )); 206 JPanel iteratorPanel = new JPanel (); 207 newIterator = new JButton ("createNodeIterator"); 208 expandERs = new JCheckBox ("expandEntityReferences"); 209 iteratorPanel.add(newIterator); 210 expandERs.setSelected(true); 211 iteratorPanel.add(expandERs); 212 settingsPanel.add(iteratorPanel, BorderLayout.NORTH); 213 newIterator.addActionListener(this); 214 215 JPanel whatPanel = new JPanel (); 216 whatPanel.setBorder(BorderFactory.createCompoundBorder( 217 BorderFactory.createTitledBorder("whatToShow"), 218 BorderFactory.createEmptyBorder(4, 4, 4, 4) 219 )); 220 whatToShow = new JList (DOMTreeFull.whatArray); 221 JScrollPane whatScroll = 222 new JScrollPane (whatToShow) { 223 private static final long serialVersionUID = 3546357344813724213L; 224 public Dimension getPreferredSize(){ 225 return new Dimension (200, 75 ); 226 } 227 }; 228 229 whatPanel.add(whatScroll); 230 231 232 JPanel filterPanel = new JPanel (); 233 filterPanel.setBorder(BorderFactory.createCompoundBorder( 234 BorderFactory.createTitledBorder("NodeNameFilter"), 235 BorderFactory.createEmptyBorder(4, 4, 4, 4) 236 )); 237 filterPanel.setLayout(new BorderLayout ()); 238 match = new JCheckBox ("match/ignore node name", true); 239 nameFilter = new JTextField (10); 240 filterPanel.add(match, BorderLayout.NORTH); 241 filterPanel.add(nameFilter, BorderLayout.SOUTH); 242 243 settingsPanel.add(whatPanel, BorderLayout.WEST); 244 settingsPanel.add(filterPanel, BorderLayout.EAST); 245 246 247 whatToShow.addListSelectionListener( 249 new ListSelectionListener () { 250 public void valueChanged(ListSelectionEvent e) { 251 } 253 } 254 ); 255 256 257 JPanel controlsPanel = new JPanel (new BorderLayout ()); 258 JPanel buttonsPanel = new JPanel (new BorderLayout ()); 259 buttonsPanel.add(iteratePanel, BorderLayout.NORTH); 260 buttonsPanel.add(domPanel, BorderLayout.SOUTH); 261 controlsPanel.add(buttonsPanel, BorderLayout.WEST); 262 controlsPanel.add(settingsPanel, BorderLayout.CENTER); 263 264 controlsPanel.setBorder(BorderFactory.createCompoundBorder( 265 BorderFactory.createTitledBorder("Controls"), 266 BorderFactory.createEmptyBorder(4, 4, 4, 4) 267 )); 268 269 270 JPanel treePanel = new JPanel (new BorderLayout ()); 272 273 JScrollPane treeScroll = new JScrollPane (jtree) ; 274 treeScroll.setBorder(BorderFactory.createCompoundBorder( 275 BorderFactory.createTitledBorder("Tree View"), 276 BorderFactory.createEmptyBorder(4, 4, 4, 4) 277 )); 278 279 messageText = new JTextArea (3,5); 281 282 JPanel messagePanel = new JPanel (new BorderLayout ()); 283 messageScroll = new JScrollPane (messageText); 284 messagePanel.add(messageScroll); 285 messagePanel.setBorder(BorderFactory.createCompoundBorder( 286 BorderFactory.createTitledBorder("Messages"), 287 BorderFactory.createEmptyBorder(4, 4, 4, 4) 288 )); 289 290 JPanel mainPanel = new JPanel (); 291 mainPanel.setLayout(new BorderLayout ()); 292 mainPanel.add(controlsPanel, BorderLayout.NORTH); 293 mainPanel.add(treeScroll, BorderLayout.CENTER); 294 mainPanel.add(messagePanel, BorderLayout.SOUTH); 295 getContentPane().add(mainPanel); 296 297 Hashtable errorNodes = errors.getErrorNodes(); 298 Enumeration elements = errorNodes.elements(); 299 while (elements.hasMoreElements()) { 300 messageText.append( (String )elements.nextElement()); 302 } 303 304 iterator = ((DocumentTraversal)document). 307 createNodeIterator( 308 document, 309 NodeFilter.SHOW_ALL, 310 new NameNodeFilter(), 311 true); 312 313 } catch (Exception e) { 314 e.printStackTrace(System.err); 315 } 316 } 317 318 public void actionPerformed(ActionEvent e) { 319 320 if (e.getSource() == newIterator) { 321 Node node = document; 322 323 int [] indices = whatToShow.getSelectedIndices(); 325 int mask = 0x0; 326 for (int i = 0; i < indices.length; i++) { 327 if (indices[i] == 0) { 328 mask = 0xFFFF; 329 break; 330 } else { 331 mask = mask | 1<<indices[i]-1; 332 } 333 } 334 String nameText = nameFilter.getText(); 336 boolean matched = match.isSelected(); 337 if (nameNodeFilter==null) { 338 nameNodeFilter = new NameNodeFilter(); 339 } 340 if (nameText.equals("")) { 341 setMessage("NodeNameFilter name is \"\". Assuming null."); 342 nameText = null; 343 } 344 nameNodeFilter.setName(nameText); 345 nameNodeFilter.setMatch(matched); 346 347 if (iterator !=null) iterator.detach(); 348 boolean expand = expandERs.isSelected(); 349 iterator = ((DocumentTraversal)document). 350 createNodeIterator( 351 node, 352 (short)mask, 353 nameNodeFilter, 354 expand); 355 setMessage("doc.createNodeIterator("+ 356 " root="+node+ 357 ", whatToShow="+mask+ 358 ", match="+matched+ 359 ", name="+nameText+")" 360 ); 361 return; 362 363 } 364 365 if (e.getSource() == addButton) { 366 367 String text = addText.getText(); 368 369 if (text==null) return; 370 371 TreeNode treeNode = (TreeNode )jtree.getLastSelectedPathComponent(); 372 if (treeNode == null) { 373 messageText.append("Must select a tree component to add a child to it."); 374 return; 375 } 376 TreePath path = new TreePath ( 377 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 378 if (path == null) { 379 setMessage("Could not create a path."); 380 return; 381 } 382 if(!jtree.getSelectionModel().isPathSelected(path)) 383 return; 384 Node node = jtree.getNode(treeNode); 385 Node textNode = document.createTextNode(text); 386 try { 387 node.appendChild(textNode); 388 } catch (DOMException dome) { 389 setMessage("DOMException:"+dome.code+", "+dome); 390 return; 391 } 392 ((DOMTreeFull.Model)jtree.getModel()).insertNode(textNode, (MutableTreeNode )treeNode); 393 394 return; 395 } 396 if (e.getSource() == nextButton) { 397 Node node = iterator.nextNode(); 398 if (node==null) { 399 setMessage("iterator.nextNode() == null"); 400 return; 401 } 402 403 setMessage("iterator.nextNode() == "+node); 404 405 TreeNode treeNode = jtree.getTreeNode(node); 406 if (treeNode == null) { 407 setMessage("No JTree TreeNode for Node name:" + node.getNodeName()); 408 return; 409 } 410 411 TreePath path = new TreePath ( 412 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 413 jtree.requestFocus(); 414 jtree.setSelectionPath(path); 415 jtree.scrollPathToVisible(path); 416 return; 417 } 418 419 if (e.getSource() == prevButton) { 420 Node node = iterator.previousNode(); 421 if (node==null) { 422 setMessage("iterator.previousNode() == null"); 423 return; 424 } 425 426 setMessage("iterator.previousNode() == "+node); 427 428 TreeNode treeNode = jtree.getTreeNode(node); 429 if (treeNode == null) { 430 setMessage("No JTree TreeNode for Node name:" + node.getNodeName()); 431 return; 432 } 433 434 TreePath path = new TreePath ( 435 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 436 jtree.requestFocus(); 437 jtree.setSelectionPath(path); 438 jtree.scrollPathToVisible(path); 439 return; 440 } 441 if (e.getSource() == removeButton) { 442 443 444 TreeNode treeNode = (TreeNode )jtree.getLastSelectedPathComponent(); 445 if (treeNode == null) { 446 messageText.append("Must select a tree component to remove it."); 447 return; 448 } 449 TreePath path = new TreePath ( 450 ((DefaultTreeModel )jtree.getModel()).getPathToRoot(treeNode)); 451 if (path == null) { 452 setMessage("Could not create a path."); 453 return; 454 } 455 if(!jtree.getSelectionModel().isPathSelected(path)) 456 return; 457 Node node = jtree.getNode(treeNode); 458 if (node == null) return; 459 Node parent = node.getParentNode(); 460 if (parent == null) return; 461 462 parent.removeChild(node); 463 464 ((DefaultTreeModel )jtree.getModel()).removeNodeFromParent((MutableTreeNode )treeNode); 465 return; 466 } 467 468 } 469 470 471 void setMessage(String string) { 472 messageText.selectAll(); 473 messageText.cut(); 474 messageText.append(string); 475 messageText.setCaretPosition(0); 476 } 477 478 480 void nodeSelected(TreeNode treeNode) { 481 482 lastSelected = treeNode; 483 Node node = jtree.getNode(treeNode); 484 485 System.out.println("nodeSelected.node="+node); 486 if (node == null) return; 487 488 setMessage(DOMTreeFull.toString(node)); 489 } 490 491 492 void expandTree() { 493 for (int i = 0; i < jtree.getRowCount(); i++) { 494 jtree.expandRow(i); 495 } 496 } 497 498 class Errors implements ErrorHandler { 499 500 Hashtable errorNodes = new Hashtable (); 501 502 public void warning(SAXParseException ex) { 503 store(ex, "[Warning]"); 504 } 505 506 public void error(SAXParseException ex) { 507 store(ex, "[Error]"); 508 } 509 510 public void fatalError(SAXParseException ex) throws SAXException { 511 store(ex, "[Fatal Error]"); 512 } 513 514 public Hashtable getErrorNodes() { 515 return errorNodes; 516 } 517 518 public Object getError(Node node) { 519 return errorNodes.get(node); 520 } 521 522 public void clearErrors() { 523 errorNodes.clear(); 524 } 525 526 void store(SAXParseException ex, String type) { 527 528 String errorString= type+" at line number, "+ex.getLineNumber() 530 +": "+ex.getMessage()+"\n"; 531 Node currentNode = null; 532 try { 533 currentNode = (Node )parser.getProperty("http://apache.org/xml/properties/dom-node"); 534 } catch (SAXException se) { 535 System.err.println(se.getMessage()); 536 return; 537 } 538 if (currentNode == null) return; 539 540 String previous = (String ) errorNodes.get(currentNode); 542 if (previous != null) 543 errorNodes.put(currentNode, previous +errorString); 544 else 545 errorNodes.put(currentNode, errorString); 546 } 547 } 548 549 } 550 | Popular Tags |