KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > bean > BeanDemo


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: BeanDemo.java,v 1.4 2005/01/29 14:52:58 maartenc Exp $
8  */

9
10 package org.dom4j.samples.bean;
11
12 import org.dom4j.samples.SAXDemo;
13
14 import java.awt.Component JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.dom4j.Attribute;
19 import org.dom4j.Document;
20 import org.dom4j.Element;
21 import org.dom4j.bean.BeanDocumentFactory;
22 import org.dom4j.io.SAXReader;
23
24 /**
25  * A simple test program to demonstrate using simple binding of JavaBeans to
26  * inside a DOM4J tree
27  *
28  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
29  * @version $Revision: 1.4 $
30  */

31 public class BeanDemo extends SAXDemo {
32
33     public static void main(String JavaDoc[] args) {
34         run(new BeanDemo(), args);
35     }
36
37     public BeanDemo() {
38     }
39
40     public void run(String JavaDoc[] args) throws Exception JavaDoc {
41         if (args.length < 1) {
42             printUsage("<XML document URL>");
43             return;
44         }
45
46         Document document = parse(args[0]);
47         process(document);
48     }
49
50     protected Document parse(String JavaDoc url) throws Exception JavaDoc {
51         SAXReader reader = new SAXReader(BeanDocumentFactory.getInstance());
52         return reader.read(url);
53     }
54
55     protected void process(Document document) throws Exception JavaDoc {
56         // find all of the windows
57
List JavaDoc windows = document.selectNodes("//window");
58         for (Iterator JavaDoc iter = windows.iterator(); iter.hasNext();) {
59             Element element = (Element) iter.next();
60             Object JavaDoc window = element.getData();
61             if (window instanceof Component JavaDoc) {
62                 Component JavaDoc component = (Component JavaDoc) window;
63                 component.setVisible(true);
64             }
65
66             println("found element: " + element);
67             println("found window: " + window);
68         }
69
70         println("");
71         println("Now lets find all the fonts...");
72
73         List JavaDoc fonts = document.selectNodes("//@font");
74         for (Iterator JavaDoc iter = fonts.iterator(); iter.hasNext();) {
75             Attribute font = (Attribute) iter.next();
76             println("found font: " + font.getData());
77         }
78
79     }
80
81 }
82
83 /*
84  * Redistribution and use of this software and associated documentation
85  * ("Software"), with or without modification, are permitted provided that the
86  * following conditions are met:
87  *
88  * 1. Redistributions of source code must retain copyright statements and
89  * notices. Redistributions must also contain a copy of this document.
90  *
91  * 2. Redistributions in binary form must reproduce the above copyright notice,
92  * this list of conditions and the following disclaimer in the documentation
93  * and/or other materials provided with the distribution.
94  *
95  * 3. The name "DOM4J" must not be used to endorse or promote products derived
96  * from this Software without prior written permission of MetaStuff, Ltd. For
97  * written permission, please contact dom4j-info@metastuff.com.
98  *
99  * 4. Products derived from this Software may not be called "DOM4J" nor may
100  * "DOM4J" appear in their names without prior written permission of MetaStuff,
101  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
102  *
103  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
104  *
105  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
106  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
107  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
108  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
109  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
110  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
111  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
112  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
113  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
114  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
115  * POSSIBILITY OF SUCH DAMAGE.
116  *
117  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
118  *
119  * $Id: BeanDemo.java,v 1.4 2005/01/29 14:52:58 maartenc Exp $
120  */

121
Popular Tags