Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java diff -u maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.4 maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.5 --- maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java:1.4 Tue Jan 1 17:21:31 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/JavaGeneratorGoal.java Thu Jan 3 05:47:08 2008 @@ -20,8 +20,8 @@ import org.apache.commons.lang.StringUtils; import org.apache.maven.plugin.MojoFailureException; -import static org.codelutin.i18n.I18n._; import org.codelutin.i18n.I18n; +import static org.codelutin.i18n.I18n._; import org.codelutin.option.Config; import org.codelutin.option.Option; import org.codelutin.option.OptionAction; @@ -40,13 +40,15 @@ import java.io.File; import java.io.StringWriter; import java.io.Writer; +import java.util.Map; +import java.util.TreeMap; /** * 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 + * @goal java * @phase compile */ public class JavaGeneratorGoal extends AbstractGeneratorGoal { @@ -144,29 +146,40 @@ protected String configSuperClass; /** - * @description la super classe des actions a generer + * @description fqn de la super classe des actions a generer * @parameter expression="${commandline.actionSuperClass}" default-value="org.codelutin.option.OptionAction" */ protected String actionSuperClass; protected DefinitionParser parser; - + protected boolean needActionConstructor; + public JavaGeneratorGoal() { } - public JavaGeneratorGoal(String prefix, boolean verbose) { - super(prefix, verbose); - } - protected void initAction() throws Exception { + // init i18n - I18n.initISO88591(System.getProperty("user.language"), System.getProperty("user.country")); - + I18n.initISO88591(); + checkInstanceOf(parserSuperClass, OptionParser.class); checkInstanceOf(optionSuperClass, Option.class); - checkInstanceOf(actionSuperClass, OptionAction.class); + //checkInstanceOf(actionSuperClass, OptionAction.class); checkInstanceOf(configSuperClass, Config.class); + initClassLoader(); + if (needSpecializedAction()) { + needActionConstructor = true; + } else { + //TODO We can not do this, source are still not compiled... + // check superClass has a default public constructor + /*Class aClass = Class.forName(actionSuperClass,false,loader); + Constructor cons = aClass.getDeclaredConstructor(); + if (Modifier.isPublic(cons.getModifiers())) { + throw new IllegalArgumentException("could not find public constructor for class " + aClass); + }*/ + needActionConstructor = false; + } if (optionPackageName == null) { optionPackageName = parserPackageName + ".options"; } @@ -233,35 +246,52 @@ } } + public JavaGeneratorGoal(String prefix, boolean verbose) { + super(prefix, verbose); + } + public void doAction() throws Exception { String optionKeySimpleName = parserSimpleName; + OptionContext[] optionContexts = parser.getOptions(); + ConfigContext[] configContexts = parser.getConfigs(); + int pos = optionKeySimpleName.indexOf("OptionParser"); if (pos > -1) { optionKeySimpleName = optionKeySimpleName.substring(0, pos); } optionKeySimpleName += "OptionKey"; + Map map = new TreeMap(); + // build a dictonary of options implementations name + for (OptionContext context : optionContexts) { + String suffix = StringUtils.capitalize(context.getKey()); + String optionClassName = optionSimpleName + suffix; + map.put(context.getKey(),optionClassName); + } + // generate specialized abstract OptionAction for this parser - AbstractActionJavaGenerator.doGenerate(out, t0, i18nPrefix, - parserPackageName, - parserSimpleName, - actionSimpleName, - actionSuperClass - ); + if (needSpecializedAction()) { + AbstractActionJavaGenerator.doGenerate(out, t0, i18nPrefix, + parserPackageName, + parserSimpleName, + actionSimpleName, + actionSuperClass + ); + } // generate specialized OptionKey for this parser OptionKeyJavaGenerator.doGenerate(out, t0, i18nPrefix, parserPackageName, parserSimpleName, optionKeySimpleName, - actionSimpleName + actionSuperClass ); // generate OptionParser implementation OptionParserJavaGenerator.doGenerate(out, t0, i18nPrefix, - parser.getOptions(), - parser.getConfigs(), + optionContexts, + configContexts, parserPackageName, parserSimpleName, optionKeySimpleName, @@ -275,7 +305,7 @@ ); // generate Config implementations - for (ConfigContext context : parser.getConfigs()) { + for (ConfigContext context : configContexts) { String suffix = StringUtils.capitalize(context.getCategory()); ConfigJavaGenerator.doGenerate(out, t0, i18nPrefix, context, configPackageName, @@ -284,24 +314,35 @@ ); } - // generate Option and OptionAction implementations - for (OptionContext context : parser.getOptions()) { - String suffix = StringUtils.capitalize(context.getKey()); - String optionClassName = optionSimpleName + suffix; + + // generate Option implementations + for (OptionContext context : optionContexts) { OptionJavaGenerator.doGenerate(out, t0, i18nPrefix, context, optionPackageName, - optionClassName, + map.get(context.getKey()), optionSuperClass ); + } + + // generate OptionAction implementations + for (OptionContext context : optionContexts) { + String suffix = StringUtils.capitalize(context.getKey()); + String optionClassName = map.get(context.getKey()); ActionJavaGenerator.doGenerate(out, t0, i18nPrefix, context, actionPackageName, actionSimpleName + suffix, - parserPackageName + '.' + actionSimpleName + "<" + optionClassName + ">", + actionSuperClass+ "<" + optionClassName + ">", optionPackageName, optionClassName, parserPackageName, - parserSimpleName + parserSimpleName, + needActionConstructor ); } + map.clear(); + } + + private boolean needSpecializedAction() { + return actionSuperClass.equals(OptionAction.class.getName()); } } \ No newline at end of file