Index: lutincommandline/src/java/org/codelutin/option/ConfigKey.java diff -u lutincommandline/src/java/org/codelutin/option/ConfigKey.java:1.4 lutincommandline/src/java/org/codelutin/option/ConfigKey.java:1.5 --- lutincommandline/src/java/org/codelutin/option/ConfigKey.java:1.4 Mon Mar 17 22:39:33 2008 +++ lutincommandline/src/java/org/codelutin/option/ConfigKey.java Wed Mar 19 20:21:11 2008 @@ -58,7 +58,7 @@ * * @author chemit */ -public class ConfigKey { +public class ConfigKey implements ContextVisitable { /** * Public factory of ConfigKey @@ -124,4 +124,8 @@ throw new RuntimeException(e); } } + + public void accept(ContextVisitor visitor) { + // no more thing to visit + } } \ No newline at end of file Index: lutincommandline/src/java/org/codelutin/option/OptionParserResult.java diff -u lutincommandline/src/java/org/codelutin/option/OptionParserResult.java:1.1 lutincommandline/src/java/org/codelutin/option/OptionParserResult.java:1.2 --- lutincommandline/src/java/org/codelutin/option/OptionParserResult.java:1.1 Sun Mar 16 21:15:58 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionParserResult.java Wed Mar 19 20:21:11 2008 @@ -1,3 +1,16 @@ +/** + * ##% Copyright (C) 2008 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; import java.util.ArrayList; @@ -114,15 +127,16 @@ return error; } + public void setError(ParserFailedException e) { + error = e; + } + @Override public String toString() { return super.toString() + " arguments:" + Arrays.toString(arguments) + ", nbOptions : " + size(); } - public void setError(ParserFailedException e) { - error = e; - } - + @Override protected void finalize() throws Throwable { super.finalize(); clear(); @@ -172,17 +186,16 @@ public void doActions(AbstractContext context, OptionKey... keys) throws Exception { for (OptionKey key : keys) { - if (!isOptionEnabled(key)) { - continue; - } + if (isOptionEnabled(key)) { - Option[] options = getOptions(key); + Option[] options = getOptions(key); - OptionAction action = key.getAction(context.getParser()); + OptionAction action = key.getAction(context.getParser()); + + action.doRun(context, options); + } - action.doRun(context, options); } } - } Index: lutincommandline/src/java/org/codelutin/option/AbstractContext.java diff -u lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.5 lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.6 --- lutincommandline/src/java/org/codelutin/option/AbstractContext.java:1.5 Tue Mar 18 23:40:47 2008 +++ lutincommandline/src/java/org/codelutin/option/AbstractContext.java Wed Mar 19 20:21:12 2008 @@ -33,7 +33,7 @@ * * @author chemit */ -public abstract class AbstractContext

{ +public abstract class AbstractContext

implements ContextVisitable { /** logger non statique pour épouser la catégorie de l'implantation */ protected final Log log = LogFactory.getLog(getClass()); @@ -220,6 +220,7 @@ * @param key la clef de la configuration recherchée * @return l'instance de la configuration */ + @SuppressWarnings({"unchecked"}) public C getConfig(ConfigKey key) { int index = configKeys.indexOf(key); return (C) configs[index]; @@ -267,12 +268,9 @@ } protected void loadFromOptions() throws Exception { - - if (!getParser().getLastResult().isOptionEnabled(getMainConfig().getConfigOptionKey())) { - return; + if (getParser().getLastResult().isOptionEnabled(getMainConfig().getConfigOptionKey())) { + parser.getLastResult().doActions(this, getMainConfig().getConfigOptionKey()); } - - parser.getLastResult().doActions(this, getMainConfig().getConfigOptionKey()); } /** @@ -382,4 +380,10 @@ } } + public void accept(ContextVisitor visitor) { + visitor.enterParser(getParser()); + for (ConfigKey configKey : configKeys) { + visitor.enterConfig(configKey); + } + } } \ No newline at end of file Index: lutincommandline/src/java/org/codelutin/option/OptionActionRunnable.java diff -u lutincommandline/src/java/org/codelutin/option/OptionActionRunnable.java:1.2 lutincommandline/src/java/org/codelutin/option/OptionActionRunnable.java:1.3 --- lutincommandline/src/java/org/codelutin/option/OptionActionRunnable.java:1.2 Mon Mar 17 22:39:33 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionActionRunnable.java Wed Mar 19 20:21:12 2008 @@ -1,7 +1,7 @@ package org.codelutin.option; /** - * Code wrapper for DefaultOption + * Action wrapper for OptionAction * * @author chemit */ public interface OptionActionRunnable, A extends OptionAction> { Index: lutincommandline/src/java/org/codelutin/option/OptionKey.java diff -u lutincommandline/src/java/org/codelutin/option/OptionKey.java:1.3 lutincommandline/src/java/org/codelutin/option/OptionKey.java:1.4 --- lutincommandline/src/java/org/codelutin/option/OptionKey.java:1.3 Sun Mar 16 21:15:58 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionKey.java Wed Mar 19 20:21:12 2008 @@ -41,7 +41,7 @@ * * @author chemit */ -public class OptionKey, A extends OptionAction> { +public class OptionKey, A extends OptionAction> implements ContextVisitable { /** la clef non typée de l'option */ final protected String optionKey; @@ -166,4 +166,8 @@ public String toString() { return super.toString() + '<' + optionKey + '>'; } + + public void accept(ContextVisitor visitor) { + // no more thing to visit + } } Index: lutincommandline/src/java/org/codelutin/option/ParserUtil.java diff -u lutincommandline/src/java/org/codelutin/option/ParserUtil.java:1.4 lutincommandline/src/java/org/codelutin/option/ParserUtil.java:1.5 --- lutincommandline/src/java/org/codelutin/option/ParserUtil.java:1.4 Wed Mar 19 18:53:37 2008 +++ lutincommandline/src/java/org/codelutin/option/ParserUtil.java Wed Mar 19 20:21:12 2008 @@ -20,11 +20,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.codelutin.option.def.OptionDefinition; -import org.codelutin.util.ReflectUtil; import java.io.IOException; -import java.io.Writer; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; @@ -37,7 +34,7 @@ public class ParserUtil { - protected static String addTitle(String txt, char c, boolean headAndTail) { + public static String addTitle(String txt, char c, boolean headAndTail) { StringBuilder writer = new StringBuilder(); if (headAndTail) { for (int i = 0, j = txt.length(); i < j; i++) { @@ -52,41 +49,10 @@ return writer.toString(); } - public static void toString(Class parserClass, Writer w, String head, String prefix, String prefixOption, String prefixConfig) throws IOException { - try { - w.append(addTitle(head, '=', true)).append("\n\n"); - w.append(addTitle(prefixOption, '-', false)).append("\n"); - for (OptionKey key : ReflectUtil.getConstants(parserClass, OptionKey.class)) { - OptionDefinition definition = key.getDefinition(); - w.append(prefix).append(definition.toString()); - w.append("\n ").append(key.getDescription()).append("\n\n"); - } - List configKeys = ReflectUtil.getConstants(parserClass, ConfigKey.class); - if (configKeys.isEmpty()) { - return; - } - w.append(addTitle(prefixConfig, '-', false)).append("\n"); - for (ConfigKey key : configKeys) { - w.append("\n").append(addTitle(key.getDescription() + " (" + key.getCategory() + ")", '~', false)).append("\n"); - for (ConfigPropertyKey propertyKey : ReflectUtil.getConstants(key.getAbstractConfigClass(), ConfigPropertyKey.class)) { - w.append(prefix).append(propertyKey.getKey()).append(" (").append(propertyKey.getType().getSimpleName()).append(")"); - if (propertyKey.getDefaultValue()!=null) { - w.append(" "); - - } - w.append("\n ").append(propertyKey.getDescription()).append("\n\n"); - } - } - } finally { - if (w != null) { - w.flush(); - } - } - } - /** * Un contexte abstrait pour parseur (qui peut contenir un contexte parent * et des contextes enfants) et qui gère les erreurs. + * * @author chemit */ @@ -94,13 +60,13 @@ protected abstract E newError(String content, Exception e); - /** logger non statique pour �tre dans la bonne cat�gorie */ + /** logger non statique pour etre dans la bonne categorie */ protected final Log log = LogFactory.getLog(getClass()); - /** le contexte parent (peut �tre null) */ + /** le contexte parent (peut etre null) */ protected P parent; - /** la liste des contextes enfants (peut �tre null) */ + /** la liste des contextes enfants (peut etre null) */ protected final List contexts; /** flag pour marquer l'�tat du parser */ Index: lutincommandline/src/java/org/codelutin/option/OptionParser.java diff -u lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.8 lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.9 --- lutincommandline/src/java/org/codelutin/option/OptionParser.java:1.8 Mon Mar 17 22:39:33 2008 +++ lutincommandline/src/java/org/codelutin/option/OptionParser.java Wed Mar 19 20:21:12 2008 @@ -23,14 +23,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.i18n.I18n; -import static org.codelutin.i18n.I18n._; import org.codelutin.option.OptionParserContexts.ParserContext; import org.codelutin.option.def.OptionDefinition; import org.codelutin.option.def.OptionDefinitionBuilder; import org.codelutin.util.ReflectUtil; -import java.io.IOException; -import java.io.Writer; import java.lang.reflect.Method; import java.util.Collections; import java.util.List; @@ -54,7 +51,7 @@ * pas des inner-classes. */ -public abstract class OptionParser { +public abstract class OptionParser implements ContextVisitable { /** logger non statique pour épouser la catégorie de l'implantation */ protected final Log log = LogFactory.getLog(getClass()); @@ -154,21 +151,6 @@ } - /** - * Print usage of the parser - * - * @param w writer - * @param name title - * @throws IOException if any io pb while writing - */ - public void printUsage(Writer w, String name) throws IOException { - String head = _("lutinutil.parserdef.printUsage.head", name); - String prefixOption = _("lutinutil.parserdef.printUsage.options.head", name); - String prefixConfig = _("lutinutil.parserdef.printUsage.configs.head", name); - String prefix = "\n"; - ParserUtil.toString(getClass(), w, head, prefix, prefixOption, prefixConfig); - } - public OptionParserResult getLastResult() { return lastResult; } @@ -213,6 +195,13 @@ } } } + + public void accept(ContextVisitor visitor) { + for (OptionKey optionKey : optionKeys) { + visitor.visitOptionKey(optionKey); + } + visitor.exitParser(this); + } } Index: lutincommandline/src/java/org/codelutin/option/ContextVisitable.java diff -u /dev/null lutincommandline/src/java/org/codelutin/option/ContextVisitable.java:1.1 --- /dev/null Wed Mar 19 20:21:17 2008 +++ lutincommandline/src/java/org/codelutin/option/ContextVisitable.java Wed Mar 19 20:21:12 2008 @@ -0,0 +1,30 @@ +/** + * ##% Copyright (C) 2008 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; + +/** + * The context visitable contract + * + * @author chemit + */ +public interface ContextVisitable { + + /** + * method to implement for each visitable to indicate what to visit + * + * @param visitor le visiteur + */ + void accept(ContextVisitor visitor); +} \ No newline at end of file Index: lutincommandline/src/java/org/codelutin/option/ContextVisitor.java diff -u /dev/null lutincommandline/src/java/org/codelutin/option/ContextVisitor.java:1.1 --- /dev/null Wed Mar 19 20:21:17 2008 +++ lutincommandline/src/java/org/codelutin/option/ContextVisitor.java Wed Mar 19 20:21:12 2008 @@ -0,0 +1,39 @@ +/** + * ##% Copyright (C) 2008 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; + +/** + * The context visitor contract + * + * @author chemit + */ +public interface ContextVisitor { + + void enterContext(ContextVisitable visitable); + + void enterConfig(ContextVisitable visitable); + + void visitConfigKey(ContextVisitable visitable); + + void exitConfig(ContextVisitable visitable); + + void enterParser(ContextVisitable visitable); + + void visitOptionKey(ContextVisitable visitable); + + void exitParser(ContextVisitable visitable); + + void exitContext(ContextVisitable visitable); +}