KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > TreeViewer


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import javax.swing.JFrame JavaDoc;
25 import javax.swing.JScrollPane JavaDoc;
26 import javax.swing.JTree JavaDoc;
27 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
28 import javax.swing.tree.MutableTreeNode JavaDoc;
29
30 import nu.xom.Builder;
31 import nu.xom.Document;
32 import nu.xom.Element;
33 import nu.xom.Elements;
34
35 /**
36  *
37  * <p>
38  * Demonstrates using Swing to present a graphical display of
39  * the tree structure of an XML document.
40  * </p>
41  *
42  * @author Elliotte Rusty Harold
43  * @version 1.0
44  *
45  */

46 public class TreeViewer {
47
48     // Initialize the per-element data structures
49
public static MutableTreeNode JavaDoc processElement(Element element) {
50
51         String JavaDoc data;
52         if (element.getNamespaceURI().equals(""))
53             data = element.getLocalName();
54         else {
55             data =
56                 '{'
57                     + element.getNamespaceURI()
58                     + "} "
59                     + element.getQualifiedName();
60         }
61
62         MutableTreeNode JavaDoc node = new DefaultMutableTreeNode JavaDoc(data);
63         Elements children = element.getChildElements();
64         for (int i = 0; i < children.size(); i++) {
65             node.insert(processElement(children.get(i)), i);
66         }
67
68         return node;
69
70     }
71
72     public static void display(Document doc) {
73
74         Element root = doc.getRootElement();
75         JTree JavaDoc tree = new JTree JavaDoc(processElement(root));
76         JScrollPane JavaDoc treeView = new JScrollPane JavaDoc(tree);
77         JFrame JavaDoc f = new JFrame JavaDoc("XML Tree");
78
79
80         String JavaDoc version = System.getProperty("java.version");
81         if (version.startsWith("1.2") || version.startsWith("1.1")) {
82             f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
83         }
84         else {
85             // JFrame.EXIT_ON_CLOSE == 3 but this named constant is not
86
// available in Java 1.2
87
f.setDefaultCloseOperation(3);
88         }
89         f.getContentPane().add(treeView);
90         f.pack();
91         f.show();
92
93     }
94
95     public static void main(String JavaDoc[] args) {
96
97         try {
98             Builder builder = new Builder();
99             for (int i = 0; i < args.length; i++) {
100                 Document doc = builder.build(args[i]);
101                 display(doc);
102             }
103         }
104         catch (Exception JavaDoc ex) {
105             System.err.println(ex);
106         }
107
108     } // end main()
109

110 } // end TreeViewer
Popular Tags