Frames | No Frames |
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; 46: import java.io.BufferedWriter; 47: import java.io.File; 48: import java.io.FileWriter; 49: import java.io.IOException; 50: import java.io.InputStream; 51: import java.io.Writer; 52: import java.net.URL; 53: import java.util.Iterator; 54: import java.util.Properties; 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 loadProperties(final URL propertyURL) { 74: final Properties p = new Properties(); 75: try { 76: final InputStream in = new BufferedInputStream(propertyURL.openStream()); 77: p.load(in); 78: in.close(); 79: } 80: catch (Exception 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[] args) throws Exception { 94: 95: Log.getInstance().addTarget(new PrintStreamLogTarget()); 96: 97: URL propertyURL = ObjectUtilities.getResourceRelative 98: ("generator.properties", DescriptionGenerator.class); 99: if (args.length > 0) { 100: final File f = new File(args[0]); 101: propertyURL = f.toURL(); 102: } 103: final Properties p = loadProperties(propertyURL); 104: 105: final String handlerSource = p.getProperty("attributedefinition"); 106: if (handlerSource != null) { 107: final Properties handlers = loadProperties(new URL(propertyURL, handlerSource)); 108: ModelBuilder.getInstance().addAttributeHandlers(handlers); 109: } 110: 111: final String source = p.getProperty("sourcedirectory", "."); 112: final String target = p.getProperty("targetfile", "model.xml"); 113: DescriptionModel model = null; 114: try { 115: model = new DefaultModelReader().load(target); 116: } 117: catch (Exception 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 source, 137: final Properties configuration, 138: final DescriptionModel model) { 139: 140: final JavaSourceCollector jsc = new JavaSourceCollector(new File(source)); 141: 142: final Iterator it = configuration.keySet().iterator(); 143: while (it.hasNext()) { 144: final String pName = (String) 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 target, final DescriptionModel model) throws IOException { 166: Log.debug ("Writing ..."); 167: final ModelWriter writer = new ModelWriter(); 168: writer.setModel(model); 169: final Writer w = new BufferedWriter(new FileWriter(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 target, final DescriptionModel model) throws IOException { 183: Log.debug ("Writing multiple files ..."); 184: final SplittingModelWriter writer = new SplittingModelWriter(); 185: writer.setModel(model); 186: writer.write(target); 187: } 188: 189: }