Author: tchemit Date: 2009-05-18 10:51:45 +0000 (Mon, 18 May 2009) New Revision: 1599 Removed: nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParser.java nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserDefaultOption.java nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserOption.java Modified: nuitonutil/trunk/pom.xml Log: use mavempom snapshot remove deprecated poms Modified: nuitonutil/trunk/pom.xml =================================================================== --- nuitonutil/trunk/pom.xml 2009-05-18 10:36:02 UTC (rev 1598) +++ nuitonutil/trunk/pom.xml 2009-05-18 10:51:45 UTC (rev 1599) @@ -10,7 +10,7 @@ <parent> <groupId>org.nuiton</groupId> <artifactId>mavenpom</artifactId> - <version>1.0.0-alpha-2</version> + <version>1.0.0-alpha-3-SNAPSHOT</version> </parent> <artifactId>nuiton-utils</artifactId> Deleted: nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParser.java =================================================================== --- nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParser.java 2009-05-18 10:36:02 UTC (rev 1598) +++ nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParser.java 2009-05-18 10:51:45 UTC (rev 1599) @@ -1,393 +0,0 @@ -/* *##% Lutin utilities library - * Copyright (C) 2004 - 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ - -/* * -* ArgumentsParser.java -* -* Created: 22 août 2003 -* -* @author Benjamin Poussin <poussin@codelutin.com> -* @version $Revision$ -* -* Mise a jour: $Date$ -* par : $Author$ -*/ - -package org.nuiton.util; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; - -/** - * Parser d'option de la ligne de commande. - * IDEA: ajouter le type de l'argument a parser (int, double, String, boolean, Class, File, ...) - * et mettre des methodes pour récupérer directement ce type. - * - * @author poussin - * - * @deprecated since 0.30 , prefer use of {@link ApplicationConfig} instead - */ -@Deprecated -public class ArgumentsParser { - - /** La liste des arguments qui ne sont pas des options */ - protected ArrayList<String> arguments = new ArrayList<String>(); - /** le nom à afficher pour l'usage */ - protected String name = ""; - - /** - * la liste des options, la cle est une option, et la valeur est la liste - * des options qui ont été lu grâce à celle-ci. - */ - protected HashMap<ArgumentsParserOption, ArrayList<ArgumentsParserOption>> options = - new HashMap<ArgumentsParserOption, ArrayList<ArgumentsParserOption>>(); - /** liste a plat de toutes les options lus. */ - protected ArrayList<ArgumentsParserOption> parsedOptions = new ArrayList<ArgumentsParserOption>(); - - /** - * Constructor for the ArgumentsParser object - * - * @param name le nom à afficher pour l'usage - */ - public ArgumentsParser(String name) { - this.name = name; - } - - /** - * Permet de d'ajouter une option facilement au parser, sans avoir besoin de - * créer un objet Option. - * - * @param optionName le nom de cette option. - * @param acceptedOption la liste des marqueurs acceptables pour cette - * option. exemple -o, --option, --options - * @param argument le nombre d'argument que cette option a - * @param description description de cette option - * @return retourne l'option créer et qui sera utilisé - * @see #addOption(ArgumentsParserOption) - */ - public ArgumentsParserOption addOption(String optionName, - String description, int argument, String... acceptedOption) { - return addOption(new ArgumentsParserDefaultOption - (optionName, description, argument, acceptedOption)); - } - - /** - * Permet d'ajouter une option au parseur - * - * @param option l'option à ajouter - * @return retourne l'option passé en paramètre - */ - public ArgumentsParserOption addOption(ArgumentsParserOption option) { - options.put(option, new ArrayList<ArgumentsParserOption>()); - return option; - } - - /** - * Permet de vérifier la coherence de toutes les options. Cela consiste à - * regarder si deux options n'utilise pas les même marqueurs, si deux - * options n'ont pas le même nom, si le nombre de répétition min d'une - * option n'est pas suppérieur au nombre de répétition max - * - * @return une chaine de caractère indiquant toutes les erreurs. Si la - * chaine est vide alors il n'y a pas d'erreur. - */ - public String checkCoherence() { - StringBuffer result = new StringBuffer(); - HashSet<String> optionNames = new HashSet<String>(); - HashSet<String> used = new HashSet<String>(); - - for (ArgumentsParserOption option : options.keySet()) { - if (optionNames.contains(option.getName())) { - result.append("option name " + option.getName() - + " is allready used\n"); - } - optionNames.add(option.getName()); - - String[] acceptedOptions = option.getAcceptedOption(); - for (String acceptedOption : acceptedOptions) { - if (used.contains(acceptedOption)) { - result.append("Option " + acceptedOption + " in " - + option.getName() + " is allready used\n"); - } - used.add(acceptedOption); - } - - if (option.getRepetitionMax() > 0 - && option.getRepetitionMin() > option.getRepetitionMax()) { - result.append("repetition min is over repetition max for: " - + option.getName() + "\n"); - option.setRepetitionMax(option.getRepetitionMin()); - } - } - return result.toString(); - } - - /** - * Permet de vérifier la cohérence de la ligne de commande par rapport aux - * contraintes des options. Cela consiste à vérifier que chaque option est - * bien répété le nmobre de fois souhaité par l'option qui à permis de la - * lire. - * - * @return vrai s'il n'y a pas d'erreur - * @throws ArgumentsParserException une exception est levé dès qu'une - * erreur est détectée. - */ - boolean checkParsing() throws ArgumentsParserException { - for (ArgumentsParserOption option : options.keySet()) { - ArrayList<ArgumentsParserOption> list = options.get(option); - if (option.getRepetitionMin() > 0 - && list.size() < option.getRepetitionMin()) { - throw new ArgumentsParserException( - "Error option repeted less than accepted need " - + option.getRepetitionMin() + " find " + list.size() + ": " - + option); - } - if (option.getRepetitionMax() > 0 - && list.size() > option.getRepetitionMax()) { - throw new ArgumentsParserException( - "Error option repeted more than accepted: " + option); - } - } - return true; - } - - /** - * Retourne les arguments de la ligne de commande qui n'ont pas été lu pas - * les options - * - * @return la liste des arguments non lu par les options. - */ - public ArrayList<String> getArguments() { - return arguments; - } - - /** - * Retourne le nom utilisé pour l'affichage de l'usage - * - * @return la chaine de caratère utilisée pour l'usage - */ - public String getName() { - return name; - } - - /** - * Permet de savoir si une option a ete trouvée sur la ligne de commande - * - * @param optionName le nom de l'option dont il faut vérifier l'existance - * @return vrai si l'option existe sur la ligne de commande - */ - public boolean hasParsedOption(String optionName) { - return getParsedOptions(optionName).size() != 0; - } - - /** - * Retourne le premier argument d'un certain type - * - * @param optionName le nom de l'option à retourner - * @return la valeur de l'option si elle existe, null sinon - */ - public String[] getParsedOption(String optionName) { - ArrayList<ArgumentsParserOption> list = getParsedOptions(optionName); - if (list.size() == 0) { - return null; - } else { - return list.get(0).getArguments(); - } - } - - /** - * Retourne la liste de toutes les options créées durant le parsage de la - * ligne de commande. - * - * @return la liste de toutes les options lus - */ - public ArrayList<ArgumentsParserOption> getParsedOptions() { - return parsedOptions; - } - - /** - * Retourne la liste de toutes les options d'un certain type - * - * @param optionName le nom des options à retourner - * @return la liste des options d'un certain nom, ou une liste vide si - * aucune option de ce nom existe ou si cette option n'a pas été trouvé - * sur la ligne de commande - */ - public ArrayList<ArgumentsParserOption> getParsedOptions(String optionName) { - ArrayList<ArgumentsParserOption> result = new ArrayList<ArgumentsParserOption>(); - for (ArgumentsParserOption option : options.keySet()) { - if (optionName.equals(option.getName())) { - result = options.get(option); - } - } - return result; - } - - /** - * Permet de recherche une option permettant de lire l'argument courant de - * la ligne de commande et d'utiliser une copie de cette option pour lire la - * suite de la ligne de commande. - * - * @param args la ligne de commande - * @param index la position courant de parsage de la ligne de commande - * @return retourne l'option qui a permit de lire la ligne de commande - * ou null si aucune option ne peut lire l'argument courant. - */ - ArgumentsParserOption lookingForOption(String[] args, int index) { - for (ArgumentsParserOption option : options.keySet()) { - ArgumentsParserOption copy = option.copy(); - if (copy.parse(args, index)) { - ArrayList<ArgumentsParserOption> list = options.get(option); - parsedOptions.add(copy); - list.add(copy); - return copy; - } - } - return null; - } - - - /** - * Une methode main pour tester la librairie - * - * @param args The command line arguments - * @throws Exception on fait suivre toutes les erreurs - */ - public static void main(String[] args) throws Exception { - ArgumentsParser ap = new ArgumentsParser("ArgumentsParserTest"); - ap.addOption("project", "le nom du projet", 1, "-p") - .setRepetitionMin(1); - - ap.addOption("lib", "les librairies", 1, "-l") - .setRepetitionMin(2).setRepetitionMax(1); - - ap.addOption("truc", "les dummy", 1, "-t"); - - ap.addOption("package", "le package utilisé", - 1, "-k", "--package").setRepetitionMax(10); - - System.out.println(ap.usage()); - System.out.println(); - System.out.println("check coherence..."); - System.out.println(ap.checkCoherence()); - System.out.println("parsing..."); - ap.parse(args); - System.out.println(ap.getParsedOptions()); - System.out.println(ap.getParsedOptions("package")); - System.out.println(ap.getArguments()); - } - - /** - * Cette méthode permet de parser une ligne de commande. Aucune - * réinitialisation n'est faite entre deux appels. - * - * @param args la ligne de commande à parser - * @return la liste de toutes les options lus - * @throws ArgumentsParserException si la vérification de la lecture - * montre des incohérence une exception est levée - */ - public ArgumentsParserOption[] parse(String[] args) throws ArgumentsParserException { - for (int i = 0; i < args.length; i++) { - ArgumentsParserOption option = lookingForOption(args, i); - if (option != null) { - i += option.count(); - } else { - arguments.add(args[i]); - } - } - - checkParsing(); - - return (ArgumentsParserOption[]) parsedOptions.toArray( - new ArgumentsParserOption[parsedOptions.size()]); - } - - /** - * <p> - * <p/> - * Permet d'afficher l'usage gràce aux informations des options: répétition, - * description, ...</p> <p> - * <p/> - * la chaîne retourné est de la forme suivante</p> <pre> - * usage: ArgumentsParserTest [package{10}][truc+] lib project+ - * <p/> - * package(-k, --package) le package utilisé - * truc(-t) les dummy - * lib(-l) les librairies - * project(-p) le nom du projet - * </pre> <p> - * <p/> - * ArgumentsParserTest est le nom passé au constructeur de ArgumentsParser. - * </p> <p> - * <p/> - * une option qui a une répétition min à 0 est entre [ ]</p> <p> - * <p/> - * une option qui a une répétition max à 0 à un + ajouté</p> <p> - * <p/> - * une option qui a une répétition max à supérieur à 1 à le nombre max - * ajouté entre {}</p> - * - * @return la chaîne de caratère de l'usage. - */ - public String usage() { - StringBuffer result = new StringBuffer(); - result.append("usage: " + getName() + " "); - for (ArgumentsParserOption option : options.keySet()) { - if (option.getRepetitionMin() <= 0) { - result.append("["); - } else { - result.append(" "); - } - result.append(option.getName()); - if (option.getRepetitionMax() <= 0) { - result.append("+"); - } else if (option.getRepetitionMax() > 1) { - result.append("{" + option.getRepetitionMax() + "}"); - } - - for (int a = 0; a < option.count(); a++) { - result.append(" <arg" + a + ">"); - } - - if (option.getRepetitionMin() <= 0) { - result.append("]"); - } else { - result.append(" "); - } - } - result.append("\n\n"); - for (ArgumentsParserOption option : options.keySet()) { - result.append(option.getName()); - result.append("("); - String[] acceptedOption = option.getAcceptedOption(); - for (int i = 0; i < acceptedOption.length; i++) { - result.append(acceptedOption[i]); - if (i + 1 < acceptedOption.length) { - result.append(", "); - } - } - result.append(")"); - result.append("\t"); - result.append(option.getDescription()); - result.append("\n"); - } - return result.toString(); - } -} -// -- end class ArgumentsParser - Deleted: nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserDefaultOption.java =================================================================== --- nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserDefaultOption.java 2009-05-18 10:36:02 UTC (rev 1598) +++ nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserDefaultOption.java 2009-05-18 10:51:45 UTC (rev 1599) @@ -1,222 +0,0 @@ -/* *##% Lutin utilities library - * Copyright (C) 2004 - 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ - -/** - * ArgumentsParserDefaultOption.java Created: 22 août 2003 - * - *@author Benjamin Poussin <poussin@codelutin.com> - * - * Copyright Code Lutin - *@version $Revision$ Mise a jour: $Date$ par : $Author$ - */ - -package org.nuiton.util; - -import java.util.ArrayList; - -/** - * Description of the Class - * - * @author poussin - * created 22 août 2003 - * - * @deprecated since 0.30 , prefer use of {@link ApplicationConfig} instead - */ -public class ArgumentsParserDefaultOption implements ArgumentsParserOption {// ArgumentsParserDefaultOption - - /** Toutes les options accepté: -o --option */ - String[] acceptedOption; - /** L'option utilisé sur la ligne de commande */ - String parsedOption; - /** Le nom de cette option */ - String name; - /** le nombre d'argument de cette option */ - int argument = 0; - /** la liste des arguments */ - ArrayList<String> argumentList = new ArrayList<String>(); - /** - * le nombre de fois minimum que l'option peut-être répétée sur la ligne de - * commande - */ - int repetitionMin = 0; - /** - * le nombre de fois maximum que l'option peut-être répétée sur la ligne de - * commande: 1 par defaut - */ - int repetitionMax = 1; - /** Description of the argument */ - String description = ""; - - /** - * @param name le nom de l'option - * @param acceptedOption la liste variante de l'option accepté: -o --option - * @param argument le nombre d'argument que cette option accepte - * @param description TODO Description of the Parameter - */ - public ArgumentsParserDefaultOption(String name, String description, - int argument, String... acceptedOption) { - this.name = name; - this.description = description; - this.acceptedOption = acceptedOption; - this.argument = argument; - } - - public String[] getAcceptedOption() { - return acceptedOption; - } - - /** - * TODO Description of the Method - * - * @return TODO Description of the Return Value - */ - public ArgumentsParserOption copy() { - return new ArgumentsParserDefaultOption(name, description, argument, acceptedOption).setRepetitionMin(getRepetitionMin()).setRepetitionMax(getRepetitionMax()); - } - - /** - * Description of the Method - * - * @param args Description of the Parameter - * @param index Description of the Parameter - * @return vrai si le parsing a reussi - */ - public boolean parse(String[] args, int index) { - parsedOption = args[index++]; - if (acceptOption(parsedOption)) { - for (int i = 0; i < count(); i++) { - argumentList.add(args[index + i]); - } - return true; - } - return false; - } - - /** - * Gets the usedOption attribute of the DefaultOption object - * - * @return The usedOption value - */ - public String getUsedOption() { - return parsedOption; - } - - /** - * Gets the description attribute of the DefaultOption object - * - * @return The description value - */ - public String getDescription() { - return description; - } - - - /** - * Get repetitionMin property. - * - * @return RepetitionMin property. - */ - public int getRepetitionMin() { - return this.repetitionMin; - } - - /** - * Set repetitionMin property. - * - * @param repetitionMin New repetitionMin property. - * @return TODO Description of the Return Value - */ - public ArgumentsParserOption setRepetitionMin(int repetitionMin) { - this.repetitionMin = repetitionMin; - return this; - } - - - /** - * Get repetitionMax property. - * - * @return RepetitionMax property. - */ - public int getRepetitionMax() { - return this.repetitionMax; - } - - /** - * Set repetitionMax property. - * - * @param repetitionMax New repetitionMax property. - * @return TODO Description of the Return Value - */ - public ArgumentsParserOption setRepetitionMax(int repetitionMax) { - this.repetitionMax = repetitionMax; - return this; - } - - - /** - * Gets the name attribute of the DefaultOption object - * - * @return The name value - */ - public String getName() { - return name; - } - - /** - * Gets the arguments attribute of the DefaultOption object - * - * @return The arguments value - */ - public String[] getArguments() { - return (String[]) argumentList.toArray(new String[count()]); - } - - /** - * return numbers of args for this option - * - * @return numbers of args for this option - */ - public int count() { - return argument; - } - - /** - * Description of the Method - * - * @param optionName Description of the Parameter - * @return Description of the Return Value - */ - protected boolean acceptOption(String optionName) { - for (int i = 0; i < acceptedOption.length; i++) { - if (acceptedOption[i].equals(optionName)) { - return true; - } - } - return false; - } - - /** - * TODO Description of the Method - * - * @return TODO Description of the Return Value - */ - public String toString() { - return name + "(" + parsedOption + ")" + argumentList; - } -}// ArgumentsParserDefaultOption - Deleted: nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserOption.java =================================================================== --- nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserOption.java 2009-05-18 10:36:02 UTC (rev 1598) +++ nuitonutil/trunk/src/main/java/org/nuiton/util/ArgumentsParserOption.java 2009-05-18 10:51:45 UTC (rev 1599) @@ -1,84 +0,0 @@ -/* *##% Lutin utilities library - * Copyright (C) 2004 - 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ - -package org.nuiton.util; - -/** - * Cette classe représente une option de la ligne de commande - * - * @author poussin - * created 22 août 2003 - * - * @deprecated since 0.30 , prefer use of {@link ApplicationConfig} instead - */ -public interface ArgumentsParserOption extends Cloneable {// Option - /** - * Essai de parser un argument. Si cela a fonctionné, retourne un argument - * sinon retourne null. - * - *@param arg liste des arguments de la ligne de commande - *@param index l'index ou le parser est rendu - *@return vrai si le parsing a réussi - */ - public boolean parse(String[] arg, int index); - - /** - * Retourne le nom de l'option - * - * @return le nom - */ - public String getName(); - - public String getDescription(); - - public String [] getAcceptedOption(); - - public int getRepetitionMin(); - public int getRepetitionMax(); - public ArgumentsParserOption setRepetitionMin(int repetitionMin); - public ArgumentsParserOption setRepetitionMax(int repetitionMax); - - /** - * Retourne l'option utilisée sur la ligne de commande - * - *@return l'option utilisée sur la ligne de commende - */ - public String getUsedOption(); - - /** - * Retourne la liste des arguments de l'option. - * - *@return la liste des arguments de l'option. - */ - public String[] getArguments(); - - /** - * Permet de connaitre le nombre d'argument consommé par cet objet - * - *@return le nombre d'argument - */ - public int count(); - - /** - * Copie l'objet - * - *@return retourne un nouvel objet - */ - public ArgumentsParserOption copy(); -}// Option -