com.st.p2012.mind.Launcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mindoc Show documentation
Show all versions of mindoc Show documentation
Document generator for MIND
The newest version!
/**
\file com/st/p2012/mind/Launcher.java
\brief The launcher class for Mindoc
\n
This file is part of the Platform 2012 program,
a cooperation between STMicroelectronics and CEA.\n
Redistribution of this file to outside parties is
strictly prohibited without the written consent
of the module owner indicated below.\n
\par Module owner: [email protected]
\par Copyright (C) 2009 STMicroelectronics
\par Authors: [email protected]
\par Id: $Id$
\par Date: $Date$
\par Revision: $Rev$
*/
package com.st.p2012.mind;
import java.io.File;
import java.io.IOException;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
public class Launcher {
private static final String CONFIG_FILE_NAME = "resources/config.prop";
private static final String MINDOC_HOME = "MINDOC_HOME";
private static final String RESOURCE_DIR_NAME = "resources";
public static final String DOC_FILES_DIRECTORY = "doc-files";
public static final Logger logger = Logger.getAnonymousLogger();
private static final String COMMAND_NAME = "mindoc";
private static final String DEFAULT_DESTINATION = "./target/doc";
static final String HTML_RESOURCES_DIR = "resources/html";
private final static Options options = new Options();
private static final String DESTINATION_PATH_OPTION = "d";
private static final String VERBOSE_OPTION = "v";
private static final String HELP_OPTION = "help";
private static final String HELP_OPTION_SHORT = "h";
private static final String OVERVIEW_OPTION = "overview";
private static final String DOCTITLE_OPTION = "doctitle";
public static void main(final String[] args) {
initLogger();
if(System.getenv(MINDOC_HOME) == null ) {
logger.severe("MINDOC_HOME variable is not defined. MINDOC_HOME must point to the location where mindoc is installed.");
System.exit(1);
}
final File propertyFile = new File(System.getenv(MINDOC_HOME), CONFIG_FILE_NAME);
if(!propertyFile.canRead()) {
logger.severe("Cannot find configuration file " + propertyFile.getAbsolutePath());
System.exit(1);
} else {
try {
MindocConfig.loadProperties(propertyFile);
} catch (final IOException e) {
logger.severe("Error loading configuration file " + propertyFile.getAbsolutePath() + ": " + e.getLocalizedMessage());
System.exit(1);
}
}
final Option destinationPathOption = new Option(DESTINATION_PATH_OPTION, true, "The path where the documentation is generated.");
//destinationPathOption.setRequired(true);
final Option verboseOption = new Option(VERBOSE_OPTION, false, "Verbose output.");
final Option helpDirectoryOption = new Option(HELP_OPTION_SHORT, HELP_OPTION, false, "Print this message and exit.");
final Option overviewOption = new Option(OVERVIEW_OPTION, true, "Specifies the file that contains the overview documentation.");
final Option docTitleOption = new Option(DOCTITLE_OPTION, true, "Specifies the title that will be used in the the overview page.");
options.addOption(destinationPathOption);
options.addOption(verboseOption);
options.addOption(helpDirectoryOption);
options.addOption(overviewOption);
options.addOption(docTitleOption);
File sourceDirectories[] = null;
File targetDirectory = new File(DEFAULT_DESTINATION);
File overviewFile = null;
String docTitle = null;
final CommandLineParser parser = new PosixParser();
try {
final CommandLine cmd = parser.parse(options, args);
if(cmd.hasOption(HELP_OPTION)){
printHelp();
System.exit(0);
}
if(cmd.hasOption(VERBOSE_OPTION))
logger.setLevel(Level.ALL);
if(cmd.getArgs().length >= 1) {
final String sourceList[] = cmd.getArgs();
sourceDirectories = new File[sourceList.length];
for (int i = 0; i < sourceList.length; i++) {
final File sourceDirectory = new File(sourceList[i]);
if( !sourceDirectory.isDirectory() ||
!sourceDirectory.canRead()) {
logger.severe(String.format("Cannot read source path '%s'.", sourceDirectory.getPath()));
System.exit(2);
}
sourceDirectories[i] = sourceDirectory;
}
} else {
logger.severe("You must specify a source path.");
printHelp();
System.exit(1);
}
if(cmd.hasOption(DESTINATION_PATH_OPTION)) {
targetDirectory = new File(cmd.getOptionValue(DESTINATION_PATH_OPTION));
} else {
logger.info("Destination directory not specified. Documentation will be generated in default location (" + DEFAULT_DESTINATION + ").");
}
if(cmd.hasOption(OVERVIEW_OPTION)) {
overviewFile = new File(cmd.getOptionValue(OVERVIEW_OPTION));
}
if(cmd.hasOption(DOCTITLE_OPTION)) {
docTitle = cmd.getOptionValue(DOCTITLE_OPTION);
}
} catch (final ParseException e) {
logger.severe( "Command line parse error. Reason: " + e.getMessage() );
System.exit(1);
}
//check destination directory
if(targetDirectory.exists()) {
if(!targetDirectory.isDirectory()) {
logger.severe("Destination path must be a directory.");
System.exit(1);
}
if(!targetDirectory.canWrite()) {
logger.severe("Cannot write in destination directory.");
System.exit(1);
}
} else {
if(!targetDirectory.mkdirs()) {
logger.severe("Cannot create destination directory.");
System.exit(1);
}
}
runGenarators(sourceDirectories, targetDirectory, new File(getMindocHome(), RESOURCE_DIR_NAME), docTitle, overviewFile);
ResourceCopier.copyResources(sourceDirectories, targetDirectory);
logger.info("Documentation generated in " + targetDirectory.getPath());
}
private static void runGenarators(final File sourceDirectories[], final File targetDirectory, final File resourceDirectory, final String docTitle, final File overviewFile) {
try {
logger.fine("Generating indexes...");
final DocumentationIndexGenerator indexGenerator = new DocumentationIndexGenerator(sourceDirectories, resourceDirectory, docTitle, overviewFile);
indexGenerator.generateIndexPages(targetDirectory);
logger.fine("Generating documentation...");
final DefinitionTreeDocumentationGenerator definitionGenerator = new DefinitionTreeDocumentationGenerator(sourceDirectories);
definitionGenerator.generateDocumentation(targetDirectory);
} catch (final Exception e) {
logger.severe("Error while generating documentation: " + e.getLocalizedMessage());
System.exit(1);
}
}
private static void printHelp() {
final HelpFormatter formatter = new HelpFormatter();
final String header = " generates documentation for ADL, IDL and implementation files located in .";
formatter.printHelp(COMMAND_NAME + " [OPTION] ()+", header, options , null);
}
static String getMindocHome() {
return System.getenv(MINDOC_HOME);
}
private static void initLogger() {
final Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
logger.setLevel(Level.INFO);
logger.setUseParentHandlers(false);
logger.addHandler(consoleHandler);
}
private static class ConsoleHandler extends StreamHandler {
public ConsoleHandler() {
super(System.out, new Formatter() {
@Override
public String format(final LogRecord record) {
final StringBuilder sb = new StringBuilder();
if(record.getLevel() == Level.SEVERE) {
sb.append("Error: ");
} else if (record.getLevel() == Level.WARNING){
sb.append("Warning: ");
}
sb.append(record.getMessage());
sb.append('\n');
return sb.toString();
}
});
}
@Override
public synchronized void publish(final LogRecord arg0) {
super.publish(arg0);
flush();
}
}
}