Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/ConfigJavaGenerator.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/ConfigJavaGenerator.java:1.1 --- /dev/null Sun Dec 30 23:34:04 2007 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/ConfigJavaGenerator.java Sun Dec 30 23:33:58 2007 @@ -0,0 +1,151 @@ +/* +* ##% 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.java; + +import javassist.Modifier; +import org.apache.commons.lang.StringUtils; +import org.codelutin.i18n.I18n; +import org.codelutin.option.ConfigPropertyKey; +import org.codelutin.option.def.DefinitionParserContexts.ConfigContext; +import org.codelutin.option.def.DefinitionParserContexts.ConfigPropertyContext; +import org.codelutin.option.generate.java.AbstractJavaGenerator; + +import java.io.File; +import java.io.IOException; +import static java.lang.reflect.Modifier.ABSTRACT; +import static java.lang.reflect.Modifier.PROTECTED; +import static java.lang.reflect.Modifier.PUBLIC; +import java.text.FieldPosition; +import java.text.MessageFormat; + +/** + * Generateur d'implantation de Config. + *
+ * Utiliser la méthode {@link #doGenerate(File, long, String, ConfigContext, String, String, String)} + * pour générer la nouvelle config. + * + * @author tony + */ +public class ConfigJavaGenerator extends AbstractJavaGenerator { + + public static void doGenerate(File out, + long timestamp, + String i18nPrefix, + ConfigContext context, + String pack, + String simpleName, + String superClass) throws IOException { + ConfigJavaGenerator gen; + gen = new ConfigJavaGenerator(out, timestamp, i18nPrefix, context, pack, simpleName, superClass); + gen.generate(); + } + + private static final MessageFormat CLASS_JAVADOC_MESSAGE = new MessageFormat("Implantation de la configuration de cat\u00E9gorie{0}.\n\n@author {1}\n@see {2}");
+
+ private static final String SUFFIX_CONFIG_PROPERTY_FIELD = "_PROPERTY_KEY";
+
+ protected ConfigContext context;
+
+ protected ConfigJavaGenerator(File out,
+ long timestamp,
+ String i18nPrefix,
+ ConfigContext context,
+ String pack,
+ String simpleName,
+ String superClass) {
+ super(out, timestamp, i18nPrefix, ABSTRACT | PUBLIC, pack, simpleName, superClass);
+ this.context = context;
+ }
+
+ protected void addContent() {
+ registerStaticImport(staticImports, ConfigPropertyKey.class, "newConfigPropertyKey");
+ registerImport(normalImports, ConfigPropertyKey.class);
+ registerStaticImport(staticImports, I18n.class, "_");
+
+ StringBuilder staticBloc = new StringBuilder();
+ // add constant ConfigPropertyKey fields
+ String classPrefix = ConfigPropertyKey.class.getSimpleName();
+ for (ConfigPropertyContext property : this.context.getContexts()) {
+ String key = property.getKey();
+ Class> type = property.getType();
+ registerImport(normalImports, type);
+ String clazz = classPrefix + "<" + type.getSimpleName() + ">";
+ String fieldName = getConstantFieldName(key, SUFFIX_CONFIG_PROPERTY_FIELD);
+ String javadoc = "La clef pour acc\u00E9der \u00E0 la propri\u00E9t\u00E9 " + key + " de la config";
+ String description = invokeI18N(property.getKey() + ".description");
+ String params = "\"" + key + "\", " + type.getSimpleName() + ".class, " + property.getModifiers() + ", " + description;
+ Object value = property.getDefaultValue();
+ if (value != null) {
+ params += ", \"" + value + "\"";
+ }
+ staticBloc.append('\n').append(fieldName).append(" = newConfigPropertyKey(").append(params).append(");");
+ addStaticField(fieldName, clazz, javadoc, Modifier.PUBLIC | Modifier.FINAL, null);
+ }
+ // add propertec constructor (this is an abstract class)
+ builder.append('\n');
+ addConstructor(null, PROTECTED, "super(\"" + context.getCategory() + "\");", null);
+ // add properties typed accessors
+ for (ConfigPropertyContext propertyContext : context.getContexts()) {
+ generatePropertyAccessor(propertyContext);
+ }
+ // add static bloc to initialize the constant fields
+ addStaticBloc(staticBloc.toString().substring(1));
+ }
+
+
+ @Override
+ protected String generateClassJavadoc() {
+ StringBuffer sb = new StringBuffer();
+ CLASS_JAVADOC_MESSAGE.format(new Object[]{context.getCategory(), userName, superClass}, sb, new FieldPosition(0));
+ return sb.toString();
+ }
+
+ @Override
+ protected String invokeI18N(String suffix) {
+ return super.invokeI18N("config." + context.getCategory() + "." + suffix);
+ }
+
+ protected void generatePropertyAccessor(ConfigPropertyContext context) {
+ String key = context.getKey();
+ String argumentKeyCap = StringUtils.capitalize(key);
+ Class> type = context.getType();
+ //int modifiers = context.getModifiers();
+ registerImport(normalImports, type);
+ String typeAsStr = type.getSimpleName();
+ boolean booleanValueType = type == Boolean.class;
+ if (booleanValueType) {
+ typeAsStr = "boolean";
+ }
+ String methodName = buildGetterMethodName(booleanValueType, argumentKeyCap);
+ String fieldName = getConstantFieldName(key, SUFFIX_CONFIG_PROPERTY_FIELD);
+ String javadoc = generatePropertyAccessorJavadoc(context, fieldName);
+ addMethod(typeAsStr, methodName, javadoc, PUBLIC, "return getProperty(" + fieldName + ");", null);
+ }
+
+ protected String generatePropertyAccessorJavadoc(ConfigPropertyContext config, String fieldName) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Accesseur de la valeur de la propri\u00E9t\u00E9 ");
+ sb.append(config.getKey());
+ sb.append("\nde la configuration, ou sa valeur par d\u00E9faut si elle existe.");
+ sb.append("\n\n@return la valeur de la propri\u00E9t\u00E9 ou sa valeur par d\u00E9faut (si elle existe).");
+ sb.append("\n@see #").append(fieldName).append('\n');
+ return sb.toString();
+ }
+}
\ No newline at end of file
Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractJavaGenerator.java
diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractJavaGenerator.java:1.1
--- /dev/null Sun Dec 30 23:34:04 2007
+++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractJavaGenerator.java Sun Dec 30 23:33:58 2007
@@ -0,0 +1,427 @@
+/*
+* ##% 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.java;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.codelutin.util.FileUtil;
+import org.codelutin.util.StringUtil;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Cette classe sert de base pour générer des sources java.
+ *
+ * @author tony
+ */
+public abstract class AbstractJavaGenerator {
+
+ /** la méthode à implanter pour ajouter du code dans la classe */
+ protected abstract void addContent();
+
+ private static final Log log = LogFactory.getLog(AbstractJavaGenerator.class);
+
+ public static String getConstantFieldName(String key, String suffix) {
+ return StringUtil.convertToConstantName(key) + suffix;
+ }
+
+ private static final String LICENCE = "/* \n" +
+ "* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin.\n" +
+ "*\n" +
+ "* This program is free software; you can redistribute it and/or\n" +
+ "* modify it under the terms of the GNU General Public License\n" +
+ "* as published by the Free Software Foundation; either version 2\n" +
+ "* of the License, or (at your option) any later version.\n" +
+ "*\n" +
+ "* This program is distributed in the hope that it will be useful,\n" +
+ "* but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
+ "* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
+ "* GNU General Public License for more details.\n" +
+ "*\n" +
+ "* You should have received a copy of the GNU General Public License\n" +
+ "* along with this program; if not, write to the Free Software\n" +
+ "* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n" +
+ "* ##% */";
+
+ /** @return la javadoc de la classe à générer */
+ protected String generateClassJavadoc() {
+ return null;
+ }
+
+ /** chemin où générer le source */
+ protected File out;
+
+ /** timestamp pour la sauvegarde des classes */
+ protected long timestamp;
+
+ /** le nom de l'utilisateur qui a lance la generation */
+ protected String userName;
+
+ /** le prefix i18n à appliquer */
+ protected String i18nPrefix;
+
+ /** le package de destination de la classe */
+ protected String pack;
+
+ /** le nom simple de la classe */
+ protected String simpleName;
+
+ /** le nom simple de la classe sans typage (s'il y en avait un) */
+ protected String simpleNameWithNoType;
+
+ /** le FQN (ou le nom simple) de la superClasse */
+ protected String superClass;
+
+ /** le FQN (ou le nom simple) de la superclasse sans typage (s'il y en avait un) */
+ protected String superClassWithNoType;
+
+ /** les FQN (ou les noms simples) des interfaces qu'implémente la classe */
+ protected String[] interfaceClass;
+
+ /** les modifiers de la classe */
+ protected int modifiers;
+
+ /** la liste des imports à ajouter */
+ protected List" + key + "";
+ String description = invokeI18N("option.description." + key);
+ String params = "\"" + key + "\", " + optionImpl + ".class, " + actionImpl + ".class, " + description + ", definitions[" + i + "]";
+ sb.append('\n').append(fieldName).append(" = ").append(factoryMethod).append('(').append(params).append(");");
+ addStaticField(fieldName, clazz, javadoc, PUBLIC | FINAL, null);
+ }
+ }
+
+ protected void addConfigKeyFields(StringBuilder sb) {
+ if (ccontexts.length == 0) {
+ return;
+ }
+ registerImport(normalImports, ConfigKey.class);
+ registerStaticImport(staticImports, ConfigKey.class, "newConfigKey");
+ sb.append("\n// configs key init");
+ String classPrefix;
+ classPrefix = ConfigKey.class.getSimpleName();
+ for (ConfigContext context : ccontexts) {
+ String key = context.getCategory();
+ String suffix = StringUtils.capitalize(key);
+ String configImpl = configClassPrefix + suffix;
+ if (!pack.equals(configPack)) {
+ registerImport(normalImports, configPack + "." + configImpl);
+ }
+ String clazz = classPrefix + "<" + configImpl + ">";
+ String fieldName = StringUtil.convertToConstantName(key) + SUFFIX_CONFIG_FIELD;
+ String javadoc = "La clef pour acc\u00E9der \u00E0 la config de cat\u00E9gorie " + key + "";
+ String description = invokeI18N("config.description." + key);
+ String params = "\"" + key + "\", " + configImpl + ".class, " + description;
+ sb.append('\n').append(fieldName).append(" = newConfigKey(").append(params).append(");");
+ addStaticField(fieldName, clazz, javadoc, PUBLIC | FINAL, null);
+ }
+ }
+
+
+ protected void generateConfigAccessor(ConfigContext context) {
+ String key = context.getCategory();
+ String suffix = StringUtils.capitalize(key);
+ String methodName = "get" + suffix + "Config";
+ String fieldName = getConstantFieldName(key, SUFFIX_CONFIG_FIELD);
+ String abstractClass = configClassPrefix + suffix;
+ String javadoc = generateConfigAccessorJavadoc(abstractClass, key, fieldName);
+ addMethod(abstractClass, methodName, javadoc, PUBLIC, "return getConfig(" + fieldName + ");", null);
+ }
+
+ protected String generateConfigAccessorJavadoc(String abstractClass, String key, String fieldName) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("Accesseur typ\u00E9 de l'instance partag\u00E9e de la configuration de cat\u00E9gorie ").append(key).append("\n(implantantion abstraite dans la classe ");
+ sb.append(abstractClass).append(")\n.");
+ sb.append("\n\n@return l'unique instance parta\u00E9e de la configuration de cat\u00E9gorie ").append(key).append("");
+ sb.append("\n@see #").append(fieldName);
+ sb.append("\n@see ").append(abstractClass).append('\n');
+ return sb.toString();
+ }
+
+ private final static MessageFormat OPTION_BEGIN = new MessageFormat("// option {0}/{1} : {2}\n");
+
+ /**
+ * A walker to generate the content of buildDefinition(OptionDefinitionBuilder)
+ * method.
+ *
+ * @author chemit
+ */
+ class ToInitMethodSourceWalker extends DefinitionParserUtil.DefinitionParserWalker {
+
+ protected StringBuilder builder;
+
+ public String doWalk(OptionContext[] ocontexts) {
+ this.builder = new StringBuilder();
+ invokeBuilderMethod("beginBuilder", ocontexts.length);
+ walk(ocontexts);
+ invokeBuilderMethod("endBuilder");
+ return builder.toString();
+ }
+
+ @Override
+ protected void enterOption(OptionContext option, int optionIndex) {
+ StringBuffer sb = new StringBuffer();
+ OPTION_BEGIN.format(new Object[]{optionIndex + 1, option.getParent().getContexts().size(), option.getKey()}, sb, new FieldPosition(0));
+ builder.append(sb.toString());
+ invokeBuilderMethod("beginOption", option.getContexts().size());
+ }
+
+ @Override
+ protected void exitOption(OptionContext option, int optionIndex) {
+ invokeBuilderMethod("endOption", option.getKey(), option.getMin(), option.getMax(), option.getAlias());
+ }
+
+
+ @Override
+ protected void enterGroup(GroupContext group, int groupIndex) {
+ invokeBuilderMethod("beginGroup", group.getContexts().size());
+ }
+
+ @Override
+ protected void exitGroup(GroupContext group, int groupIndex) {
+ invokeBuilderMethod("endGroup", group.getPos());
+ }
+
+ @Override
+ protected void enterArgument(ArgumentContext argument, int argumentIndex) {
+ ArgumentType type = argument.getType();
+ if (type == ArgumentType.constant) {
+ invokeBuilderMethod("addConstantArgument", argument.getKey(), argument.getMin());
+ } else {
+ ArgumentValueType valueType = argument.getValueType();
+ registerStaticImport(staticImports, ArgumentType.class, type + "");
+ registerStaticImport(staticImports, ArgumentValueType.class, valueType + "");
+ invokeBuilderMethod("addArgument", type, valueType, argument.getKey(), argument.getMin(), argument.getMax());
+ }
+ }
+
+ private void invokeBuilderMethod(String methodName, Object... params) {
+ invokeMethod(builder, "builder", methodName, params);
+ }
+ }
+}
\ No newline at end of file
Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractActionJavaGenerator.java
diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractActionJavaGenerator.java:1.1
--- /dev/null Sun Dec 30 23:34:04 2007
+++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractActionJavaGenerator.java Sun Dec 30 23:33:58 2007
@@ -0,0 +1,91 @@
+/*
+* ##% 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.java;
+
+import org.codelutin.option.Option;
+import org.codelutin.option.generate.java.AbstractJavaGenerator;
+
+import java.io.File;
+import java.io.IOException;
+import static java.lang.reflect.Modifier.ABSTRACT;
+import static java.lang.reflect.Modifier.PROTECTED;
+import static java.lang.reflect.Modifier.PUBLIC;
+import java.text.FieldPosition;
+import java.text.MessageFormat;
+
+/**
+ * Generateur d'implantation de clef d'Option.
+ *
+ * Utiliser la méthode {@link #doGenerate(java.io.File,long, String, String, String, String,String)}
+ * pour générer une nouvelle action.
+ *
+ * @author tony
+ */
+public class AbstractActionJavaGenerator extends AbstractJavaGenerator {
+ public static void doGenerate(File out,
+ long timestamp,
+ String i18nPrefix,
+ String parserPack,
+ String parserSimpleName,
+ String simpleName,
+ String superClass) throws IOException {
+ String superClassWithType = superClass + "{0}.\n\n@author {1}\n@see {2}\n@see {3}");
+
+ protected String parserSimpleName;
+
+ protected AbstractActionJavaGenerator(File out,
+ long timestamp,
+ String i18nPrefix,
+ String pack,
+ String simpleName,
+ String superClass,
+ String parserSimpleName) {
+ super(out, timestamp, i18nPrefix, ABSTRACT | PUBLIC, pack, simpleName, superClass);
+ this.parserSimpleName = parserSimpleName;
+
+ }
+
+ protected void addContent() {
+ // add a proptected constructor (this is an abstract class)
+ registerImport(normalImports, Option.class);
+ //registerImport(normalImports, superClassWithNoType);
+ builder.append('\n');
+ addConstructor(null, PROTECTED, "super(parser);", null, parserSimpleName + " parser");
+ }
+
+ @Override
+ protected String generateClassJavadoc() {
+ StringBuffer sb = new StringBuffer();
+ CLASS_JAVADOC_MESSAGE.format(new Object[]{parserSimpleName, userName, superClassWithNoType, parserSimpleName}, sb, new FieldPosition(0));
+ return sb.toString();
+ }
+
+}
\ No newline at end of file
Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/ActionJavaGenerator.java
diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/ActionJavaGenerator.java:1.1
--- /dev/null Sun Dec 30 23:34:04 2007
+++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/ActionJavaGenerator.java Sun Dec 30 23:33:58 2007
@@ -0,0 +1,146 @@
+/*
+* ##% 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.java;
+
+import org.apache.commons.lang.StringUtils;
+import org.codelutin.option.def.ArgumentType;
+import static org.codelutin.option.def.ArgumentValueType.BOOLEAN;
+import org.codelutin.option.def.DefinitionParserContexts.ArgumentContext;
+import org.codelutin.option.def.DefinitionParserContexts.GroupContext;
+import org.codelutin.option.def.DefinitionParserContexts.OptionContext;
+import org.codelutin.option.generate.java.AbstractJavaGenerator;
+
+import java.io.File;
+import java.io.IOException;
+import static java.lang.reflect.Modifier.ABSTRACT;
+import static java.lang.reflect.Modifier.PROTECTED;
+import static java.lang.reflect.Modifier.PUBLIC;
+import java.text.FieldPosition;
+import java.text.MessageFormat;
+
+/**
+ * Le générateur d'action.
+ *
+ * Utiliser la méthode
+ * {@link #doGenerate(File,long,String, OptionContext , String, String, String, String, String, String, String)}
+ *
+ * pour générer une nouvelle action.
+ *
+ * @author tony
+ */
+public class ActionJavaGenerator extends AbstractJavaGenerator {
+
+ public static void doGenerate(File out,
+ long timestamp,
+ String i18nPrefix,
+ OptionContext context,
+ String pack,
+ String simpleName,
+ String superClass,
+ String optionPack,
+ String optionClass,
+ String parserPack,
+ String parserClass) throws IOException {
+ ActionJavaGenerator gen = new ActionJavaGenerator(out, timestamp, i18nPrefix, context,
+ pack, simpleName, superClass,
+ optionPack, optionClass,
+ parserPack, parserClass
+ );
+ gen.generate();
+ }
+
+ private static final MessageFormat CLASS_JAVADOC_MESSAGE = new MessageFormat("Implantation de l''action associ\u00E9e à l''option {0}\n" + "qui est implant\u00E9e dans la classe {1}.\n\n@author {2}\n@see {3}\n@see {4}");
+
+ protected OptionContext context;
+ protected String optionPack, parserPack;
+
+ protected String optionClass, parserClass;
+
+ protected ActionJavaGenerator(File out,
+ long timestamp,
+ String i18nPrefix,
+ OptionContext context,
+ String pack,
+ String simpleName,
+ String superClass,
+ String optionPack,
+ String optionClass,
+ String parserPack,
+ String parserClass) {
+ super(out, timestamp, i18nPrefix, PUBLIC | ABSTRACT, pack, simpleName, superClass);
+ this.optionClass = optionClass;
+ this.parserClass = parserClass;
+ this.optionPack = optionPack;
+ this.parserPack = parserPack;
+ this.context = context;
+ }
+
+ protected void addContent() {
+ // add protected constructor (this is an abstract class)
+ builder.append('\n');
+ registerImport(normalImports, optionPack, optionClass);
+ registerImport(normalImports, parserPack, parserClass);
+ addConstructor(null, PROTECTED, "super(parser);", null, parserClass + " parser");
+ // add delegated accessor to option argumentents
+ if (context.getGroups() != null) {
+ for (GroupContext groupContext : context.getGroups()) {
+ for (ArgumentContext argumentContext : groupContext.getArguments()) {
+ generateArgumentAccessor(argumentContext);
+ }
+ }
+ }
+ }
+
+ @Override
+ protected String generateClassJavadoc() {
+ StringBuffer sb = new StringBuffer();
+ CLASS_JAVADOC_MESSAGE.format(new Object[]{context.getKey(), optionClass, userName, optionClass, parserClass}, sb, new FieldPosition(0));
+ return sb.toString();
+ }
+
+ protected void generateArgumentAccessor(ArgumentContext argument) {
+ String key = argument.getKey();
+ String argumentKeyCap = StringUtils.capitalize(key);
+ Class type = argument.getValueType().getClazz();
+ registerImport(normalImports, type);
+ String typeAsStr = type.getSimpleName();
+ boolean multi = argument.getMax() == -1 || argument.getMax() > 1;
+
+ boolean booleanValueType = argument.getValueType() == BOOLEAN || argument.getType() == ArgumentType.constant;
+ if (booleanValueType) {
+ typeAsStr = "boolean";
+ }
+ String methodName;
+ methodName = buildGetterMethodName(booleanValueType, argumentKeyCap);
+ String javadoc = generateArgumentAccessorJavadoc(methodName);
+ addMethod(typeAsStr, methodName, javadoc, PUBLIC, "return option." + methodName + "();", null);
+ if (multi) {
+ methodName = buildGetterMethodName(false, argumentKeyCap + "s");
+ javadoc = generateArgumentAccessorJavadoc(methodName);
+ addMethod(typeAsStr + "[]", methodName, javadoc, PUBLIC, "return option." + methodName + "();", null);
+ }
+ }
+
+ protected String generateArgumentAccessorJavadoc(String methodName) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("@return see {@link ").append(optionClass).append('#').append(methodName).append("()}");
+ return sb.toString();
+ }
+}
\ No newline at end of file
Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionJavaGenerator.java
diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionJavaGenerator.java:1.1
--- /dev/null Sun Dec 30 23:34:04 2007
+++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionJavaGenerator.java Sun Dec 30 23:33:58 2007
@@ -0,0 +1,194 @@
+/*
+* ##% 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.java;
+
+import org.apache.commons.lang.StringUtils;
+import org.codelutin.log.LutinLogFactory;
+import org.codelutin.option.Argument;
+import org.codelutin.option.def.ArgumentType;
+import org.codelutin.option.def.ArgumentValueType;
+import org.codelutin.option.def.DefinitionParserContexts.ArgumentContext;
+import org.codelutin.option.def.DefinitionParserContexts.GroupContext;
+import org.codelutin.option.def.DefinitionParserContexts.OptionContext;
+import org.codelutin.option.generate.java.AbstractJavaGenerator;
+import org.codelutin.util.CardinalityHelper;
+
+import java.io.File;
+import java.io.IOException;
+import static java.lang.reflect.Modifier.PUBLIC;
+import java.text.FieldPosition;
+import java.text.MessageFormat;
+
+/**
+ * Generateur d'implantation d'Option.
+ *
+ * Utiliser la méthode {@link #doGenerate(File,long, String, OptionContext , String, String, String)}
+ * pour générer une nouvelle action.
+ *
+ * @author tony
+ */
+public class OptionJavaGenerator extends AbstractJavaGenerator {
+
+ static {
+ System.setProperty("org.apache.commons.logging.LogFactory", LutinLogFactory.class.getName());
+ }
+
+ public static void doGenerate(File out,
+ long timestamp,
+ String i18nPrefix,
+ OptionContext context,
+ String pack,
+ String simpleName,
+ String superClass) throws IOException {
+ OptionJavaGenerator gen;
+ gen = new OptionJavaGenerator(out, timestamp, i18nPrefix, context, pack, simpleName, superClass);
+ gen.generate();
+ }
+
+ private static final MessageFormat CLASS_JAVADOC_MESSAGE = new MessageFormat("Implantation de l''option {0}.\n\n@author {1}\n@see {2}");
+
+ protected OptionContext context;
+
+ protected OptionJavaGenerator(File out,
+ long timestamp,
+ String i18nPrefix,
+ OptionContext context,
+ String pack,
+ String simpleName,
+ String superClass) {
+ super(out, timestamp, i18nPrefix, PUBLIC, pack, simpleName, superClass);
+ this.context = context;
+ }
+
+ protected void addContent() {
+ registerImport(normalImports, Argument.class);
+ // add constructor
+ builder.append('\n');
+ addConstructor(null, PUBLIC, "super(usedAlias, arguments);", null, "String usedAlias", "Argument... arguments");
+ // add arguments typed accessors
+ if (context.getGroups() != null) {
+ for (GroupContext groupContext : context.getGroups()) {
+ for (ArgumentContext argumentContext : groupContext.getArguments()) {
+ generateArgumentAccessor(groupContext, argumentContext);
+ }
+ }
+ }
+ }
+
+ @Override
+ protected String generateClassJavadoc() {
+ StringBuffer sb = new StringBuffer();
+ CLASS_JAVADOC_MESSAGE.format(new Object[]{context.getKey(), userName, superClass}, sb, new FieldPosition(0));
+ return sb.toString();
+ }
+
+ protected void generateArgumentAccessor(GroupContext group, ArgumentContext argument) {
+ int position = group.getPos();
+ String key = argument.getKey();
+ String argumentKeyCap = StringUtils.capitalize(key);
+ Class type = argument.getValueType().getClazz();
+ registerImport(normalImports, type);
+ String typeAsStr = type.getSimpleName();
+
+ boolean booleanValueType = argument.getValueType() == ArgumentValueType.BOOLEAN || argument.getType() == ArgumentType.constant;
+ if (booleanValueType) {
+ typeAsStr = "boolean";
+ }
+
+ boolean multi = argument.getMax() == -1 || argument.getMax() > 1;
+
+ boolean named = false;
+ boolean valued = false;
+ String methodName;
+ String argkey = null;
+ switch (argument.getType()) {
+ case constant:
+ break;
+ case valued:
+ valued = true;
+ break;
+ case namedAndValued:
+ argkey = "\"" + argument.getKey() + "\"";
+ valued = true;
+ named = true;
+ break;
+ }
+ methodName = buildGetterMethodName(booleanValueType, argumentKeyCap);
+ String javadoc = generateArgumentAccessorJavadoc(group, argument, typeAsStr, false);
+ if (valued) {
+ // add a typed getter T getArgument
+ String params = position + ", " + typeAsStr + ".class, " + argkey + "," + named;
+ addMethod(typeAsStr, methodName, javadoc, PUBLIC, "return getValuedArgumentValue(" + params + ");", null);
+ if (multi) {
+ methodName = buildGetterMethodName(false, argumentKeyCap + "s");
+ typeAsStr += "[]";
+ // add a typed getter T[] getArguments
+ javadoc = generateArgumentAccessorJavadoc(group, argument, typeAsStr, true);
+ addMethod(typeAsStr, methodName, javadoc, PUBLIC, "return getValuedArgumentValues(" + params + ");", null);
+ }
+ } else {
+ addMethod(typeAsStr, methodName, javadoc, PUBLIC, "return \"" + key + "\".equals(getConstantArgumentValue(" + position + "));", null);
+ }
+ }
+
+ protected String generateArgumentAccessorJavadoc(GroupContext group, ArgumentContext argument, String type, boolean multi) {
+ StringBuilder sb = new StringBuilder();
+ boolean isConstant = argument.getType() == ArgumentType.constant;
+ if (CardinalityHelper.isRepetable(argument.getMax())) {
+ if (multi) {
+ sb.append("Accesseur de toutes les valeurs de l'argument ");
+ sb.append(argument.getKey());
+ sb.append("\ndu groupe d'arguments \u00E0 la position ");
+ sb.append(group.getPos());
+ sb.append(".");
+ } else {
+ sb.append("Accesseur de la premi\u00E8re valeur de l'argument ");
+ sb.append(argument.getKey());
+ sb.append("\ndu groupe d'arguments \u00E0 la position ");
+ sb.append(group.getPos());
+ sb.append(".");
+ }
+ } else {
+ if (isConstant) {
+ sb.append("Accesseur de pr\u00E9sence de l'argument ");
+ sb.append(argument.getKey());
+ sb.append("\nde type constant dans le groupe d'arguments \u00E0 la position ");
+ sb.append(group.getPos());
+ sb.append(".");
+ } else {
+ sb.append("Accesseur de la valeur de l'argument ");
+ sb.append(argument.getKey());
+ sb.append("\ndu groupe d'arguments \u00E0 la position ");
+ sb.append(group.getPos());
+ sb.append(".");
+ }
+ }
+ if (multi) {
+ sb.append("\n\n@return toutes les valeurs de l'argument de type ").append(type).append(" ou un tableau vide si auncune valeur trouv\u00E9e.\n");
+ } else {
+ if (isConstant) {
+ sb.append("\n\n@return true si cet argument a \u00E9t\u00E9 rencontr\u00E9, false sinon.\n");
+ } else {
+ sb.append("\n\n@return la valeur de l'argument de type ").append(type).append(" ou null si auncune valeur trouv\u00E9e.\n");
+ }
+ }
+ return sb.toString();
+ }
+}
Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionKeyJavaGenerator.java
diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionKeyJavaGenerator.java:1.1
--- /dev/null Sun Dec 30 23:34:04 2007
+++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionKeyJavaGenerator.java Sun Dec 30 23:33:59 2007
@@ -0,0 +1,103 @@
+/*
+* ##% 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.java;
+
+import org.codelutin.option.Option;
+import org.codelutin.option.OptionKey;
+import org.codelutin.option.def.OptionDefinition;
+import org.codelutin.option.generate.java.AbstractJavaGenerator;
+
+import java.io.File;
+import java.io.IOException;
+import static java.lang.reflect.Modifier.PRIVATE;
+import static java.lang.reflect.Modifier.PUBLIC;
+import java.text.FieldPosition;
+import java.text.MessageFormat;
+
+/**
+ * Generateur d'implantation de clef d'Option.
+ *
+ * Utiliser la méthode {@link #doGenerate(java.io.File,long, String, String, String, String, String)}
+ * pour générer une nouvelle action.
+ *
+ * @author tony
+ */
+public class OptionKeyJavaGenerator extends AbstractJavaGenerator {
+ public static void doGenerate(File out,
+ long timestamp,
+ String i18nPrefix,
+ String parserPack,
+ String parserSimpleName,
+ String simpleName,
+ String abstractActionSimpleName
+ ) throws IOException {
+ String superClassWithType = OptionKey.class.getSimpleName() + "{0}.\n\n@author {1}\n@see {2}\n@see {3}");
+
+ protected String parserSimpleName;
+ protected String abstractActionSimpleName;
+
+ protected OptionKeyJavaGenerator(File out,
+ long timestamp,
+ String i18nPrefix,
+ String pack,
+ String simpleName,
+ String superClass,
+ String parserSimpleName,
+ String abstractActionSimpleName) {
+ super(out, timestamp, i18nPrefix, PUBLIC, pack, simpleName, superClass);
+ this.parserSimpleName = parserSimpleName;
+ this.abstractActionSimpleName = abstractActionSimpleName;
+ }
+
+ protected void addContent() {
+ registerImport(normalImports, Option.class);
+ registerImport(normalImports, OptionKey.class);
+ registerImport(normalImports, OptionDefinition.class);
+ // add factory static method newXXX
+ String methodName = "new" + simpleNameWithNoType;
+ String returnType = getClassType(abstractActionSimpleName) + " " + simpleNameWithNoType + "