Index: lutincommandline/src/java/org/codelutin/option/ConfigKey.java diff -u lutincommandline/src/java/org/codelutin/option/ConfigKey.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/ConfigKey.java:1.2 --- lutincommandline/src/java/org/codelutin/option/ConfigKey.java:1.1.1.1 Sat Feb 9 15:05:36 2008 +++ lutincommandline/src/java/org/codelutin/option/ConfigKey.java Sat Mar 15 09:05:17 2008 @@ -18,7 +18,9 @@ * ##% */ package org.codelutin.option; -import static org.codelutin.i18n.I18n._;import java.lang.reflect.Modifier; +import static org.codelutin.i18n.I18n._; + +import java.lang.reflect.Modifier; /** * Une classe pour représenter une clef de config typée. @@ -77,7 +79,6 @@ return new ConfigKey(key, configClass, description); } - /** la clef non typée de l'option */ final protected String category; @@ -130,15 +131,21 @@ */ public synchronized C getConfig() { if (config == null) { - if (configClass == null) { - throw new IllegalStateException("no concrete config found for " + abstractConfigClass); - } - try { - config = configClass.getConstructor().newInstance(); - } catch (Exception e) { - throw new RuntimeException(e); - } + throw new IllegalStateException("the config was not init, use first method initConfig " + this); } return config; } + + public void initConfig(OptionParser optionParser) { + if (configClass == null) { + throw new IllegalStateException("no concrete config found for " + abstractConfigClass); + } + try { + //TODO should test if the constructor exists ? + config = configClass.getConstructor(optionParser.getClass()).newInstance(optionParser); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } } \ No newline at end of file Index: lutincommandline/src/java/org/codelutin/option/OptionParserContexts.java diff -u lutincommandline/src/java/org/codelutin/option/OptionParserContexts.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/OptionParserContexts.java:1.2 --- lutincommandline/src/java/org/codelutin/option/OptionParserContexts.java:1.1.1.1 Sat Feb 9 15:05:35 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionParserContexts.java Sat Mar 15 09:05:17 2008 @@ -78,7 +78,7 @@ } /** - * le context de base � utiliser dans le parseur d'options {@link OptionParser} + * le context de base à utiliser dans le parseur d'options {@link OptionParser} * * @author chemit */ @@ -104,27 +104,27 @@ } /** - * Cette classe contient les contextes d'options d�tect�s pendand le parsing. + * Cette classe contient les contextes d'options détectés pendand le parsing. *

- * Elle est de visibilit� package, car elle n'a pas � �tre utilis�e ailleurs. + * Elle est de visibilité package, car elle n'a pas à être utilisée ailleurs. * * @author chemit */ static class ParserContext extends AbstractOptionParserContext { - /** le dictionnaire des definitions d'options index�es par leurs alias */ + /** le dictionnaire des definitions d'options indexées par leurs alias */ protected final Map indexAlias; /** order on options by their names */ protected final List definitionsIndex; - /** les occurences des options trouv�es (est mis � jour dans la m�thode */ + /** les occurences des options trouvées (est mis à jour dans la méthode */ final int[] optionFounds; - /** les positions des args non utilis�s */ + /** les positions des args non utilisés */ final List unusedArguments; - /** les options disponibles pendant la d�tection des args */ + /** les options disponibles pendant la détection des args */ final List availabledOptions; int pos = 0; @@ -160,22 +160,22 @@ } /** - * Detecte les options et leurs arguments, � partir de la suite des - * arguments donn�s et construit la liste des parser d'options. + * Detecte les options et leurs arguments, à partir de la suite des + * arguments donnés et construit la liste des parser d'options. *

- * D�tecte les contextes d'options � analyser. - * Une fois un contexte d'option d�tect� (un alias et les arguments + * Détecte les contextes d'options à analyser. + * Une fois un contexte d'option détecté (un alias et les arguments * jusqu'au prochain alias), on instancie un {@link OptionContext}, - * et on lance la m�thode + * et on lance la méthode * {@link #addSon(org.codelutin.option.ParserUtil.AbstractParserContext)}. *

- * Cette m�thode suit l'algorthime g�n�rique, on surcharge + * Cette méthode suit l'algorthime générique, on surcharge * {@link #preAddSonHook(OptionContext)} , pour lancer l'analyse de - * l'option, et la m�thode {@link #postAddSonHook(OptionContext)} - * pour mettre � jour la table d'occurrences des options trouv�es si + * l'option, et la méthode {@link #postAddSonHook(OptionContext)} + * pour mettre à jour la table d'occurrences des options trouvées si * l'option est valide. * - * @param args la liste des arguments � scanner + * @param args la liste des arguments à scanner */ protected void detectOptions(String... args) { int length = args.length; @@ -279,17 +279,17 @@ /** la position de l'alias dans la ligne de commande */ final int commandLinePosition; - /** la liste des arguments � traiter pour cette option */ + /** la liste des arguments à traiter pour cette option */ final String[] argumentsToTreate; /** l'alias utilis� pour cette option dans la ligne de commande */ final String alias; /** la d�finition de l'option */ final OptionDefinition definition; - /** les occurences des groups trouv�s */ + /** les occurences des groups trouvés */ final int[] groupFounds; /** la liste des groupes possibles pour cette option */ final List groups; - /** la liste des groupes possibles pendant la d�tection des arguments */ + /** la liste des groupes possibles pendant la détection des arguments */ final List availabledGroups; /** le dernier groupe d'argument valide et encore disponible */ @@ -392,7 +392,7 @@ for (ArgumentContext argumentContext : contexts) { args.add(argumentContext.instanciate()); } - OptionKey key = parser.getOptionKey(definition.getKey()); + OptionKey, ? extends OptionAction> key = parser.getOptionKey(definition.getKey()); return key.addOption(alias, args); } catch (Exception e) { throw new RuntimeException(e); @@ -574,9 +574,9 @@ /** @author tony */ static class ArgumentContext extends AbstractOptionParserContext { - /** la d�finition de l'argument */ + /** la définition de l'argument */ final ArgumentDefinition definition; - /** la d�finition du group de l'argument */ + /** la définition du group de l'argument */ final GroupDefinition groupDefinition; /** la position dans la ligne de commande */ final int commandLinePosition; @@ -584,7 +584,7 @@ final int pos; /** l'argument correspondant dans la ligne de commande */ final String argument; - /** la valeur typ�e de l'argument */ + /** la valeur typée de l'argument */ final Object value; protected ArgumentContext(OptionContext parent, Index: lutincommandline/src/java/org/codelutin/option/Config.java diff -u lutincommandline/src/java/org/codelutin/option/Config.java:1.4 lutincommandline/src/java/org/codelutin/option/Config.java:1.5 --- lutincommandline/src/java/org/codelutin/option/Config.java:1.4 Sun Feb 24 18:48:43 2008 +++ lutincommandline/src/java/org/codelutin/option/Config.java Sat Mar 15 09:05:17 2008 @@ -376,8 +376,7 @@ * @param b flag to says, remove safeKey */ public void clearSafeData(boolean b) { - for (Iterator it = safeKeys.iterator(); it.hasNext();) - { + for (Iterator it = safeKeys.iterator(); it.hasNext();) { ConfigPropertyKey safeKey = it.next(); safeKey.clear(b); it.remove(); @@ -479,15 +478,14 @@ // a shame that Properties.store can only deal with String values if (val == null) { getTmp().put(keyStr, ""); - } - else { - if(val.getClass().isEnum()) { + } else { + if (val.getClass().isEnum()) { // always store the nam of an enum,since toString // method can be ovveride in Enum implementation // This is a problem to convert from string // using name is safe, will awlays succeded to convert // using EnumConverter - getTmp().put(keyStr, ((Enum)val).name()); + getTmp().put(keyStr, ((Enum) val).name()); } else { getTmp().put(keyStr, val.toString()); } @@ -579,8 +577,9 @@ if (source == null || !source.exists()) { return; } - InputStream inputStream = new FileInputStream(source); + InputStream inputStream = null; try { + inputStream = new FileInputStream(source); load(inputStream); } finally { if (inputStream != null) { @@ -607,7 +606,7 @@ log.debug(key.getKey() + " = " + defaultValue); setProperty(key, defaultValue); } - } + } } protected void loadFromSystem() { @@ -696,12 +695,12 @@ // on doit retirer ce préfixe int pos = kStr.indexOf('.'); if (pos > -1) { - kStr = kStr.substring(pos+1); + kStr = kStr.substring(pos + 1); } else { // pour assurer la compatibilité avec les versions // antiérieures d'Isis qui ne supportaient pas le préfixage // des clefs par catégorie - if (getKey(kStr)!=null) { + if (getKey(kStr) != null) { // have a common key with another config // can not treate this one continue; Index: lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java diff -u lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java:1.4 lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java:1.5 --- lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java:1.4 Fri Feb 22 15:24:02 2008 +++ lutincommandline/src/java/org/codelutin/option/SimpleConfigImpl.java Sat Mar 15 09:05:17 2008 @@ -29,9 +29,9 @@ * * @author chemit */ -public abstract class SimpleConfigImpl> extends Config { +public abstract class SimpleConfigImpl,O extends OptionKey> extends Config { - protected abstract T getParser(); + protected T parser; protected abstract O getConfigOptionKey(); @@ -39,16 +39,19 @@ protected abstract ConfigPropertyKey getFileNameConfigKey(); - protected SimpleConfigImpl(String category, String description) { + protected SimpleConfigImpl(T parser,String category, String description) { super(category, description); String home = System.getProperty("user.home"); if (home == null) { // this is a serious fatal error throw new RuntimeException("lutinutil.error.user.home"); } + setParser(parser); doInit(); } + + @Override public void save() throws IOException { // always save all configs in context in the same time @@ -135,4 +138,12 @@ protected File getConfigFile(Option option) { return option.getValuedArgumentValue(0, File.class, null, false); } + + public final T getParser() { + return parser; + } + + public void setParser(T parser) { + this.parser = parser; + } } Index: lutincommandline/src/java/org/codelutin/option/OptionKey.java diff -u lutincommandline/src/java/org/codelutin/option/OptionKey.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/OptionKey.java:1.2 --- lutincommandline/src/java/org/codelutin/option/OptionKey.java:1.1.1.1 Sat Feb 9 15:05:36 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionKey.java Sat Mar 15 09:05:17 2008 @@ -18,8 +18,9 @@ * ##% */ package org.codelutin.option; +import static org.codelutin.i18n.I18n._; import org.codelutin.option.def.OptionDefinition; -import org.codelutin.util.ArrayUtil;import static org.codelutin.i18n.I18n._; +import org.codelutin.util.ArrayUtil; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -39,11 +40,11 @@ * pour positionner la classe contrête d'OptionAction). *

* L'action liée est instanciée une seule fois, pour lancer une action utilisez - * la méthode {@link #doAction(OptionParser , Option[])}. + * la méthode {@link #doAction(AbstractContext , Option[])}. * * @author chemit */ -public class OptionKey> { +public class OptionKey, A extends OptionAction> { /** la clef non typée de l'option */ final protected String optionKey; @@ -124,25 +125,25 @@ /** * Launch action for the given parser and options * - * @param parser the used parser + * @param context the used context * @param options the options to fire * @throws Exception if any problem */ - public void doAction(P parser, O... options) throws Exception { + public void doAction(C context, O... options) throws Exception { if (options.length > 0) { - getAction(parser).doRun(options); + getAction(context.getParser()).doRun(context, options); } } /** * Launch action for the given parser * - * @param parser the used parser + * @param context the used context * @throws Exception if any problem */ - public void doAction(P parser) throws Exception { + public void doAction(C context) throws Exception { if (!options.isEmpty()) { - getAction(parser).doRun(ArrayUtil.toArray(options, optionClass)); + getAction(context.getParser()).doRun(context, ArrayUtil.toArray(options, optionClass)); } } @@ -168,7 +169,7 @@ throw new IllegalStateException("no concrete action found for " + abstractActionClass); } try { - if(actionConstructor==null) { + if (actionConstructor == null) { try { actionConstructor = actionClass.getConstructor(); } catch (NoSuchMethodException e) { @@ -209,6 +210,6 @@ @Override public String toString() { - return super.toString()+'<'+optionKey+'>'; + return super.toString() + '<' + optionKey + '>'; } } Index: lutincommandline/src/java/org/codelutin/option/OptionAction.java diff -u lutincommandline/src/java/org/codelutin/option/OptionAction.java:1.1.1.1 lutincommandline/src/java/org/codelutin/option/OptionAction.java:1.2 --- lutincommandline/src/java/org/codelutin/option/OptionAction.java:1.1.1.1 Sat Feb 9 15:05:36 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionAction.java Sat Mar 15 09:05:17 2008 @@ -3,67 +3,64 @@ /** * A simple empty action for an option. *

- * Use method {@link #doRun(Option[])} to launch action for the given options. + * Use method {@link #doRun(AbstractContext,Option[])} to launch action for the given options. *

* This method offers some hooks to use for controling running : *

- * {@link #beforeAll(Option[])} : no {@link #option} is set, before any doRun. + * {@link #beforeAll(AbstractContext,Option...)} : no {@link #option} is set, before any doRun. *

- * {@link #beforeRun()} : an {@link #option} has been set in action, just before {@link #run()} + * {@link #beforeRun(AbstractContext)} : an {@link #option} has been set in action, just before {@link #run(AbstractContext)} *

- * {@link #afterRun()} : just after {@link #run()}, the current {@link #option} is still available. + * {@link #afterRun(AbstractContext)} : just after {@link #run(AbstractContext)}, the current {@link #option} is still available. *

- * {@link #afterAll(Option[])} : no more {@link #option} is set, after all doRun. + * {@link #afterAll(AbstractContext,Option...)} : no more {@link #option} is set, after all doRun. * * @author chemit */ -public abstract class OptionAction { +public abstract class OptionAction> { /** * Method to implement logic of the action for the current option. *

* This method will never by directly called, you have to use - * {@link #doRun(Option[])} method instead which offers some hooks to control actions. + * {@link #doRun(AbstractContext,Option[])} method instead which offers some hooks to control actions. * + * @param context the used context * @throws Exception if any problem while action */ - protected abstract void run() throws Exception; + protected abstract void run(C context) throws Exception; - /** the parser used to obtain options */ - protected P parser; - /** the current option to be used for running. */ + /** + * the current option to be used for running. + */ protected O option; - protected OptionAction(P parser) { - this.parser = parser; - } - - public void doRun(O... options) throws Exception { - beforeAll(options); + public void doRun(C context, O... options) throws Exception { + beforeAll(context, options); for (O option : options) { this.option = option; - beforeRun(); - run(); - afterRun(); + beforeRun(context); + run(context); + afterRun(context); this.option = null; } - afterAll(options); + afterAll(context, options); } - protected void beforeAll(O... options) { + protected void beforeAll(C context, O... options) { } - protected void beforeRun() { + protected void beforeRun(C context) { } - protected void afterRun() { + protected void afterRun(C context) { } - protected void afterAll(O... options) throws Exception { + protected void afterAll(C context, O... options) throws Exception { } - protected P getParser() { + /*protected P getParser() { return parser; - } + }*/ } Index: lutincommandline/src/java/org/codelutin/option/OptionParser.java diff -u lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.4 lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.5 --- lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.4 Sat Mar 1 13:03:25 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionParser.java Sat Mar 15 09:05:17 2008 @@ -22,8 +22,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import static org.codelutin.i18n.I18n._; import org.codelutin.i18n.I18n; +import static org.codelutin.i18n.I18n._; import org.codelutin.option.OptionParserContexts.OptionContext; import org.codelutin.option.OptionParserContexts.ParserContext; import org.codelutin.option.def.OptionDefinition; @@ -150,15 +150,14 @@ } } - /** - * Initialize all configs, simply by instanciate them. - * - */ + /** Initialize all configs, simply by instanciate them. */ public void initConfigs() { for (ConfigKey configKey : configKeys) { + configKey.initConfig(this); configKey.getConfig(); } } + /** * Enregistre les implantations d'action à partir d'une classe contenant * des classes imbriquées d'implantation d'OptionAction. @@ -257,11 +256,12 @@ * Pour exécuter les actions de toutes les options trouvées pendant le parsing * en utilisant l'ordre de définition des options. * + * @param context used context * @throws Exception si un problème pendant les actions */ - public void doAllActions() throws Exception { + public void doAllActions(AbstractContext context) throws Exception { if (getNbOptions() > 0) { - doActions(optionKeys.toArray(new OptionKey[optionKeys.size()])); + doActions(context, optionKeys.toArray(new OptionKey[optionKeys.size()])); } } @@ -270,13 +270,14 @@ *

* L'ordre d'exécution est celui des clefs données. * - * @param keys les clefs des options à utiliser + * @param context used context + * @param keys les clefs des options à utiliser * @throws Exception si problème dans l'exécution des actions */ @SuppressWarnings({"unchecked"}) - public void doActions(OptionKey... keys) throws Exception { + public void doActions(AbstractContext context, OptionKey... keys) throws Exception { for (OptionKey key : keys) { - key.doAction(this, getOptions(key)); + key.doAction(context, getOptions(key)); } } @@ -286,7 +287,7 @@ * @param key la clef typée des options à rechercher * @return une table des options trouvées ou un tableau vide */ - public O[] getOptions(OptionKey key) { + public O[] getOptions(OptionKey key) { return ArrayUtil.toArray(key.getOptions(), key.getOptionClass()); } @@ -296,7 +297,7 @@ * @param key la clef typée de l'option à rechercher * @return la premier option trouvée ou null */ - public O getOption(OptionKey key) { + public O getOption(OptionKey key) { return !isOptionEnabled(key) ? null : getOptions(key)[0]; } @@ -437,7 +438,7 @@ * @return la clef typée ou null si non trouvée. */ @SuppressWarnings({"unchecked"}) - protected OptionKey getOptionKey(String key) { + protected OptionKey, ? extends OptionAction> getOptionKey(String key) { for (OptionKey optionKey : optionKeys) { if (optionKey.getOptionKey().equals(key)) { return optionKey; Index: lutincommandline/src/java/org/codelutin/option/AbstractContext.java diff -u /dev/null lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.1 --- /dev/null Sat Mar 15 09:05:23 2008 +++ lutincommandline/src/java/org/codelutin/option/AbstractContext.java Sat Mar 15 09:05:17 2008 @@ -0,0 +1,105 @@ +package org.codelutin.option; + + +import java.io.IOException; + +/** + * Definition d'un contexte applicatif, encapsulant le parser et les configurations de l'application. + * + * @author chemit + */ +public abstract class AbstractContext

{ + /** + * le parseur utilisé au démarrage pour récupérer les options passées + * par l'utilisateur. + */ + protected P parser; + /** + * flag pour indiquer si 'lon veut quitter l'appli après les options + */ + protected boolean quit; + /** + * flag pour indiquer si l'on affiche ou non l'ui principale + */ + protected boolean launchUI = true; + /** + * flag pour indiquer une première utilisation de l'application (pas de + * fichier de configuration) + */ + protected boolean firstLaunch; + + protected AbstractContext(String[] args) throws IOException, ParserException { + + // parse arguments + getParser().doParse(args); + + // init configs + getParser().initConfigs(); + + // init i18n with user locale (or default one if not found) + initI18n(); + + // mark if first launch + if (getConfig()!=null) { + setFirstLaunch(!getConfig().getSource().exists()); + + // save config + getConfig().save(); + } + + } + + /** + * initialisation i18n après init du parser et des configs. + */ + public abstract void initI18n(); + + /** + * @return le parseur utilisé pour parser les options de la ligne de + * commande. + */ + public abstract P getParser(); + + /** + * @return la configuration principale de l'application + */ + public abstract Config getConfig(); + + + /** + * @return true si on doit quitter l'application (uniquement utilisé + * lors du démarrage de l'application lors de l'exécution des options + * utilisateurs) + */ + public boolean isQuit() { + return quit; + } + + /** + * @return true si on doit lancer l'ui au démarrage de + * l'application, après avoir traité toutes les options utilisateurs + */ + public boolean isLaunchUI() { + return launchUI; + } + + /** + * @return true s'il s'agit de la première utilisation de + * l'application (aucun de fichier de configuration trouvé) + */ + public boolean isFirstLaunch() { + return firstLaunch; + } + + public void setQuit(boolean quit) { + this.quit = quit; + } + + public void setLaunchUI(boolean launchUI) { + this.launchUI = launchUI; + } + + public void setFirstLaunch(boolean firstLaunch) { + this.firstLaunch = firstLaunch; + } +} \ No newline at end of file