~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/samples/TreeViewer.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
25
import javax.swing.JScrollPane;
 
26
import javax.swing.JTree;
 
27
import javax.swing.tree.DefaultMutableTreeNode;
 
28
import javax.swing.tree.MutableTreeNode;
 
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 processElement(Element element) {
 
50
 
 
51
        String 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 node = new DefaultMutableTreeNode(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 tree = new JTree(processElement(root));
 
76
        JScrollPane treeView = new JScrollPane(tree);
 
77
        JFrame f = new JFrame("XML Tree");
 
78
 
 
79
 
 
80
        String 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[] 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 ex) {
 
105
            System.err.println(ex);
 
106
        }
 
107
 
 
108
    } // end main()
 
109
 
 
110
} // end TreeViewer
 
 
b'\\ No newline at end of file'