~ubuntu-branches/ubuntu/karmic/libxerces2-java/karmic

« back to all changes in this revision

Viewing changes to samples/dom/traversal/IteratorView.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2004-06-06 18:00:26 UTC
  • Revision ID: james.westby@ubuntu.com-20040606180026-a3vh56uc95hjbyfh
Tags: upstream-2.6.2
ImportĀ upstreamĀ versionĀ 2.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The Apache Software License, Version 1.1
 
3
 *
 
4
 *
 
5
 * Copyright (c) 1999,2000 The Apache Software Foundation.  All rights 
 
6
 * reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
11
 *
 
12
 * 1. Redistributions of source code must retain the above copyright
 
13
 *    notice, this list of conditions and the following disclaimer. 
 
14
 *
 
15
 * 2. Redistributions in binary form must reproduce the above copyright
 
16
 *    notice, this list of conditions and the following disclaimer in
 
17
 *    the documentation and/or other materials provided with the
 
18
 *    distribution.
 
19
 *
 
20
 * 3. The end-user documentation included with the redistribution,
 
21
 *    if any, must include the following acknowledgment:  
 
22
 *       "This product includes software developed by the
 
23
 *        Apache Software Foundation (http://www.apache.org/)."
 
24
 *    Alternately, this acknowledgment may appear in the software itself,
 
25
 *    if and wherever such third-party acknowledgments normally appear.
 
26
 *
 
27
 * 4. The names "Xerces" and "Apache Software Foundation" must
 
28
 *    not be used to endorse or promote products derived from this
 
29
 *    software without prior written permission. For written 
 
30
 *    permission, please contact apache@apache.org.
 
31
 *
 
32
 * 5. Products derived from this software may not be called "Apache",
 
33
 *    nor may "Apache" appear in their name, without prior written
 
34
 *    permission of the Apache Software Foundation.
 
35
 *
 
36
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 
37
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
38
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
39
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 
40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
42
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 
43
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
44
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
45
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 
46
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
47
 * SUCH DAMAGE.
 
48
 * ====================================================================
 
49
 *
 
50
 * This software consists of voluntary contributions made by many
 
51
 * individuals on behalf of the Apache Software Foundation and was
 
52
 * originally based on software copyright (c) 1999, International
 
53
 * Business Machines, Inc., http://www.apache.org.  For more
 
54
 * information on the Apache Software Foundation, please see
 
55
 * <http://www.apache.org/>.
 
56
 */
 
57
package dom.traversal;
 
58
 
 
59
import java.awt.*;
 
60
import java.awt.event.*;
 
61
import java.util.Hashtable;
 
62
import java.util.Enumeration;
 
63
import javax.swing.*;
 
64
import javax.swing.tree.*;
 
65
import javax.swing.event.*;
 
66
import org.apache.xerces.parsers.*;
 
67
import org.w3c.dom.*;
 
68
import org.w3c.dom.traversal.*;
 
69
import org.xml.sax.ErrorHandler;
 
70
import org.xml.sax.SAXException;
 
71
import org.xml.sax.SAXParseException;
 
72
 
 
73
import ui.DOMTreeFull;
 
74
 
 
75
/** This class shows a DOM Document in a JTree, and presents controls
 
76
 *  which allow the user to create and view the progress of a NodeIterator
 
77
 *  in the DOM tree.
 
78
 */
 
79
public class IteratorView 
 
80
    extends JFrame 
 
81
    implements ActionListener {
 
82
 
 
83
    Document document;
 
84
    TreeNode lastSelected;
 
85
    DOMParser parser;
 
86
    JTextArea messageText;
 
87
    JScrollPane messageScroll;
 
88
    DOMTreeFull jtree;
 
89
    NodeIterator iterator;
 
90
    NameNodeFilter nameNodeFilter;
 
91
    
 
92
    JButton nextButton;
 
93
    JButton prevButton;
 
94
    JButton removeButton;
 
95
    JButton addButton;
 
96
    JTextField addText;
 
97
    JButton newIterator;
 
98
    JList whatToShow;
 
99
    JCheckBox match;
 
100
    JTextField nameFilter;
 
101
    String whatArray[] = new String [] { 
 
102
            "ALL",
 
103
            "ELEMENT",
 
104
            "ATTRIBUTE",
 
105
            "TEXT",
 
106
            "CDATA_SECTION",
 
107
            "ENTITY_REFERENCE",
 
108
            "ENTITY",
 
109
            "PROCESSING_INSTRUCTION", 
 
110
            "COMMENT", 
 
111
            "DOCUMENT", 
 
112
            "DOCUMENT_TYPE",
 
113
            "DOCUMENT_FRAGMENT",
 
114
            "NOTATION" 
 
115
            };
 
116
    JCheckBox expandERs;
 
117
    
 
118
    Hashtable treeNodeMap = new Hashtable();
 
119
    
 
120
    
 
121
    /** main */
 
122
    public static void main (String args[]) {
 
123
 
 
124
        if (args.length > 0) {
 
125
            String filename = args[0];
 
126
            try {
 
127
                IteratorView frame = new IteratorView(filename) ;
 
128
                frame.addWindowListener(new java.awt.event.WindowAdapter() {
 
129
                 public void windowClosing(java.awt.event.WindowEvent e) {
 
130
                     System.exit(0);
 
131
                 }
 
132
                });
 
133
                frame.setSize(640, 480);
 
134
                frame.setVisible(true);
 
135
                } catch (Exception e) {
 
136
                    e.printStackTrace(System.err);
 
137
                }
 
138
        }
 
139
    }
 
140
    
 
141
    /** Constructor */
 
142
    public IteratorView (String filename) {
 
143
        super("IteratorView: "+filename);
 
144
        try {
 
145
            parser = new DOMParser();
 
146
            parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
 
147
            parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
 
148
            Errors errors = new Errors();
 
149
            parser.setErrorHandler(errors);
 
150
            parser.parse(filename);
 
151
            document = parser.getDocument();
 
152
            
 
153
            if (!document.isSupported("Traversal", "2.0")) {
 
154
                // This cannot happen with ou DOMParser...
 
155
                throw new RuntimeException("This DOM Document does not support Traversal");
 
156
            }
 
157
 
 
158
            // jtree  UI setup
 
159
            jtree = new DOMTreeFull((Node)document);
 
160
            jtree.getSelectionModel().setSelectionMode
 
161
                (TreeSelectionModel.SINGLE_TREE_SELECTION);
 
162
            jtree.setRootVisible(false);
 
163
 
 
164
            // Listen for when the selection changes, call nodeSelected(node)
 
165
            jtree.addTreeSelectionListener(
 
166
                new TreeSelectionListener() {
 
167
                    public void valueChanged(TreeSelectionEvent e) {
 
168
                        TreePath path = (TreePath)e.getPath(); 
 
169
                        TreeNode treeNode = (TreeNode)path.getLastPathComponent();
 
170
                        if(jtree.getSelectionModel().isPathSelected(path))
 
171
                                nodeSelected(treeNode);
 
172
                    }
 
173
                }
 
174
            );
 
175
            
 
176
            
 
177
            //expandTree();
 
178
            
 
179
                        
 
180
            // controls
 
181
            
 
182
            //iterate panel
 
183
            JPanel iteratePanel = new JPanel();
 
184
            iteratePanel.setBorder(BorderFactory.createCompoundBorder(
 
185
                BorderFactory.createTitledBorder("Iterate"),
 
186
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
187
                ));
 
188
            
 
189
            prevButton = new JButton("Previous");
 
190
            iteratePanel.add(prevButton);
 
191
            prevButton.addActionListener(this);
 
192
            
 
193
            nextButton = new JButton("Next");
 
194
            iteratePanel.add(nextButton);
 
195
            nextButton.addActionListener(this);
 
196
        
 
197
            //DOM tree panel
 
198
            JPanel domPanel = new JPanel();
 
199
            domPanel.setLayout(new BorderLayout());
 
200
            domPanel.setBorder(BorderFactory.createCompoundBorder(
 
201
                BorderFactory.createTitledBorder("Selected Node"),
 
202
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
203
                ));
 
204
            
 
205
            removeButton = new JButton("Remove Selected Node");
 
206
            domPanel.add(removeButton, BorderLayout.NORTH);
 
207
            removeButton.addActionListener(this);
 
208
            
 
209
            addButton = new JButton("Add Text Node");
 
210
            addText = new JTextField(10);
 
211
            domPanel.add(addButton, BorderLayout.CENTER);
 
212
            domPanel.add(addText, BorderLayout.SOUTH);
 
213
            addButton.addActionListener(this);
 
214
         
 
215
            // iterator settings
 
216
            JPanel settingsPanel = new JPanel();
 
217
            settingsPanel.setLayout(new BorderLayout());
 
218
            settingsPanel.setBorder(BorderFactory.createCompoundBorder(
 
219
                BorderFactory.createTitledBorder("Iterator Settings"),
 
220
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
221
                ));
 
222
            JPanel iteratorPanel = new JPanel();
 
223
            newIterator = new JButton("createNodeIterator");
 
224
            expandERs = new JCheckBox("expandEntityReferences");
 
225
            iteratorPanel.add(newIterator);
 
226
            expandERs.setSelected(true);
 
227
            iteratorPanel.add(expandERs);
 
228
            settingsPanel.add(iteratorPanel, BorderLayout.NORTH);
 
229
            newIterator.addActionListener(this);
 
230
    
 
231
            JPanel whatPanel = new JPanel();
 
232
            whatPanel.setBorder(BorderFactory.createCompoundBorder(
 
233
                BorderFactory.createTitledBorder("whatToShow"),
 
234
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
235
                ));
 
236
            whatToShow = new JList(DOMTreeFull.whatArray);
 
237
            JScrollPane whatScroll = 
 
238
            new JScrollPane(whatToShow) {
 
239
                    public Dimension getPreferredSize(){
 
240
                        return new Dimension(200, 75 );
 
241
                    }
 
242
                };
 
243
            
 
244
            whatPanel.add(whatScroll);
 
245
            
 
246
            
 
247
            JPanel filterPanel = new JPanel();
 
248
            filterPanel.setBorder(BorderFactory.createCompoundBorder(
 
249
                BorderFactory.createTitledBorder("NodeNameFilter"),
 
250
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
251
                ));
 
252
            filterPanel.setLayout(new BorderLayout());
 
253
            match = new JCheckBox("match/ignore node name", true);
 
254
            nameFilter = new JTextField(10);
 
255
            filterPanel.add(match, BorderLayout.NORTH);
 
256
            filterPanel.add(nameFilter, BorderLayout.SOUTH);
 
257
            
 
258
            settingsPanel.add(whatPanel, BorderLayout.WEST);
 
259
            settingsPanel.add(filterPanel, BorderLayout.EAST);
 
260
            
 
261
 
 
262
            // Listen for when the selection changes, call nodeSelected(node)
 
263
            whatToShow.addListSelectionListener(
 
264
                new ListSelectionListener() {
 
265
                    public void valueChanged(ListSelectionEvent e) {
 
266
                        // do nothing on selection...
 
267
                    }
 
268
                }
 
269
            );
 
270
            
 
271
            
 
272
            JPanel controlsPanel = new JPanel(new BorderLayout());
 
273
            JPanel buttonsPanel = new JPanel(new BorderLayout());
 
274
            buttonsPanel.add(iteratePanel, BorderLayout.NORTH);
 
275
            buttonsPanel.add(domPanel, BorderLayout.SOUTH);
 
276
            controlsPanel.add(buttonsPanel, BorderLayout.WEST);
 
277
            controlsPanel.add(settingsPanel, BorderLayout.CENTER);
 
278
            
 
279
            controlsPanel.setBorder(BorderFactory.createCompoundBorder(
 
280
                BorderFactory.createTitledBorder("Controls"),
 
281
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
282
                ));
 
283
                
 
284
                
 
285
            // tree panel    
 
286
            JPanel treePanel = new JPanel(new BorderLayout());
 
287
                
 
288
            JScrollPane treeScroll = new JScrollPane(jtree) ;
 
289
            treeScroll.setBorder(BorderFactory.createCompoundBorder(
 
290
                BorderFactory.createTitledBorder("Tree View"),
 
291
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
292
                ));
 
293
 
 
294
            // message text UI setup
 
295
            messageText = new JTextArea(3,5);
 
296
 
 
297
            JPanel messagePanel = new JPanel(new BorderLayout());
 
298
            messageScroll = new JScrollPane(messageText);
 
299
            messagePanel.add(messageScroll);
 
300
            messagePanel.setBorder(BorderFactory.createCompoundBorder(
 
301
                BorderFactory.createTitledBorder("Messages"),
 
302
                BorderFactory.createEmptyBorder(4, 4, 4, 4)
 
303
                ));
 
304
            
 
305
            JPanel mainPanel = new JPanel();
 
306
            mainPanel.setLayout(new BorderLayout());
 
307
            mainPanel.add(controlsPanel, BorderLayout.NORTH);
 
308
            mainPanel.add(treeScroll, BorderLayout.CENTER);
 
309
            mainPanel.add(messagePanel, BorderLayout.SOUTH);
 
310
            getContentPane().add(mainPanel);
 
311
            
 
312
            Hashtable errorNodes = errors.getErrorNodes();
 
313
            Enumeration elements = errorNodes.elements();
 
314
            while (elements.hasMoreElements()) {
 
315
                //*** append errors to messageText
 
316
                messageText.append( (String)elements.nextElement());
 
317
            }    
 
318
            
 
319
            // This cast must work, because we have tested above
 
320
            // with document.isSupported("Traversal")
 
321
            iterator = ((DocumentTraversal)document).
 
322
                createNodeIterator(
 
323
                    document, 
 
324
                    NodeFilter.SHOW_ALL, 
 
325
                    new NameNodeFilter(),
 
326
                    true);
 
327
            
 
328
        } catch (Exception e) {
 
329
            e.printStackTrace(System.err);
 
330
        }
 
331
    }
 
332
    
 
333
    public void actionPerformed(ActionEvent e) {
 
334
        
 
335
        if (e.getSource() == newIterator) {
 
336
            Node node = document;
 
337
            
 
338
            // whatToShow section
 
339
            int  [] indices = whatToShow.getSelectedIndices();
 
340
            int mask = 0x0;
 
341
            for (int i = 0; i < indices.length; i++) {
 
342
                if (indices[i] == 0) {
 
343
                    mask = 0xFFFF;
 
344
                    break;
 
345
                } else {
 
346
                    mask = mask | 1<<indices[i]-1;
 
347
                }
 
348
            }
 
349
            // filter section
 
350
            String nameText = nameFilter.getText();
 
351
            boolean matched = match.isSelected();
 
352
            if (nameNodeFilter==null) {
 
353
                nameNodeFilter = new NameNodeFilter();
 
354
            }
 
355
            if (nameText.equals("")) {
 
356
                setMessage("NodeNameFilter name is \"\". Assuming null.");
 
357
                nameText = null;
 
358
            }
 
359
            nameNodeFilter.setName(nameText);
 
360
            nameNodeFilter.setMatch(matched);
 
361
            
 
362
            if (iterator !=null) iterator.detach();
 
363
            boolean expand = expandERs.isSelected();
 
364
            iterator = ((DocumentTraversal)document).
 
365
                createNodeIterator(
 
366
                    node, 
 
367
                    (short)mask, 
 
368
                    nameNodeFilter,
 
369
                    expand);
 
370
            setMessage("doc.createNodeIterator("+ 
 
371
                        " root="+node+
 
372
                        ", whatToShow="+mask+
 
373
                        ", match="+matched+
 
374
                        ", name="+nameText+")"
 
375
                        );
 
376
            return;
 
377
            
 
378
        } 
 
379
        
 
380
        if (e.getSource() == addButton) {
 
381
            
 
382
            String text = addText.getText();
 
383
            
 
384
            if (text==null) return;
 
385
            
 
386
            TreeNode treeNode = (TreeNode)jtree.getLastSelectedPathComponent();
 
387
            if (treeNode == null) {
 
388
                messageText.append("Must select a tree component to add a child to it.");
 
389
                return;
 
390
            }
 
391
            TreePath path = new TreePath(
 
392
                    ((DefaultTreeModel)jtree.getModel()).getPathToRoot(treeNode));
 
393
            if (path == null) {
 
394
                setMessage("Could not create a path.");
 
395
                return;
 
396
            }
 
397
            if(!jtree.getSelectionModel().isPathSelected(path))
 
398
                return;
 
399
            Node node = jtree.getNode(treeNode);
 
400
            Node textNode = document.createTextNode(text);
 
401
            try {
 
402
                node.appendChild(textNode);
 
403
            } catch (DOMException dome) {
 
404
                setMessage("DOMException:"+dome.code+", "+dome);
 
405
                return;
 
406
            }
 
407
            ((DOMTreeFull.Model)jtree.getModel()).insertNode(textNode, (MutableTreeNode)treeNode);
 
408
            
 
409
            return;
 
410
        }
 
411
        if (e.getSource() == nextButton) {          
 
412
            Node node = iterator.nextNode();
 
413
            if (node==null) {
 
414
                setMessage("iterator.nextNode() == null");
 
415
                return;
 
416
            } 
 
417
            
 
418
            setMessage("iterator.nextNode() == "+node);
 
419
 
 
420
            TreeNode treeNode = jtree.getTreeNode(node);
 
421
            if (treeNode == null) {
 
422
                setMessage("No JTree TreeNode for Node name:" + node.getNodeName());
 
423
                return;
 
424
            }
 
425
            
 
426
            TreePath path = new TreePath(
 
427
                    ((DefaultTreeModel)jtree.getModel()).getPathToRoot(treeNode));
 
428
            jtree.requestFocus();
 
429
            jtree.setSelectionPath(path);
 
430
            jtree.scrollPathToVisible(path);
 
431
            return;
 
432
        } 
 
433
        
 
434
        if (e.getSource() == prevButton) {          
 
435
            Node node = iterator.previousNode();
 
436
            if (node==null) {
 
437
                setMessage("iterator.previousNode() == null");
 
438
                return;
 
439
            } 
 
440
            
 
441
            setMessage("iterator.previousNode() == "+node);
 
442
 
 
443
            TreeNode treeNode = jtree.getTreeNode(node);
 
444
            if (treeNode == null) {
 
445
                setMessage("No JTree TreeNode for Node name:" + node.getNodeName());
 
446
                return;
 
447
            }
 
448
            
 
449
            TreePath path = new TreePath(
 
450
                    ((DefaultTreeModel)jtree.getModel()).getPathToRoot(treeNode));
 
451
            jtree.requestFocus();
 
452
            jtree.setSelectionPath(path);
 
453
            jtree.scrollPathToVisible(path);
 
454
            return;
 
455
        }
 
456
        if (e.getSource() == removeButton) {
 
457
            
 
458
            /** If the node is not selected don't remove. */
 
459
            TreeNode treeNode = (TreeNode)jtree.getLastSelectedPathComponent();
 
460
            if (treeNode == null) {
 
461
                messageText.append("Must select a tree component to remove it.");
 
462
                return;
 
463
            }
 
464
            TreePath path = new TreePath(
 
465
                    ((DefaultTreeModel)jtree.getModel()).getPathToRoot(treeNode));
 
466
            if (path == null) {
 
467
                setMessage("Could not create a path.");
 
468
                return;
 
469
            }
 
470
            if(!jtree.getSelectionModel().isPathSelected(path))
 
471
                return;
 
472
            Node node = jtree.getNode(treeNode);
 
473
            if (node == null) return;
 
474
            Node parent = node.getParentNode();
 
475
            if (parent == null) return;
 
476
            
 
477
            parent.removeChild(node);
 
478
    
 
479
            ((DefaultTreeModel)jtree.getModel()).removeNodeFromParent((MutableTreeNode)treeNode);
 
480
            return;
 
481
        } 
 
482
        
 
483
    }
 
484
    
 
485
    /** Helper function to set messages */
 
486
    void setMessage(String string) {
 
487
        messageText.selectAll();
 
488
        messageText.cut();
 
489
        messageText.append(string);
 
490
        messageText.setCaretPosition(0);
 
491
    }
 
492
        
 
493
    /** called when our JTree's nodes are selected.
 
494
     */
 
495
    void nodeSelected(TreeNode treeNode) {
 
496
 
 
497
        lastSelected = treeNode;
 
498
        Node node = jtree.getNode(treeNode);
 
499
        
 
500
        System.out.println("nodeSelected.node="+node);
 
501
        if (node == null) return;
 
502
                            
 
503
        setMessage(DOMTreeFull.toString(node));
 
504
    }
 
505
    
 
506
    /** Utility function to expand the jtree */
 
507
    void expandTree() {
 
508
        for (int i = 0; i < jtree.getRowCount(); i++) {
 
509
            jtree.expandRow(i);
 
510
        }
 
511
    }
 
512
    
 
513
    class Errors implements ErrorHandler {
 
514
 
 
515
        Hashtable errorNodes = new Hashtable();
 
516
 
 
517
        public void warning(SAXParseException ex) {
 
518
            store(ex, "[Warning]");
 
519
        }
 
520
 
 
521
        public void error(SAXParseException ex) {
 
522
            store(ex, "[Error]");
 
523
        }
 
524
 
 
525
        public void fatalError(SAXParseException ex) throws SAXException {
 
526
            store(ex, "[Fatal Error]");
 
527
        }
 
528
 
 
529
        public Hashtable getErrorNodes() {
 
530
            return errorNodes;
 
531
        }
 
532
 
 
533
        public Object getError(Node node) {
 
534
            return errorNodes.get(node);
 
535
        }
 
536
 
 
537
        public void clearErrors() {
 
538
            errorNodes.clear();
 
539
        }
 
540
 
 
541
        void store(SAXParseException ex, String type) {
 
542
 
 
543
            // build error text
 
544
            String errorString= type+" at line number, "+ex.getLineNumber()
 
545
                +": "+ex.getMessage()+"\n";
 
546
            Node currentNode = null;
 
547
            try {
 
548
                currentNode = (Node)parser.getProperty("http://apache.org/xml/properties/dom-node");
 
549
            } catch (SAXException se) {
 
550
                System.err.println(se.getMessage());
 
551
                return;
 
552
            }
 
553
            if (currentNode == null) return;
 
554
 
 
555
            // accumulate any multiple errors per node in the Hashtable.
 
556
            String previous = (String) errorNodes.get(currentNode);
 
557
            if (previous != null)
 
558
                errorNodes.put(currentNode, previous +errorString);
 
559
            else
 
560
                errorNodes.put(currentNode, errorString);
 
561
        }
 
562
    }
 
563
    
 
564
}