    001/* ========================================================================
    002 * JCommon : a free general purpose class library for the Java(tm) platform
    003 * ========================================================================
    004 *
    005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006 *
    007 * Project Info:  http://www.jfree.org/jcommon/index.html
    008 *
    009 * This library is free software; you can redistribute it and/or modify it
    010 * under the terms of the GNU Lesser General Public License as published by
    011 * the Free Software Foundation; either version 2.1 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This library is distributed in the hope that it will be useful, but
    015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017 * License for more details.
    018 *
    019 * You should have received a copy of the GNU Lesser General Public
    020 * License along with this library; if not, write to the Free Software
    021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022 * USA.
    023 *
    024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025 * in the United States and other countries.]
    026 *
    027 * -------------------------
    028 * DescriptionGenerator.java
    029 * -------------------------
    030 * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
    031 *
    032 * Original Author:  Thomas Morgner;
    033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034 *
    035 * $Id: DescriptionGenerator.java,v 1.4 2005/10/18 13:32:20 mungady Exp $
    036 *
    037 * Changes
    038 * -------------------------
    039 * 21.06.2003 : Initial version
    040 *
    041 */
    042
    043package org.jfree.xml.generator;
    044
    045import java.io.BufferedInputStream;
    046import java.io.BufferedWriter;
    047import java.io.File;
    048import java.io.FileWriter;
    049import java.io.IOException;
    050import java.io.InputStream;
    051import java.io.Writer;
    052import java.net.URL;
    053import java.util.Iterator;
    054import java.util.Properties;
    055
    056import org.jfree.util.Log;
    057import org.jfree.util.PrintStreamLogTarget;
    058import org.jfree.util.ObjectUtilities;
    059import org.jfree.xml.generator.model.DescriptionModel;
    060
    061/**
    062 * A utility application for generating class descriptions.
    063 */
    064public final class DescriptionGenerator {
    065
    066    /**
    067     * Loads a property set from the specified URL.
    068     *
    069     * @param propertyURL  the URL.
    070     *
    071     * @return The properties.
    072     */
    073    private static Properties loadProperties(final URL propertyURL) {
    074        final Properties p = new Properties();
    075        try {
    076            final InputStream in = new BufferedInputStream(propertyURL.openStream());
    077            p.load(in);
    078            in.close();
    079        }
    080        catch (Exception e) {
    081            System.err.println("Unable to load properties from " + propertyURL);
    082        }
    083        return p;
    084    }
    085
    086    /**
    087     * Runs the generator, using the 'generator.properties' file for configuration info.
    088     *
    089     * @param args  command line arguments.
    090     *
    091     * @throws Exception if something goes wrong!
    092     */
    093    public static void main(final String[] args) throws Exception {
    094
    095        Log.getInstance().addTarget(new PrintStreamLogTarget());
    096
    097        URL propertyURL = ObjectUtilities.getResourceRelative
    098                ("generator.properties", DescriptionGenerator.class);
    099        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}