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 normalImports; + + /** la liste des imports statiques à ajouter */ + protected List staticImports; + + /** internal builder of class's header */ + protected StringBuilder builder; + + /** internal builder of class */ + protected StringBuilder headerBuilder; + + protected AbstractJavaGenerator(File out, long timestamp, String i18nPrefix, int modifiers, String pack, String simpleName, String superClass, String... interfaceClass) { + this.out = out; + this.timestamp = timestamp; + this.i18nPrefix = i18nPrefix; + this.pack = pack; + this.simpleName = simpleName; + this.simpleNameWithNoType = getNameWithNoType(simpleName); + this.superClass = superClass; + this.superClassWithNoType = getNameWithNoType(superClass); + this.interfaceClass = interfaceClass; + this.modifiers = modifiers; + this.userName = System.getProperty("user.name", getClass().getSimpleName()); + } + + protected String getNameWithNoType(String simpleName) { + int pos = simpleName.indexOf('<'); + return pos == -1 ? simpleName : simpleName.substring(0, pos); + } + + protected void generate() throws IOException { + builder = new StringBuilder(); + headerBuilder = new StringBuilder(); + normalImports = new ArrayList(); + staticImports = new ArrayList(); + try { + openClass(); + addContent(); + closeClass(); + generateHeader(); + writeClass(); + } finally { + builder = null; + normalImports.clear(); + staticImports.clear(); + normalImports = null; + staticImports = null; + } + } + + public File getOut() { + return out; + } + + public long getTimestamp() { + return timestamp; + } + + public String getFQN() { + return pack + '.' + simpleName; + } + + protected String invokeI18N(String suffix) { + return "_(\"" + i18nPrefix + "." + suffix + "\")"; + } + + protected void invokeMethod(StringBuilder builder, String caller, String nmae, Object... params) { + builder.append(caller).append('.').append(nmae).append('('); + if (params.length > 0) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < params.length - 1; i++) { + Object param = params[i]; + if (param instanceof String) { + String s = (String) param; + if (s.startsWith("#")) { + sb.append(", _").append("(\"").append(i18nPrefix).append('.').append(s.substring(1)).append("\")"); + } else if (s.startsWith("!")) { + sb.append(", ").append(s.substring(1)); + } else { + sb.append(", \"").append(param).append("\""); + } + } else if (param instanceof Class) { + sb.append(", ").append(((Class) param).getSimpleName()).append(".class"); + } else if (param instanceof Enum) { + registerStaticImport(staticImports, ((Enum) param).getDeclaringClass(), param + ""); + sb.append(", ").append(param); + } else { + sb.append(", ").append(param); + } + } + Object param = params[params.length - 1]; + if (param instanceof String) { + String s = (String) param; + if (s.startsWith("#")) { + sb.append(", _").append("(\"").append(s.substring(1)).append("\")"); + } else if (s.startsWith("!")) { + sb.append(", ").append(s.substring(1)); + } else { + sb.append(", \"").append(param).append("\""); + } + } else if (param instanceof String[]) { + for (String o : (String[]) param) { + sb.append(", \"").append(o).append("\""); + } + } else if (param instanceof Class) { + sb.append(", ").append(((Class) param).getSimpleName()).append(".class"); + } else if (param instanceof Enum) { + registerStaticImport(staticImports, param.getClass().getEnclosingClass(), param + ""); + sb.append(", ").append(param); + } else { + sb.append(", ").append(param); + } + builder.append(sb.toString().substring(2)); + } + builder.append(");\n"); + } + + protected String buildGetterMethodName(boolean booleanValueType, String methodName) { + methodName = (booleanValueType ? "is" : "get") + methodName; + return methodName; + } + + protected void registerImport(List imports, Class clazz) { + if (clazz.isPrimitive() || clazz.getPackage().getName().equals("java.lang") || clazz.getPackage().getName().equals(pack)) { + return; + } + registerImport(imports, clazz.getName()); + } + + protected void registerImport(List imports, String pack, String clazz) { + if (pack.equals("java.lang") || this.pack.equals(pack)) { + return; + } + registerImport(imports, pack + "." + clazz); + } + + protected void registerImport(List imports, String fqn) { + if (imports.contains(fqn)) { + return; + } + imports.add(fqn); + } + + protected void registerStaticImport(List imports, Class clazz, String fqn) { + registerStaticImport(imports, clazz.getName() + "." + fqn); + } + + protected void registerStaticImport(List imports, String fqn) { + if (imports.contains(fqn)) { + return; + } + imports.add(fqn); + } + + protected void openClass() { + //builder.append("\n"); + addJavadoc("", generateClassJavadoc()); + builder.append("\n"); + addModifiers(modifiers); + builder.append("class ").append(simpleName); + if (superClass != null) { + builder.append(" extends ").append(superClass); + } + if (interfaceClass.length > 0) { + builder.append(" implements "); + StringBuilder sb = new StringBuilder(); + for (String interfaceClas : interfaceClass) { + sb.append(", ").append(interfaceClas); + } + builder.append(sb.toString().substring(2)); + } + builder.append(" {"); + } + + protected void addGetterMethod(String prefix, String methodName, String body) { + builder.append("\n ").append(prefix).append(" ").append(methodName).append("() {\n ").append(body).append("\n }").append("\n"); + } + + protected void addStaticField(String name, String type, String javadoc, int modifiers, String init) { + addField(name, type, javadoc, modifiers | Modifier.STATIC, init); + } + + protected void addStaticMethod(String returnType, String name, String javadoc, int modifiers, String body, String[] exceptions, String... params) { + addMethod(returnType, name, javadoc, modifiers | Modifier.STATIC, body, exceptions, params); + } + + protected void addStaticBloc(String content) { + builder.append("\n\n static {"); + for (String line : content.split("\n")) { + builder.append("\n ").append(line); + } + builder.append("\n }"); + } + + protected void addField(String name, String type, String javadoc, int modifiers, String init) { + addJavadoc(" ", javadoc); + builder.append("\n "); + addModifiers(modifiers); + builder.append(type).append(" ").append(name); + if (init != null) { + builder.append(" = ").append(init); + } + builder.append(";"); + } + + protected void addConstructor(String javadoc, int modifiers, String body, String[] exceptions, String... params) { + addMethod(null, simpleNameWithNoType, javadoc, modifiers, body, exceptions, params); + } + + protected void addMethod(String returnType, String name, String javadoc, int modifiers, String body, String[] exceptions, String... params) { + addJavadoc(" ", javadoc); + builder.append("\n "); + addModifiers(modifiers); + if (returnType != null) { + builder.append(returnType); + builder.append(" "); + } + builder.append(name); + builder.append("("); + if (params.length > 0) { + StringBuilder sb = new StringBuilder(); + for (String param : params) { + sb.append(", ").append(param); + } + builder.append(sb.toString().substring(2)); + } + builder.append(")"); + if (exceptions != null && exceptions.length > 0) { + StringBuilder sb = new StringBuilder(); + for (String exception : exceptions) { + sb.append(", ").append(exception); + } + builder.append(" throws").append(sb.toString().substring(2)); + } + builder.append(" {\n"); + for (String s : body.split("\n")) { + builder.append(" ").append(s).append('\n'); + } + builder.append(" }"); + } + + protected void closeClass() { + builder.append("\n}\n"); + } + + protected void generateHeader() { + headerBuilder.append(LICENCE).append("\n"); + headerBuilder.append("\npackage ").append(pack).append(";\n"); + if (!staticImports.isEmpty()) { + Collections.sort(staticImports); + for (String anImport : staticImports) { + headerBuilder.append("\nimport static ").append(anImport).append(";"); + } + headerBuilder.append("\n"); + } + Collections.sort(normalImports); + for (String anImport : normalImports) { + headerBuilder.append("\nimport ").append(anImport).append(";"); + } + } + + protected void writeClass() throws IOException { + String filename = pack.replaceAll("\\.", File.separator); + if (filename.length() > 0) { + filename += File.separator; + } + filename += simpleNameWithNoType + ".java"; + File f = new File(out, filename); + if (!f.getParentFile().exists()) { + f.getParentFile().mkdirs(); + } + String content = headerBuilder.toString() + builder.toString(); + log.info(getFQN()); + log.trace(content); + FileUtil.writeString(f, content); + } + + protected void addModifiers(int modifiers) { + if (Modifier.isStatic(modifiers)) { + builder.append("static "); + } + if (Modifier.isPublic(modifiers)) { + builder.append("public "); + } + if (Modifier.isProtected(modifiers)) { + builder.append("protected "); + } + if (Modifier.isPrivate(modifiers)) { + builder.append("private "); + } + if (Modifier.isFinal(modifiers)) { + builder.append("final "); + } + if (Modifier.isAbstract(modifiers)) { + builder.append("abstract "); + } + if (Modifier.isTransient(modifiers)) { + builder.append("transient "); + } + if (Modifier.isVolatile(modifiers)) { + builder.append("volatile "); + } + } + + protected void addJavadoc(String prefix, String doc) { + + if (doc != null && !"".equals(doc.trim())) { + + builder.append("\n\n").append(prefix).append("/**"); + if (doc.indexOf('\n') == -1) { + builder.append(" ").append(doc).append(" */"); + } else { + for (String s : doc.split("\n")) { + builder.append("\n").append(prefix).append(" * ").append(s); + } + builder.append("\n").append(prefix).append(" */"); + } + } + } + +} \ No newline at end of file Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionParserJavaGenerator.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionParserJavaGenerator.java:1.1 --- /dev/null Sun Dec 30 23:34:04 2007 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionParserJavaGenerator.java Sun Dec 30 23:33:58 2007 @@ -0,0 +1,294 @@ +/* +* ##% 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.i18n.I18n; +import org.codelutin.option.ConfigKey; +import org.codelutin.option.OptionParser; +import org.codelutin.option.def.ArgumentType; +import org.codelutin.option.def.ArgumentValueType; +import org.codelutin.option.def.DefinitionParser; +import org.codelutin.option.def.DefinitionParserContexts.ArgumentContext; +import org.codelutin.option.def.DefinitionParserContexts.ConfigContext; +import org.codelutin.option.def.DefinitionParserContexts.GroupContext; +import org.codelutin.option.def.DefinitionParserContexts.OptionContext; +import org.codelutin.option.def.DefinitionParserUtil; +import org.codelutin.option.def.OptionDefinition; +import org.codelutin.option.def.OptionDefinitionBuilder; +import org.codelutin.option.generate.java.AbstractJavaGenerator; +import org.codelutin.util.StringUtil; + +import java.io.File; +import java.io.IOException; +import static java.lang.reflect.Modifier.FINAL; +import static java.lang.reflect.Modifier.PUBLIC; +import java.text.FieldPosition; +import java.text.MessageFormat; + +/** + * Permet de générer un {@link OptionParser} à  partir des résultats du parsing + * d'un {@link DefinitionParser}. + *

+ * Utiliser la méthode {@link #doGenerate(File,long, String, OptionContext[], ConfigContext[],String, String,String, String, String, String, String, String,String,String)} + * pour générer un nouveeau parseur. + * + * @author chemit + */ +public class OptionParserJavaGenerator extends AbstractJavaGenerator { + public static void doGenerate(File out, + long timestamp, + String i18nPrefix, + OptionContext[] ocontexts, + ConfigContext[] ccontexts, + String pack, + String simpleName, + String optionKeySimpleName, + String optionPack, + String optionClassPrefix, + String configPack, + String configClassPrefix, + + String actionPack, + String actionClassPrefix, + String superClass) throws IOException { + OptionParserJavaGenerator gen; + gen = new OptionParserJavaGenerator(out, timestamp, i18nPrefix, ocontexts, ccontexts, pack, simpleName, optionKeySimpleName, optionPack, optionClassPrefix, configPack, configClassPrefix, actionPack, actionClassPrefix, superClass); + gen.generate(); + } + + private static final String SUFFIX_OPTION_FIELD = "_OPTION_KEY"; + private static final String SUFFIX_CONFIG_FIELD = "_CONFIG_KEY"; + + private static final String BUILD_DEFINITIONS_JAVADOC = "Initialise le parser avec les d\u00E9finitions d'options\n\n@param builder le builder utilisé pour construire les définitions"; + + private static final MessageFormat CLASS_JAVADOC_MESSAGE = new MessageFormat("Implantation du parseur d''options.\n\n@author {0}\n@see {1}"); + + protected String optionPack; + protected String optionKeySimpleName; + protected String optionClassPrefix; + protected String configPack; + protected String configClassPrefix; + protected String actionPack; + protected String actionClassPrefix; + + protected OptionContext[] ocontexts; + protected ConfigContext[] ccontexts; + + protected OptionParserJavaGenerator(File out, + long timestamp, + String i18nPrefix, + OptionContext[] ocontexts, + ConfigContext[] ccontexts, + String pack, + String simpleName, + String optionKeySimpleName, + String optionPack, + String optionClassPrefix, + String configPack, + String configClassPrefix, + String actionPack, + String actionClassPrefix, + String superClass) { + super(out, timestamp, i18nPrefix, PUBLIC, pack, simpleName, superClass); + this.optionKeySimpleName = optionKeySimpleName; + this.optionPack = optionPack; + this.configPack = configPack; + this.actionPack = actionPack; + this.optionClassPrefix = optionClassPrefix; + this.configClassPrefix = configClassPrefix; + this.actionClassPrefix = actionClassPrefix; + this.ocontexts = ocontexts; + this.ccontexts = ccontexts; + + } + + protected void addContent() { + registerStaticImport(staticImports, I18n.class, "_"); + registerImport(normalImports, OptionDefinitionBuilder.class); + registerImport(normalImports, OptionDefinition.class); + + StringBuilder staticBloc = new StringBuilder(); + // add ConfigKey constant fields + addConfigKeyFields(staticBloc); + // add OptionKey constant fields + addOptionKeyFields(staticBloc); + // add configs shared instances accessors + for (ConfigContext context : ccontexts) { + generateConfigAccessor(context); + } + // add static bloc to initiliaze the constants + addStaticBloc(staticBloc.toString().substring(1)); + // add static method to build the definition by programmation + String body = new ToInitMethodSourceWalker().doWalk(ocontexts); + addStaticMethod("void", "buildDefinitions", BUILD_DEFINITIONS_JAVADOC, 0, body, null, OptionDefinitionBuilder.class.getSimpleName() + " builder"); + } + + @Override + protected String generateClassJavadoc() { + StringBuffer sb = new StringBuffer(); + CLASS_JAVADOC_MESSAGE.format(new Object[]{userName, superClass}, sb, new FieldPosition(0)); + return sb.toString(); + } + + + protected void addOptionKeyFields(StringBuilder sb) { + if (ocontexts.length == 0) { + return; + } + String classPrefix; + classPrefix = optionKeySimpleName; + String factoryMethod = "new" + optionKeySimpleName; + registerStaticImport(staticImports, pack + "." + optionKeySimpleName + "." + factoryMethod); + + sb.append("\n// build options definitions"); + sb.append("\nOptionDefinition[] definitions = buildDefinitions(").append(simpleNameWithNoType).append(".class);"); + sb.append("\n// options key init"); + for (int i = 0; i < ocontexts.length; i++) { + OptionContext context = ocontexts[i]; + String key = context.getKey(); + String suffix = StringUtils.capitalize(key); + String optionImpl = optionClassPrefix + suffix; + String actionImpl = actionClassPrefix + suffix; + if (!pack.equals(optionPack)) { + registerImport(normalImports, optionPack + "." + optionImpl); + } + if (!pack.equals(actionPack)) { + registerImport(normalImports, actionPack + "." + actionImpl); + } + String clazz = classPrefix + "<" + optionImpl + ',' + actionImpl + ">"; + //String clazz = classPrefix + "<" + optionImpl + ',' + simpleName + ',' + actionImpl + ">"; + String fieldName = StringUtil.convertToConstantName(key) + SUFFIX_OPTION_FIELD; + String javadoc = "La clef pour acc\u00E9der \u00E0 l'option de clef " + 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 + "'; + String simpleNameWithType = simpleName + getClassType(); + AbstractActionJavaGenerator gen; + gen = new AbstractActionJavaGenerator(out, timestamp, i18nPrefix, parserPack, simpleNameWithType, superClassWithType, parserSimpleName); + gen.generate(); + } + + public static String getClassType() { + return ""; + } + + private static final MessageFormat CLASS_JAVADOC_MESSAGE = new MessageFormat("Implantation de base des actions sp\u00E9cifiques au parseur {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() + ""; + String simpleNameWithType = simpleName + getClassType(abstractActionSimpleName); + OptionKeyJavaGenerator gen; + gen = new OptionKeyJavaGenerator(out, timestamp, i18nPrefix, parserPack, simpleNameWithType, superClassWithType, parserSimpleName, abstractActionSimpleName); + gen.generate(); + } + + public static String getClassType(String abstractActionSimpleName) { + return ">"; + } + + private static final MessageFormat CLASS_JAVADOC_MESSAGE = new MessageFormat("Implantation de clef d''option sp\u00E9cifique pour le parseur {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 + ""; + StringBuilder body = new StringBuilder(); + body.append("return new ").append(simpleNameWithNoType).append("(key, optionClass, abstractActionClass, description, definition);"); + builder.append('\n'); + addStaticMethod(returnType, methodName, null, PUBLIC, body.toString(), null, "String key", "Class optionClass", "Class abstractActionClass", "String description", "OptionDefinition definition"); + // add private constructor + builder.append('\n'); + addConstructor(null, PRIVATE, "super(key, optionClass, abstractActionClass, description, definition);", null, "String key", "Class optionClass", "Class abstractActionClass", "String description", "OptionDefinition definition"); + } + + @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