DBVIEW
src/org/dbview/utils/args4j/CommaSeparatedStripedListOptionHandler.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 /**
00020  * @author Denis Beurive
00021  */
00022 
00023 package org.dbview.utils.args4j;
00024 
00025 import org.kohsuke.args4j.CmdLineException;
00026 import org.kohsuke.args4j.CmdLineParser;
00027 import org.kohsuke.args4j.OptionDef;
00028 import org.kohsuke.args4j.spi.OptionHandler;
00029 import org.kohsuke.args4j.spi.Parameters;
00030 import org.kohsuke.args4j.spi.Setter;
00031 import java.util.*;
00032 
00033 /**
00034  * This class implements a special kind of options' values.
00035  * Options' values of this kind are comma separated lists of values.
00036  * For example: "value1,value,value3".
00037  * @author Denis BEURIVE
00038  * @see args4j documentation.
00039  */
00040 public class CommaSeparatedStripedListOptionHandler extends OptionHandler<String>
00041 {
00042     /**
00043      * Constructor.
00044      * @param parser Args4J command line parser.
00045      * @param option See documentation for Args4J.
00046      * @param setter See documentation for Args4J.
00047      */
00048     public CommaSeparatedStripedListOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super String> setter)
00049     {
00050         super(parser, option, setter);
00051     }
00052 
00053     /**
00054      * This method sets the parsed argument.
00055      * @param in_parameters List of strings that represents the parameters for the option.
00056      * @warning Keep in mind that this method may set the value null.
00057      * @return See documentation for Args4J.
00058      */
00059     public int parseArguments(Parameters in_parameters) throws CmdLineException
00060     {
00061         String option_value = in_parameters.getParameter(0);
00062 
00063         // System.out.println("CommaSeparatedListOptionHandler 1");
00064 
00065         if (null == option_value) { setter.addValue(null); return 1; }
00066 
00067         // Make sure that the value does not start with a dash.
00068         if (option_value.startsWith("-"))
00069         { throw new CmdLineException(this.owner, "Invalid option's value. Option values can not start with a dash. Dashes are used to specify options. If the option's value you need to specify starts with a dash, then you must escape it. Exemple \"-myValue\" becomes \"\\-myValue\"."); }
00070         option_value = option_value.replaceFirst("^\\\\", "");
00071 
00072         // Split the value in order to remove leading and trailing spaces.
00073         String[] values = option_value.split(",");
00074         ArrayList<String> striped_values = new ArrayList<String>();
00075         for (String value: values) {striped_values.add(value.trim()); }
00076         setter.addValue(org.dbview.utils.Strings.join(striped_values, ","));
00077         return 1;
00078     }
00079 
00080     /**
00081      * See documentation for Args4J.
00082      * @return See documentation for Args4J.
00083      */
00084     public String getDefaultMetaVariable()
00085     {
00086         return "VAL";
00087     }
00088 
00089 }