Package juju.reattore.util

Source Code of juju.reattore.util.VDocletCLI

/*  Reattore HTTP Server

    Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    $Id: VDocletCLI.java,v 1.2 2003/03/05 05:06:16 michaelh Exp $
*/

/* The above header is only there to satisfy the automatic code test
   tools; the following is the true copyright statement */

/*
Copyright (c) 2002, Mike Williams
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package juju.reattore.util;

import com.thoughtworks.qdox.model.JavaSource;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import vdoclet.docinfo.DocInfo;
import vdoclet.docinfo.QDoxBuilder;
import vdoclet.Generator;

/**
* Command-line entry-point to vDoclet
*/
public class VDocletCLI {

    //---( State )---
   
    /** output directory */
    private File destDir;
   
    /** control template */
    private String template;
   
    /** input files/directories */
    private String[] sourceFiles;

    /** input path */
    private String sourcePath;

    /** verbose flag */
    private boolean verbose = false;

    /** docinfo */
    private DocInfo docInfo;

    //---( Execution )---

    /** Main entry point.

        @param args Command line args.
        @throws Exception from the lower layers.
    */
    public static void main(String[] args) throws Exception {
        new VDocletCLI().execute(args);
    }
   
    private void execute(String[] args) throws Exception {
        parseCommandLine(args);
        validateCommandLine();
        parseSources();
        generate();
    }
   
    private void log(String msg) {
        if (verbose) {
            System.err.println(msg);
        }
    }

    /**
     * Parse the command-line
     */
    private void parseCommandLine(String[] args) throws Exception {
        int i = 0;
        while (i < args.length && args[i].startsWith("-")) {
            if (args[i].equals("-d")) {
                destDir = new File(args[++i]);
            }
            else if (args[i].equals("-s")) {
                sourcePath = args[++i];
            }
            else if (args[i].equals("-t")) {
                template = args[++i];
            }
            else if (args[i].equals("-v")) {
                verbose = true;
            }
            else {
                throw new Exception("Illegal command-line arg: "
                                     + args[i]);
            }
            i++;
        }
        int nFiles = args.length - i;
        sourceFiles = new String[nFiles];
        System.arraycopy(args, i, sourceFiles, 0, nFiles);
    }

    /**
     * Check that we have all the required args
     */
    private void validateCommandLine() throws Exception {
        if (destDir == null) {
            throw new Exception("missing -d argument");
        }
        if (template == null) {
            throw new Exception("missing -t argument");
        }
        if (sourceFiles.length == 0) {
            throw new Exception("no source-files provided");
        }
    }

    /**
     * Parse the source-files
     */
    private void parseSources() throws IOException {   
        com.thoughtworks.qdox.JavaDocBuilder builder =
            new com.thoughtworks.qdox.JavaDocBuilder();
        ArrayList sourceDirList = new ArrayList();
        for (int i = 0; i < sourceFiles.length; i++) {
            log("reading " + sourceFiles[i]);
            File f = new File(sourceFiles[i]);
            if (f.isDirectory()) {
                builder.addSourceTree(f);
                sourceDirList.add(f);
            }
            else {
                builder.addSource(new FileReader(f));
            }
        }
        JavaSource[] javaSources = builder.getSources();
        log(javaSources.length + " source-files");
        docInfo = QDoxBuilder.build(javaSources);

        if (sourcePath != null) {
            docInfo.setSourcePath(sourcePath);
        }
        else {
            String[] srcPath =
                (String[]) sourceDirList.toArray(new String[0]);
            docInfo.setSourcePath(srcPath);
        }
    }
   
    /**
     * Kick-start generation
     */
    private void generate() throws Exception {
        Generator generator = new Generator(destDir);
        generator.setAttribute("docInfo", docInfo);
        generator.setAttribute("toolbox", new Toolbox());
        generator.eval(template,
                        new OutputStreamWriter(System.out));
    }

}
TOP

Related Classes of juju.reattore.util.VDocletCLI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.