KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > xml > generator > DescriptionGenerator


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -------------------------
28  * DescriptionGenerator.java
29  * -------------------------
30  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: DescriptionGenerator.java,v 1.4 2005/10/18 13:32:20 mungady Exp $
36  *
37  * Changes
38  * -------------------------
39  * 21.06.2003 : Initial version
40  *
41  */

42
43 package org.jfree.xml.generator;
44
45 import java.io.BufferedInputStream JavaDoc;
46 import java.io.BufferedWriter JavaDoc;
47 import java.io.File JavaDoc;
48 import java.io.FileWriter JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.InputStream JavaDoc;
51 import java.io.Writer JavaDoc;
52 import java.net.URL JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import org.jfree.util.Log;
57 import org.jfree.util.PrintStreamLogTarget;
58 import org.jfree.util.ObjectUtilities;
59 import org.jfree.xml.generator.model.DescriptionModel;
60
61 /**
62  * A utility application for generating class descriptions.
63  */

64 public final class DescriptionGenerator {
65
66     /**
67      * Loads a property set from the specified URL.
68      *
69      * @param propertyURL the URL.
70      *
71      * @return The properties.
72      */

73     private static Properties JavaDoc loadProperties(final URL JavaDoc propertyURL) {
74         final Properties JavaDoc p = new Properties JavaDoc();
75         try {
76             final InputStream JavaDoc in = new BufferedInputStream JavaDoc(propertyURL.openStream());
77             p.load(in);
78             in.close();
79         }
80         catch (Exception JavaDoc e) {
81             System.err.println("Unable to load properties from " + propertyURL);
82         }
83         return p;
84     }
85
86     /**
87      * Runs the generator, using the 'generator.properties' file for configuration info.
88      *
89      * @param args command line arguments.
90      *
91      * @throws Exception if something goes wrong!
92      */

93     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
94
95         Log.getInstance().addTarget(new PrintStreamLogTarget());
96         
97         URL JavaDoc propertyURL = ObjectUtilities.getResourceRelative
98                 ("generator.properties", DescriptionGenerator.class);
99         if (args.length > 0) {
100             final File JavaDoc f = new File JavaDoc(args[0]);
101             propertyURL = f.toURL();
102         }
103         final Properties JavaDoc p = loadProperties(propertyURL);
104
105         final String JavaDoc handlerSource = p.getProperty("attributedefinition");
106         if (handlerSource != null) {
107             final Properties JavaDoc handlers = loadProperties(new URL JavaDoc(propertyURL, handlerSource));
108             ModelBuilder.getInstance().addAttributeHandlers(handlers);
109         }
110
111         final String JavaDoc source = p.getProperty("sourcedirectory", ".");
112         final String JavaDoc target = p.getProperty("targetfile", "model.xml");
113         DescriptionModel model = null;
114         try {
115             model = new DefaultModelReader().load(target);
116         }
117         catch (Exception JavaDoc e) {
118             Log.debug("Unable to load default model. Ignoring...");
119         }
120 // Log.debug (model.getModelComments());
121
model = generate(source, p, model);
122         model.prune();
123         writeMultiFile(target, model);
124         System.exit(0);
125     }
126
127     /**
128      * Generates a {@link DescriptionModel} from the specified source.
129      *
130      * @param source the source directory.
131      * @param configuration the configuration properties.
132      * @param model the model (<code>null</code> permitted).
133      *
134      * @return A class description model.
135      */

136     public static DescriptionModel generate(final String JavaDoc source,
137                                             final Properties JavaDoc configuration,
138                                             final DescriptionModel model) {
139         
140         final JavaSourceCollector jsc = new JavaSourceCollector(new File JavaDoc(source));
141
142         final Iterator JavaDoc it = configuration.keySet().iterator();
143         while (it.hasNext()) {
144             final String JavaDoc pName = (String JavaDoc) it.next();
145             if (pName.startsWith("ignore.baseclass.")) {
146                 jsc.addIgnoredBaseClass(configuration.getProperty(pName));
147             }
148             else if (pName.startsWith("ignore.package.")) {
149                 jsc.addIgnoredPackage(configuration.getProperty(pName));
150             }
151         }
152
153         jsc.collectFiles();
154         return ModelBuilder.getInstance().buildModel(jsc, model);
155     }
156
157     /**
158      * Writes the class description model to a single file.
159      *
160      * @param target the target file name.
161      * @param model the class description model.
162      *
163      * @throws IOException if there is an I/O problem.
164      */

165     public static void writeSingleFile(final String JavaDoc target, final DescriptionModel model) throws IOException JavaDoc {
166         Log.debug ("Writing ...");
167         final ModelWriter writer = new ModelWriter();
168         writer.setModel(model);
169         final Writer JavaDoc w = new BufferedWriter JavaDoc(new FileWriter JavaDoc(target));
170         writer.write(w);
171         w.close();
172     }
173
174     /**
175      * Writes the class description model to multiple files.
176      *
177      * @param target the target file name.
178      * @param model the class description model.
179      *
180      * @throws IOException if there is an I/O problem.
181      */

182     public static void writeMultiFile(final String JavaDoc target, final DescriptionModel model) throws IOException JavaDoc {
183         Log.debug ("Writing multiple files ...");
184         final SplittingModelWriter writer = new SplittingModelWriter();
185         writer.setModel(model);
186         writer.write(target);
187     }
188     
189 }
190
Popular Tags