Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractOptionActionJavaGenerator.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractOptionActionJavaGenerator.java:1.1 --- /dev/null Sun Mar 16 04:02:59 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/AbstractOptionActionJavaGenerator.java Sun Mar 16 04:02:51 2008 @@ -0,0 +1,100 @@ +/* +* ##% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Tony Chemit +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package org.codelutin.option.generate.java; + +import org.codelutin.option.Option; + +import java.io.File; +import java.io.IOException; +import static java.lang.reflect.Modifier.ABSTRACT; +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, String, String)} + * pour générer une nouvelle action. + * + * @author tony + */ +public class AbstractOptionActionJavaGenerator extends AbstractJavaGenerator { + public static void doGenerate(File out, + long timestamp, + String i18nPrefix, + String parserPack, + String contextPack, + String parserSimpleName, + String contextSimpleName, + String simpleName, + String superClass) throws IOException { + String superClassWithType = superClass + "'; + String simpleNameWithType = simpleName + getClassType(); + AbstractOptionActionJavaGenerator gen; + gen = new AbstractOptionActionJavaGenerator(out, timestamp, i18nPrefix, parserPack,contextPack, simpleNameWithType, superClassWithType, parserSimpleName, contextSimpleName); + 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}\n@see {4}"); + + protected String parserSimpleName; + protected String contextSimpleName; + protected String contextPack; + + protected AbstractOptionActionJavaGenerator(File out, + long timestamp, + String i18nPrefix, + String pack, + String contextPack, + String simpleName, + String superClass, + String parserSimpleName, + String contextSimpleName) { + super(out, timestamp, i18nPrefix, ABSTRACT | PUBLIC, pack, simpleName, superClass); + this.parserSimpleName = parserSimpleName; + this.contextSimpleName = contextSimpleName; + this.contextPack=contextPack; + + } + + protected void addContent() { + // add a proptected constructor (this is an abstract class) + registerImport(normalImports, Option.class); + registerImport(normalImports,pack,parserSimpleName); + registerImport(normalImports, contextPack, contextSimpleName); + + builder.append('\n'); + // optionAction is now with default constructor + //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, contextSimpleName}, sb, new FieldPosition(0)); + return sb.toString(); + } + +} \ No newline at end of file Index: maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionActionJavaGenerator.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionActionJavaGenerator.java:1.1 --- /dev/null Sun Mar 16 04:02:59 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/generate/java/OptionActionJavaGenerator.java Sun Mar 16 04:02:54 2008 @@ -0,0 +1,145 @@ +/* +* ##% 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 java.io.File; +import java.io.IOException; +import static java.lang.reflect.Modifier.ABSTRACT; +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 OptionActionJavaGenerator 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 { + OptionActionJavaGenerator gen = new OptionActionJavaGenerator(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 OptionActionJavaGenerator(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); + registerImport(normalImports, parserPack, superClass); + + // 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