Index: lutinutil/src/java/org/codelutin/util/OptionDefinition.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/OptionDefinition.java:1.1 --- /dev/null Mon Nov 19 19:37:00 2007 +++ lutinutil/src/java/org/codelutin/util/OptionDefinition.java Mon Nov 19 19:36:54 2007 @@ -0,0 +1,252 @@ +/** + * ##% Copyright (C) 2002, 2007 Code Lutin 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 static org.codelutin.i18n.I18n._; + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Classe qui représente la définition d'une option. + *

+ * Une option est définition par : + *

+ * + * @author chemit + * @see OptionArgumentType + * @see OptionArgumentValueType + * @see OptionArgumentDefinition + */ +public class OptionDefinition {// OptionDefinition + + /** Le nom de cette option */ + protected String name; + + /** Description of the argument */ + protected String description; + + /** la définition normalisée de l'option */ + protected String definition; + + /** Tous les alias acceptés pour cette option : -o --option,... */ + protected String[] alias; + /** + * le nombre de fois minimum que l'option peut-être répétée sur la ligne de + * commande : par défaut 0 (option non obligatoire) + */ + protected int min; + + /** + * le nombre de fois maximum que l'option peut-être répétée sur la ligne de + * commande: 1 par defaut (-1) pour pas de max + */ + protected int max; + + /** la liste de tous les argument possibles */ + protected OptionArgumentDefinition[] arguments; + + /** + * Pour construire une définition d'option en connaissant à l'avance + * la définition de tous ses arguments. + * + * @param name le nom de l'option + * @param description la description de l'option + * @param definition la représentation normalisée de l'option + * @param min le nombre d'occurrences minimum à respecter + * @param max le nombre maximum d'occurrences à respecter + * @param alias les alias de l'option + * @param arguments les definitions d'arguments possibles pour l'option + */ + public OptionDefinition(String name, String description, String definition, + int min, int max, String[] alias, + OptionArgumentDefinition[] arguments) { + this.name = name; + this.description = description.trim(); + this.definition = definition.trim(); + this.min = min; + this.max = max; + this.alias = alias; + this.arguments = arguments; + } + + /** + * Pour construire une définition d'option en connaissant uniquement le + * nombre de ses arguments. + * + * @param name le nom de l'option + * @param description la description de l'option + * @param definition la représentation normalisée de l'option + * @param min le nombre d'occurrences minimum à respecter + * @param max le nombre maximum d'occurrences à respecter + * @param nbArguments le nombre d'arguments possibles de l'option + * @param alias les alias de l'option + */ + public OptionDefinition(String name, String description, String definition, + int min, int max, int nbArguments, String[] alias) { + this.name = name; + this.description = description == null ? null : description.trim(); + this.definition = definition; + this.min = min; + this.max = max; + this.alias = alias; + this.arguments = new OptionArgumentDefinition[nbArguments]; + } + + public String getDescription() { + return description; + } + + public int getMin() { + return this.min; + } + + public int getMax() { + return this.max; + } + + public String getName() { + return name; + } + + public String[] getAlias() { + return alias; + } + + public String getDefinition() { + return definition; + } + + public OptionArgumentDefinition[] getArguments() { + return arguments == null ? new OptionArgumentDefinition[0] : arguments; + } + + public void printUsage(Writer writer) throws IOException { + writer.append(definition).append("\n ").append(description).append("\n\n"); + } + + public void printDetail(Writer writer) throws IOException { + + writer.append(_("lutinutil.parserdef.printDetail.head", + definition, description, + arguments == null ? 0 : arguments.length, getNbGroups())); + + if (arguments != null) { + List listGroup = new ArrayList(); + int currentGroup = -2; + boolean mandatoryGroup = false; + for (int i = 0, j = arguments.length; i < j; i++) { + OptionArgumentDefinition argument = arguments[i]; + + if (currentGroup == -2 || argument.getPosition() != currentGroup) { + // new group found + + // save old group (if not empty only) + printGroupDetail(writer, listGroup, currentGroup, mandatoryGroup); + + currentGroup = argument.getPosition(); + mandatoryGroup = currentGroup > -1; + } + listGroup.add(_("lutinutil.parserdef.printDetail.argument.head", i + 1, j, argument)); + } + + // save old group (if not empty only) + printGroupDetail(writer, listGroup, currentGroup, mandatoryGroup); + + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + StringBuffer sb = new StringBuffer(name); + sb.append(' ').append(Arrays.toString(alias)); + StringUtil.printCardinalite(builder, sb.toString(), min, max, min > 0); + return builder.toString(); + } + + @Override + public boolean equals(Object o) { + return this == o || !(o == null || getClass() != o.getClass()) && + name.equals(((OptionDefinition) o).name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + if (arguments != null) { + for (int i = 0; i < arguments.length; i++) { + arguments[i] = null; + } + arguments = null; + } + if (alias != null) { + for (int i = 0; i < alias.length; i++) { + alias[i] = null; + } + alias = null; + } + } + + protected int getNbGroups() { + if (arguments == null) { + return 0; + } + List list = new ArrayList(); + for (OptionArgumentDefinition argument : arguments) { + if (!list.contains(argument.getPosition())) { + list.add(argument.getPosition()); + } + } + int result = list.size(); + list.clear(); + return result; + } + + private void printGroupDetail(Writer writer, List list, + int currentGroup, boolean mandatoryGroup) + throws IOException { + if (list.isEmpty()) { + // nothing to do here + return; + } + String txt; + if (mandatoryGroup) { + txt = _("lutinutil.parserdef.printDetail.group.mandatory",currentGroup, list.size()); + } else { + txt = _("lutinutil.parserdef.printDetail.group.optional",list.size()); + } + writer.append(txt); + for (String s : list) { + writer.append(s); + } + list.clear(); + } + +}// OptionDefinition