KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > applets > SimpleAppletDemo


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: SimpleAppletDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

9
10 package org.dom4j.samples.applets;
11
12 import java.applet.Applet JavaDoc;
13 import java.awt.Graphics JavaDoc;
14
15 import org.dom4j.Document;
16 import org.dom4j.DocumentException;
17 import org.dom4j.DocumentHelper;
18 import org.dom4j.io.OutputFormat;
19 import org.dom4j.io.XMLWriter;
20
21 /**
22  * This class demonstrate the use of dom4j in Applets. Note that applets are not
23  * allowed to read files from client disk, if unsigned.
24  *
25  * @author <a HREF="toby-wan-kenobi@gmx.de">Tobias Rademacher </a>
26  * @version $Revision: 1.4 $
27  */

28 public class SimpleAppletDemo extends Applet JavaDoc {
29
30     private static String JavaDoc DEMO_XML = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"
31             + "<web-app>\n"
32             + "<servlet>\n"
33             + "<servlet-name>snoop</servlet-name>\n"
34             + "<servlet-class>SnoopServlet</servlet-class>\n"
35             + "</servlet>\n"
36             + "</web-app>";
37
38     private Document demoDocument;
39
40     private StringBuffer JavaDoc buffer;
41
42     /**
43      * Called after init. Demonstrates the simplicity of parsing in applets.
44      */

45     public void start() {
46         try {
47             demoDocument = DocumentHelper.parseText(DEMO_XML);
48             new XMLWriter(OutputFormat.createPrettyPrint()).write(demoDocument);
49         } catch (DocumentException documentEx) {
50             documentEx.printStackTrace();
51         } catch (Exception JavaDoc ex) {
52             ex.printStackTrace();
53         }
54
55         demoXPath();
56         repaint();
57     }
58
59     /**
60      * Demonstrates the use of XPath in Applets
61      */

62     private void demoXPath() {
63         this.buffer = new StringBuffer JavaDoc("The name of the servlet is :");
64         this.buffer.append(demoDocument
65                 .valueOf("/web-app/servlet[1]/servlet-name"));
66         this.buffer.append(" and the class is ");
67         this.buffer.append(demoDocument
68                 .valueOf("/web-app/servlet[1]/servlet-class"));
69     }
70
71     /**
72      * Invoked by repaint() and paints a xpath
73      */

74     public void paint(Graphics JavaDoc g) {
75         g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
76         g.drawString(this.buffer.toString(), 5, 15);
77     }
78
79 }
80
81 /*
82  * Redistribution and use of this software and associated documentation
83  * ("Software"), with or without modification, are permitted provided that the
84  * following conditions are met:
85  *
86  * 1. Redistributions of source code must retain copyright statements and
87  * notices. Redistributions must also contain a copy of this document.
88  *
89  * 2. Redistributions in binary form must reproduce the above copyright notice,
90  * this list of conditions and the following disclaimer in the documentation
91  * and/or other materials provided with the distribution.
92  *
93  * 3. The name "DOM4J" must not be used to endorse or promote products derived
94  * from this Software without prior written permission of MetaStuff, Ltd. For
95  * written permission, please contact dom4j-info@metastuff.com.
96  *
97  * 4. Products derived from this Software may not be called "DOM4J" nor may
98  * "DOM4J" appear in their names without prior written permission of MetaStuff,
99  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
100  *
101  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
102  *
103  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
104  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
105  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
106  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
107  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
108  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
109  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
110  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
111  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
112  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
113  * POSSIBILITY OF SUCH DAMAGE.
114  *
115  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
116  *
117  * $Id: SimpleAppletDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
118  */

119
120
Popular Tags