KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JDOMAbout


1 /*--
2
3  Copyright (C) 2001-2004 Jason Hunter & Brett McLaughlin.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JDOM" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact license@jdom.org.
21  
22  4. Products derived from this software may not be called "JDOM", nor
23     may "JDOM" appear in their name, without prior written permission
24     from the JDOM Project Management (pm@jdom.org).
25  
26  In addition, we request (but do not require) that you include in the
27  end-user documentation provided with the redistribution and/or in the
28  software itself an acknowledgement equivalent to the following:
29      "This product includes software developed by the
30       JDOM Project (http://www.jdom.org/)."
31  Alternatively, the acknowledgment may be graphical using the logos
32  available at http://www.jdom.org/images/logos.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
38  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  SUCH DAMAGE.
46
47  This software consists of voluntary contributions made by many
48  individuals on behalf of the JDOM Project and was originally
49  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
50  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
51  on the JDOM Project, please see <http://www.jdom.org/>.
52
53  */

54
55 // Explicitly left in the default package
56

57 import java.io.*;
58 import java.util.*;
59 import java.util.jar.*;
60 import java.util.zip.*;
61
62 import org.jdom.*;
63 import org.jdom.input.*;
64
65 /**
66  * This class is not part of the core JDOM API, but implements the main
67  * method executed when the JDOM JAR is invoked directly. For example,
68  * to "run" the JDOM JAR directly use a command line such as the following:
69  *
70  * <pre>
71  * java -jar jdom.jar
72  * </pre>
73  *
74  * When run like this, the output will display product and version
75  * information, a brief description of JDOM and the authors, all as
76  * specified in the file META-INF/info.xml contained within the JAR.
77  * If this file does not exist in the JAR, then the JAR has been
78  * incorrectly built. Any exceptions will pop the call stack and end the
79  * program with a message describing the error.
80  *
81  * @see http://java.sun.com/docs/books/tutorial/jar/
82  *
83  * @author Steven Gould <steven.gouldATcgiusa.com>
84  */

85 public class JDOMAbout {
86     /**
87      * The main(!) method executed when this JAR file is run. Outputs
88      * information about the JAR, as extracted from the META-INF/info.xml
89      * file.
90      * @param args Ignored.
91      */

92     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
93         JDOMAbout.Info info = new JDOMAbout().new Info();
94
95         // Shortcut for info.title (because it's used so much)
96
String JavaDoc title = info.title;
97
98         // Output product information
99
System.out.println(title + " version " + info.version);
100         System.out.println("Copyright " + info.copyright);
101         System.out.println();
102         
103         // Display product/JAR "description"
104
System.out.println(info.description);
105         System.out.println();
106         
107         // Iterate through authors, outputting each in turn
108
System.out.println("Authors:");
109         Iterator it = info.authors.iterator();
110         while (it.hasNext()) {
111             Author author = (Author)it.next();
112         
113             // Output the author's name
114
System.out.print(" " + author.name);
115                 
116             // If the author has an e-mail address, output it too
117
if (author.email == null) {
118                 System.out.println();
119             } else {
120                 System.out.println(" <" + author.email + ">");
121             }
122         }
123         System.out.println();
124     
125         // Display "license" information
126
System.out.println(title + " license:");
127         System.out.println(info.license);
128         System.out.println();
129     
130         // Display "support" information
131
System.out.println(title + " support:");
132         System.out.println(info.support);
133         System.out.println();
134     
135         // Display "license" information
136
System.out.println(title + " web site: " + info.website);
137         System.out.println();
138     }
139
140     /**
141      * This class encapsulates the locating of, and reading of, the JAR
142      * information file. This file should be named META-INF/info.xml. The
143      * default constructor initializes all members variables from the
144      * content of the info.xml file.
145      *
146      * <p>This class has been separated out for a couple of reasons:
147      * <ol>
148      * <li>So that the presentation of the data in info.xml can be
149      * separated out from the reading of the file - enabling a "looser"
150      * coupling between the data and presentation.</li>
151      * <li>Just in case we later decide to make this information
152      * publically accessible. If this happens we'll probably want to
153      * implement "get" methods for each of the member variables. For
154      * now, it was decided that doing so may be too much of an overhead
155      * for an internal class.</li>
156      * </ol>
157      */

158     private class Info {
159         /** The product title, or product name. */
160         String JavaDoc title;
161
162         /**
163          * The product/JAR version number - or string. This can contain
164          * characters.
165          */

166         String JavaDoc version;
167
168         /** The JAR copyright message. */
169         String JavaDoc copyright;
170
171         /** A description of the contents of this JAR. */
172         String JavaDoc description;
173
174         /** A list of authors of this product/JAR. */
175         List authors;
176
177         /** The product/JAR license information. */
178         String JavaDoc license;
179
180         /** Product/JAR support information. */
181         String JavaDoc support;
182
183         /** The main web site for this product/JAR. */
184         String JavaDoc website;
185     
186         /**
187          * Constructor for the Info class - initializes all member
188          * variables with values taken from the JAR information file.
189          * The JAR information file should be called META-INF/info.xml
190          * and be contained within the JAR file.
191          * Any exceptions are passed to the caller.
192          */

193         Info() throws Exception JavaDoc {
194             final String JavaDoc INFO_FILENAME = "META-INF/info.xml";
195
196             SAXBuilder builder = new SAXBuilder();
197         
198             // Find the INFO_FILENAME file in any JAR in the classpath
199
// Use a brute-force search
200
JarFile jarFile = null;
201             ZipEntry zipEntry = null;
202
203             String JavaDoc classpath = System.getProperty("java.class.path");
204             StringTokenizer tokenizer = new StringTokenizer(classpath, ";:");
205             while (tokenizer.hasMoreTokens() && zipEntry == null) {
206               String JavaDoc token = tokenizer.nextToken();
207
208               try {
209                 // Try to create a JarFile representation of the JAR
210
jarFile = new JarFile(token);
211             
212                 // Get a reference to the info.xml entry in the JAR
213
zipEntry = jarFile.getEntry(INFO_FILENAME);
214               }
215               catch (Exception JavaDoc e) {
216               }
217             }
218             
219             if (zipEntry == null) {
220               throw new FileNotFoundException(INFO_FILENAME +
221                 " not found; it should be within the JDOM JAR but isn't");
222             }
223
224             // Get input stream for reading info.xml file
225
InputStream in = jarFile.getInputStream(zipEntry);
226             
227             // Using JDOM, read the info.xml file
228
Document doc = builder.build(in);
229             Element root = doc.getRootElement();
230
231             title = root.getChildTextTrim("title");
232             version = root.getChildTextTrim("version");
233             copyright = root.getChildTextTrim("copyright");
234             description = root.getChildTextTrim("description");
235             license = root.getChildTextTrim("license");
236             support = root.getChildTextTrim("support");
237             website = root.getChildTextTrim("web-site");
238
239             List authorElements = root.getChildren("author");
240             authors = new LinkedList();
241             Iterator it = authorElements.iterator();
242             while (it.hasNext()) {
243                 Element element = (Element)it.next();
244                     
245                 Author author = new Author();
246                 author.name = element.getChildTextTrim("name");
247                 author.email = element.getChildTextTrim("e-mail");
248
249                 authors.add(author);
250             }
251         }
252     }
253
254     /**
255      * A helper class that stores information about a single Author. By
256      * using this class, the Info class can store Author information
257      * without any dependence on JDOM and XML elements.
258      */

259     private class Author {
260         /** The name of this author. */
261         String JavaDoc name;
262
263         /** The e-mail address of this author. */
264         String JavaDoc email;
265     }
266 }
267
Popular Tags