Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.1 --- /dev/null Sun Dec 30 23:34:04 2007 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java Sun Dec 30 23:33:59 2007 @@ -0,0 +1,304 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Tony Chemit +* +* 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. +* ##% */ +package org.codelutin.option.generate; + +import org.apache.commons.lang.StringUtils; +import org.apache.maven.plugin.MojoFailureException; +import static org.codelutin.i18n.I18n._; +import org.codelutin.option.Config; +import org.codelutin.option.Option; +import org.codelutin.option.OptionAction; +import org.codelutin.option.OptionParser; +import org.codelutin.option.def.DefinitionParser; +import org.codelutin.option.def.DefinitionParserContexts.ConfigContext; +import org.codelutin.option.def.DefinitionParserContexts.OptionContext; +import org.codelutin.option.def.DefinitionParserUtil.TypeSource; +import org.codelutin.option.generate.java.AbstractActionJavaGenerator; +import org.codelutin.option.generate.java.ActionJavaGenerator; +import org.codelutin.option.generate.java.ConfigJavaGenerator; +import org.codelutin.option.generate.java.OptionJavaGenerator; +import org.codelutin.option.generate.java.OptionKeyJavaGenerator; +import org.codelutin.option.generate.java.OptionParserJavaGenerator; +import org.codelutin.option.generate.AbstractGeneratorGoal; + +import java.io.File; +import java.io.StringWriter; +import java.io.Writer; + +/** + * Permet de générer la factory de définitions d'options de ligne de commande, + * pour un fichier de propriété contenant la definition formelle des options. + * + * @author chemit + * @goal generate + * @phase compile + */ +public class JavaGeneratorGoal extends AbstractGeneratorGoal { + + /** + * @description Chemin du fichier de propriétés contenant les définitions d'options à  utiliser pour générer la factory de définitions d'options. + * @parameter expression="${commandline.source}" + * @required + */ + protected File source; + + /** + * @description Répertoire de sortie des classes. + * @parameter expression="${commandline.out}" default-value="${basedir}/target/gen/java" + */ + protected File out; + + /** + * @description nom du paquetage utilisé pour la génération du parseur + * @parameter expression="${commandline.parserPackageName}" + * @required + */ + protected String parserPackageName; + + /** + * @description type de source à  utiliser par le parser + * @parameter expression="${commandline.typeSource}" default-value="properties" + */ + protected String typeSource; + + /** + * @description flag to show errors of parsing. + * @parameter expression="${commandLine.showError}" default-value="true" + */ + protected boolean showErrors; + + /** + * @description nom du paquetage utilisé pour la génération des options + * @parameter expression="${commandline.optionPackageName}" + */ + protected String optionPackageName; + + /** + * @description nom du paquetage utilisé pour la génération des config + * @parameter expression="${commandline.configPackageName}" + */ + protected String configPackageName; + + /** + * @description nom du paquetage utilisé pour la génération des actions + * @parameter expression="${commandline.actionPackageName}" + */ + protected String actionPackageName; + + /** + * @description nom du parseur a generer + * @parameter expression="${commandline.parserSimpleName}" + */ + protected String parserSimpleName; + + /** + * @description prefix du nom des options a generer + * @parameter expression="${commandline.optionSimpleName}" + */ + protected String optionSimpleName; + + /** + * @description prefix du nom des config a generer + * @parameter expression="${commandline.configSimpleName}" + */ + protected String configSimpleName; + + /** + * @description prefix du nom des actions a generer + * @parameter expression="${commandline.actionSimpleName}" + */ + protected String actionSimpleName; + + /** + * @description la super classe du parseur a generer + * @parameter expression="${commandline.parserSuperClass}" default-value="org.codelutin.option.OptionParser" + */ + protected String parserSuperClass; + + /** + * @description la super classe des options a generer + * @parameter expression="${commandline.optionSuperClass}" default-value="org.codelutin.option.Option" + */ + protected String optionSuperClass; + + /** + * @description la super classe des configs a generer + * @parameter expression="${commandline.configSuperClass}" default-value="org.codelutin.option.Config" + */ + protected String configSuperClass; + + /** + * @description la super classe des actions a generer + * @parameter expression="${commandline.actionSuperClass}" default-value="org.codelutin.option.OptionAction" + */ + protected String actionSuperClass; + + protected DefinitionParser parser; + + public JavaGeneratorGoal() { + } + + public JavaGeneratorGoal(String prefix, String i18nCP, boolean verbose) { + super(prefix, i18nCP, verbose); + } + + protected void initAction() throws Exception { + checkInstanceOf(parserSuperClass, OptionParser.class); + checkInstanceOf(optionSuperClass, Option.class); + checkInstanceOf(actionSuperClass, OptionAction.class); + checkInstanceOf(configSuperClass, Config.class); + + if (optionPackageName == null) { + optionPackageName = parserPackageName + ".option"; + } + if (actionPackageName == null) { + actionPackageName = parserPackageName + ".action"; + } + if (configPackageName == null) { + configPackageName = parserPackageName + ".config"; + } + if (parserSimpleName == null) { + parserSimpleName = prefix + "OptionParser"; + } + if (optionSimpleName == null) { + optionSimpleName = prefix + "Option"; + } + if (actionSimpleName == null) { + actionSimpleName = prefix + "AbstractOptionAction"; + } + if (configSimpleName == null) { + configSimpleName = prefix + "AbstractConfig"; + } + + // make sure out exists + if (!out.exists()) { + out.mkdirs(); + } + + // get type of source for parser + TypeSource typeP = null; + try { + typeP = TypeSource.valueOf(typeSource); + } catch (Exception e) { + // ignore here (will be treate by parser) + } + + // get source (make sure to be on a absolute path) + source = new File(source.getAbsolutePath()); + + // do parse definitions and return parser + parser = DefinitionParser.doParse(typeP, source); + + log.info(_("commandline.parser.result.info", parser, parser.getOptions().length, parser.getConfigs().length)); + + // show errors + if (showErrors) { + if (parser.hasFailed()) { + Writer writer = new StringWriter(); + parser.printErrors(writer); + String lines = writer.toString(); + for (String s : lines.split("\n")) { + log.info(_("commandline.showErrors.info") + ' ' + s); + } + writer.flush(); + writer.close(); + + } else { + log.info(_("commandline.showErrors.no.error.info")); + } + } + + // quit if any error detected + if (parser.hasFailed()) { + throw new MojoFailureException(_("commandline.parser.parsing.error", parser, parser.getErrors().length)); + } + } + + public void doAction() throws Exception { + + String optionKeySimpleName = parserSimpleName; + int pos = optionKeySimpleName.indexOf("OptionParser"); + if (pos > -1) { + optionKeySimpleName = optionKeySimpleName.substring(0, pos); + } + optionKeySimpleName += "OptionKey"; + + // generate specialized abstract OptionAction for this parser + AbstractActionJavaGenerator.doGenerate(out, t0, i18nPrefix, + parserPackageName, + parserSimpleName, + actionSimpleName, + actionSuperClass + ); + + // generate specialized OptionKey for this parser + OptionKeyJavaGenerator.doGenerate(out, t0, i18nPrefix, + parserPackageName, + parserSimpleName, + optionKeySimpleName, + actionSimpleName + ); + + // generate OptionParser implementation + OptionParserJavaGenerator.doGenerate(out, t0, i18nPrefix, + parser.getOptions(), + parser.getConfigs(), + parserPackageName, + parserSimpleName, + optionKeySimpleName, + optionPackageName, + optionSimpleName, + configPackageName, + configSimpleName, + actionPackageName, + actionSimpleName, + parserSuperClass + ); + + // generate Config implementations + for (ConfigContext context : parser.getConfigs()) { + String suffix = StringUtils.capitalize(context.getCategory()); + ConfigJavaGenerator.doGenerate(out, t0, i18nPrefix, context, + configPackageName, + configSimpleName + suffix, + configSuperClass + ); + } + + // generate Option and OptionAction implementations + for (OptionContext context : parser.getOptions()) { + String suffix = StringUtils.capitalize(context.getKey()); + String optionClassName = optionSimpleName + suffix; + OptionJavaGenerator.doGenerate(out, t0, i18nPrefix, context, + optionPackageName, + optionClassName, + optionSuperClass + ); + ActionJavaGenerator.doGenerate(out, t0, i18nPrefix, context, + actionPackageName, + actionSimpleName + suffix, + parserPackageName + '.' + actionSimpleName + "<" + optionClassName + ">", + optionPackageName, + optionClassName, + parserPackageName, + parserSimpleName + ); + } + } +} \ No newline at end of file Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java:1.1 --- /dev/null Sun Dec 30 23:34:04 2007 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/AbstractGeneratorGoal.java Sun Dec 30 23:33:59 2007 @@ -0,0 +1,128 @@ +/* +* \#\#% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Tony Chemit +* +* 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. +* \#\#% */ +package org.codelutin.option.generate; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.codelutin.i18n.I18n; +import org.codelutin.log.LutinLogFactory; +import org.codelutin.util.Resource; +import org.codelutin.util.StringUtil; + +import java.io.File; +import static java.lang.System.getProperty; + +/** + * Classe de base pour une goal de génération + * + * @author tony + */ +public abstract class AbstractGeneratorGoal extends AbstractMojo { + + static { + System.setProperty("org.apache.commons.logging.LogFactory", LutinLogFactory.class.getName()); + } + + protected Log log = getLog(); + + /** + * @description Dépendance du projet. + * @parameter expression="${commandline.prefix}" + * @required + */ + protected String prefix; + + /** + * @description le chemin où trouver les fichiers de traductions pour l'initialisation I18N, sinon le fichier du plugin est utilisé, alors qu'il faut que ce soit celui de l'application + * @parameter expression="${commandline.i18nCP}" default-value="${basedir}/target/gen/i18n" + */ + protected String i18nCP; + /** + * @description flag pour afficher verbeusement ou non les logs + * @parameter expression="${commandline.verbose}" default-value="${maven.verbose}" + */ + protected boolean verbose; + + /** timestamp au démarrage (est positionné juste après l'init i18n) */ + protected long t0; + + /** prefix i18n pour la génération des clefs */ + protected String i18nPrefix; + + protected AbstractGeneratorGoal() { + } + + protected AbstractGeneratorGoal(String prefix, String i18nCP, boolean verbose) { + this.i18nCP = i18nCP; + this.prefix = prefix; + this.verbose = verbose; + } + + protected abstract void doAction() throws Exception; + + protected abstract void initAction() throws Exception; + + public void execute() throws MojoExecutionException, MojoFailureException { + t0 = System.currentTimeMillis(); + i18nPrefix = prefix.toLowerCase(); + try { + + // add i18n location as classpath + Resource.addDefaultClassLoader(new File(i18nCP).toURI().toURL()); + // init i18n + I18n.init2(getProperty("user.language"), getProperty("user.country"), "ISO-8859-1"); + + Logger logger = Logger.getLogger("org.codelutin.option"); + if (verbose) { + // set logger to debug level + if (logger != null) { + logger.setLevel(Level.DEBUG); + } + } else { + if (logger != null) { + logger.setLevel(Level.INFO); + } + } + initAction(); + + // do implented action + doAction(); + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } finally { + log.info("total time " + StringUtil.convertTime(System.currentTimeMillis() - t0)); + } + + } + + protected void checkInstanceOf(String givenClass, Class expectedClass) { + try { + Class clazz = Class.forName(givenClass,true,expectedClass.getClassLoader()); + if (!expectedClass.isAssignableFrom(clazz)) { + throw new IllegalArgumentException("required a " + expectedClass + " class but found a " + clazz); + } + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } + } +} Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java:1.1 --- /dev/null Sun Dec 30 23:34:04 2007 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/RstGeneratorGoal.java Sun Dec 30 23:33:59 2007 @@ -0,0 +1,100 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Tony Chemit +* +* 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. +* ##% */ +package org.codelutin.option.generate; + +import static org.codelutin.i18n.I18n._; +import org.codelutin.option.OptionParser; +import org.codelutin.option.ParserUtil; +import org.codelutin.option.generate.AbstractGeneratorGoal; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.Writer; +import java.net.MalformedURLException; + +/** + * Permet de générer la factory de définitions d'options de ligne de commande, + * pour un fichier de propriété contenant la definition formelle des options. + * + * @author chemit + * @goal test + * @phase test + */ +public class RstGeneratorGoal extends AbstractGeneratorGoal { + + /** + * @description nom du parseur a utiliser + * @parameter expression="${commandline.parserFQN}" + * @required + */ + protected String parserFQN; + + /** + * @description Target rst file. + * @parameter expression="${commandline.rstFilePath}" + * @required + */ + protected File rstFilePath; + + /** + * @description Répertoire de sortie des classes. + * @parameter expression="${commandline.out}" default-value="${basedir}/target/classes" + */ + protected File out; + + protected Class parserClass; + + @SuppressWarnings({"unchecked"}) + protected void initAction() throws ClassNotFoundException, MalformedURLException { + + //Resource.addDefaultClassLoader(out.toURI().toURL()); + + checkInstanceOf(parserFQN, OptionParser.class); + + // parse source + parserClass = (Class) Class.forName(parserFQN); + + if (!rstFilePath.getParentFile().exists()) { + rstFilePath.getParentFile().mkdirs(); + } + + } + + public void doAction() throws Exception { + + Writer w = null; + + try { + w = new BufferedWriter(new FileWriter(rstFilePath)); + + ParserUtil.toString(parserClass, w, + _("commandline.generateRstFile.head", prefix), + _("commandline.generateRstFile.prefix") + ' ', + _("commandline.generateRstFile.options.head", prefix), + _("commandline.generateRstFile.configs.head", prefix) + ); + log.info(_("commandline.generateRstFile.info", rstFilePath)); + } finally { + if (w != null) { + w.close(); + } + } + } +} \ No newline at end of file