Index: lutinutil/src/java/org/codelutin/util/OptionDefinitionFactory.java diff -u lutinutil/src/java/org/codelutin/util/OptionDefinitionFactory.java:1.1 lutinutil/src/java/org/codelutin/util/OptionDefinitionFactory.java:1.2 --- lutinutil/src/java/org/codelutin/util/OptionDefinitionFactory.java:1.1 Mon Nov 19 19:53:30 2007 +++ lutinutil/src/java/org/codelutin/util/OptionDefinitionFactory.java Wed Nov 28 01:23:13 2007 @@ -1,25 +1,37 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* 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.util; -import org.apache.commons.collections.map.ReferenceMap; -import static org.codelutin.i18n.I18n._; -import static org.codelutin.util.OptionDefinitionParser.LUTINUTIL_PARSERDEF_PRINT_DETAIL_OPTION_HEAD; - -import java.io.IOException; -import java.io.Writer; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.Map; /** - * Factory de définitions d'options. + * Usine de définitions d'options. *

- * Cette classe servira de super classe à la Factory generee par le parseur - * de définition, la genetation consiste a implanter la methode init pour - * initialiser la map d'option. + * Cette classe servira de super classe à l'usine générée par le plugin + * maven-commandline-plugin:generate. + * l'implantation consiste a implanter la methode init pour initialiser + * la map d'options et à ajouter comme constantes les clefs d'options utilisées. *

- * Elle contient une méthode pour récupérer une factory initialisée à partir - * de sa classe {@link #newFactory(Class)}. + * Elle contient une méthode publique statique pour récupérer une factory + * initialisée à partir de sa classe {@link #newFactory(String, ClassLoader)}. * * @author chemit */ @@ -27,78 +39,47 @@ public abstract class OptionDefinitionFactory { /** - * @param clazz class of the factory to get + * @param className fully qualified name of the factory class + * @param loader the class loader to use to find class * @return an instance of the required factory, initialize. - * @throws InstantiationException if error while instanciate factory - * @throws IllegalAccessException if can not access class */ - public static OptionDefinitionFactory newFactory( - Class clazz) - throws InstantiationException, IllegalAccessException { - - // instanciate the factory - OptionDefinitionFactory factor = clazz.newInstance(); - - // init factory (this is the generated method) - factor.init(); - - return factor; + public static OptionDefinitionFactory newFactory(String className, + ClassLoader loader) { + try { + // get factory class + Class clazz = Class.forName(className, true, loader); + // instanciate the factory + OptionDefinitionFactory factor = (OptionDefinitionFactory) clazz.newInstance(); + // init factory (this is the generated method) + factor.init(); + // return factory + return factor; + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } catch (InstantiationException e) { + throw new IllegalArgumentException(e); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException(e); + } } /** * La methode a implanter pour initialiser la factory. *

- * Il s'agit de remplir {@link #options} avec des {@link OptionDefinition} + * Il s'agit de remplir {@link #definitions} avec des + * {@link OptionDefinition}. */ public abstract void init(); - /** les options enregistrees dans la factory indexees par leur nom unique */ - protected final Map options; - - /** les clefs des options connues par la factory (Instanciation paresseuse). */ - protected List keys; + /** les options enregistrees dans la factory */ + protected final List definitions; - @SuppressWarnings("unchecked") public OptionDefinitionFactory() { - options = (Map) new ReferenceMap(); - } - - public OptionDefinition get(String key) { - return options.get(key); + definitions = new ArrayList(); } - public List getKeys() { - if (keys == null) { - ArrayList list = new ArrayList(options.keySet()); - Collections.sort(list); - keys = Collections.unmodifiableList(list); - } - return keys; - } - - public OptionDefinition[] getOptions() { - return options.values().toArray(new OptionDefinition[options.size()]); - } - - public void printUsage(Writer writer, String name) throws IOException { - writer.append(_("lutinutil.parserdef.printUsage.factory.head",name)); - if (!getKeys().isEmpty()) { - for (String key : keys) { - get(key).printUsage(writer); - } - } - } - - public void printDetails(Writer writer) throws IOException { - int size = getKeys().size(); - writer.append(_("lutinutil.parserdef.printDetail.factory.head", - this, size)); - for (int i = 0; i < size; i++) { - OptionDefinition definition = options.get(getKeys().get(i)); - writer.append(_(LUTINUTIL_PARSERDEF_PRINT_DETAIL_OPTION_HEAD, - i + 1, size, definition)); - definition.printDetail(writer); - } + public OptionDefinition[] getDefinitions() { + return definitions.toArray(new OptionDefinition[definitions.size()]); } protected OptionDefinition addOptionDefinition(String name, @@ -152,7 +133,7 @@ OptionDefinition result; result = new OptionDefinition(name, description, definition, min, max, nbArguments, alias); - options.put(name, result); + definitions.add(result); return result; } @@ -175,6 +156,6 @@ @Override protected void finalize() throws Throwable { super.finalize(); - options.clear(); + definitions.clear(); } }