KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > AbstractDemo


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

9
10 package org.dom4j.samples;
11
12 import org.dom4j.Document;
13 import org.dom4j.DocumentException;
14 import org.dom4j.io.OutputFormat;
15 import org.dom4j.io.XMLWriter;
16
17 /**
18  * An abstract base class for the demo programs.
19  *
20  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
21  * @version $Revision: 1.4 $
22  */

23 public abstract class AbstractDemo {
24
25     /** The format of XML / HTML that is output by the demo program */
26     protected OutputFormat format = new OutputFormat();
27
28     /** The writer of XML */
29     protected XMLWriter writer;
30
31     public AbstractDemo() {
32     }
33
34     protected static void run(AbstractDemo demo, String JavaDoc[] args) {
35         try {
36             demo.run(args);
37         } catch (DocumentException e) {
38             System.out.println("Exception occurred: " + e);
39             Throwable JavaDoc nestedException = e.getNestedException();
40             if (nestedException != null) {
41                 System.out.println("NestedException: " + nestedException);
42                 nestedException.printStackTrace();
43             } else {
44                 e.printStackTrace();
45             }
46         } catch (Throwable JavaDoc t) {
47             System.out.println("Exception occurred: " + t);
48             t.printStackTrace();
49         }
50     }
51
52     public void run(String JavaDoc[] args) throws Exception JavaDoc {
53         if (args.length < 1) {
54             printUsage("no XML document URL specified");
55             return;
56         }
57
58         int idx = format.parseOptions(args, 0);
59         if (idx >= args.length) {
60             printUsage("no XML document URL specified");
61             return;
62         } else {
63             writer = createXMLWriter();
64
65             Document document = parse(args[idx]);
66             process(document);
67         }
68     }
69
70     protected Document parse(String JavaDoc xmlFile) throws Exception JavaDoc {
71         throw new RuntimeException JavaDoc(
72                 "parse(String xmlFile) not implemented in this demo");
73     }
74
75     protected void process(Document document) throws Exception JavaDoc {
76         getXMLWriter().write(document);
77         getXMLWriter().flush();
78     }
79
80     protected void print(String JavaDoc text) {
81         System.out.print(text);
82     }
83
84     protected void println(String JavaDoc text) {
85         System.out.println(text);
86     }
87
88     protected void printUsage(String JavaDoc text) {
89         println("Usage: java " + getClass().getName() + " " + text);
90     }
91
92     protected XMLWriter getXMLWriter() throws Exception JavaDoc {
93         if (writer == null) {
94             writer = createXMLWriter();
95         }
96         return writer;
97     }
98
99     /**
100      * A Factory Method to create an <code>XMLWriter</code> instance allowing
101      * derived classes to change this behaviour
102      */

103     protected XMLWriter createXMLWriter() throws Exception JavaDoc {
104         return new XMLWriter(System.out, format);
105     }
106
107 }
108
109 /*
110  * Redistribution and use of this software and associated documentation
111  * ("Software"), with or without modification, are permitted provided that the
112  * following conditions are met:
113  *
114  * 1. Redistributions of source code must retain copyright statements and
115  * notices. Redistributions must also contain a copy of this document.
116  *
117  * 2. Redistributions in binary form must reproduce the above copyright notice,
118  * this list of conditions and the following disclaimer in the documentation
119  * and/or other materials provided with the distribution.
120  *
121  * 3. The name "DOM4J" must not be used to endorse or promote products derived
122  * from this Software without prior written permission of MetaStuff, Ltd. For
123  * written permission, please contact dom4j-info@metastuff.com.
124  *
125  * 4. Products derived from this Software may not be called "DOM4J" nor may
126  * "DOM4J" appear in their names without prior written permission of MetaStuff,
127  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
128  *
129  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
130  *
131  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
132  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
133  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
134  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
135  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
136  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
137  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
138  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
139  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
140  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
141  * POSSIBILITY OF SUCH DAMAGE.
142  *
143  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
144  *
145  * $Id: AbstractDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
146  */

147
Popular Tags