Revision 890

tmp/org.txm.libs.args4j/build.properties (revision 890)
1
source.. = src/
2
output.. = bin/
3
bin.includes = META-INF/,\
4
               .
0 5

  
tmp/org.txm.libs.args4j/.settings/org.eclipse.jdt.core.prefs (revision 890)
1
eclipse.preferences.version=1
2
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4
org.eclipse.jdt.core.compiler.compliance=1.7
5
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7
org.eclipse.jdt.core.compiler.source=1.7
0 8

  
tmp/org.txm.libs.args4j/.classpath (revision 890)
1
<?xml version="1.0" encoding="UTF-8"?>
2
<classpath>
3
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
4
	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5
	<classpathentry kind="src" path="src"/>
6
	<classpathentry kind="output" path="bin"/>
7
</classpath>
0 8

  
tmp/org.txm.libs.args4j/META-INF/MANIFEST.MF (revision 890)
1
Manifest-Version: 1.0
2
Bundle-ManifestVersion: 2
3
Bundle-Name: org.txm.libs.args4j
4
Bundle-SymbolicName: org.txm.libs.args4j
5
Bundle-Version: 1.0.0.qualifier
6
Automatic-Module-Name: org.txm.libs.args4j
7
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
8
Require-Bundle: org.txm.searchengine.cqp.core;bundle-version="1.1.0"
9
Export-Package: org.kohsuke.args4j,
10
 org.kohsuke.args4j.spi
0 11

  
tmp/org.txm.libs.args4j/.project (revision 890)
1
<?xml version="1.0" encoding="UTF-8"?>
2
<projectDescription>
3
	<name>org.txm.libs.args4j</name>
4
	<comment></comment>
5
	<projects>
6
	</projects>
7
	<buildSpec>
8
		<buildCommand>
9
			<name>org.eclipse.jdt.core.javabuilder</name>
10
			<arguments>
11
			</arguments>
12
		</buildCommand>
13
		<buildCommand>
14
			<name>org.eclipse.pde.ManifestBuilder</name>
15
			<arguments>
16
			</arguments>
17
		</buildCommand>
18
		<buildCommand>
19
			<name>org.eclipse.pde.SchemaBuilder</name>
20
			<arguments>
21
			</arguments>
22
		</buildCommand>
23
	</buildSpec>
24
	<natures>
25
		<nature>org.eclipse.pde.PluginNature</nature>
26
		<nature>org.eclipse.jdt.core.javanature</nature>
27
	</natures>
28
</projectDescription>
0 29

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/Messages.java (revision 890)
1
package org.kohsuke.args4j;
2

  
3
import java.text.MessageFormat;
4
import java.util.ResourceBundle;
5

  
6
/**
7
 * @author Kohsuke Kawaguchi
8
 */
9
enum Messages {
10
    MISSING_OPERAND,
11
    UNDEFINED_OPTION,
12
    NO_ARGUMENT_ALLOWED,
13
    REQUIRED_OPTION_MISSING,
14
    TOO_MANY_ARGUMENTS,
15
    REQUIRED_ARGUMENT_MISSING,
16
    METADATA_ERROR,
17
    MULTIPLE_USE_OF_ARGUMENT,
18
    MULTIPLE_USE_OF_OPTION,
19
    UNKNOWN_HANDLER,
20
    NO_OPTIONHANDLER,
21
    NO_CONSTRUCTOR_ON_HANDLER,
22
    FORMAT_ERROR_FOR_MAP,
23
    MAP_HAS_NO_KEY, UNDEFINNED_WIDGET
24
    ;
25

  
26
    private static ResourceBundle rb;
27

  
28
    public String format( Object... args ) {
29
        synchronized(Messages.class) {
30
            if(rb==null)
31
                rb = ResourceBundle.getBundle(Messages.class.getName());
32
            return MessageFormat.format(rb.getString(name()),args);
33
        }
34
    }
35
}
0 36

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/Option.java (revision 890)
1
package org.kohsuke.args4j;
2

  
3
import static java.lang.annotation.ElementType.FIELD;
4
import static java.lang.annotation.ElementType.METHOD;
5
import static java.lang.annotation.ElementType.PARAMETER;
6
import static java.lang.annotation.RetentionPolicy.RUNTIME;
7

  
8
import java.io.File;
9
import java.lang.annotation.Retention;
10
import java.lang.annotation.Target;
11
import java.lang.reflect.AccessibleObject;
12
import java.util.ResourceBundle;
13

  
14
import org.kohsuke.args4j.spi.OptionHandler;
15

  
16
/**
17
 * Marks a field/setter that receives a command line switch value.
18
 *
19
 * <p>
20
 * This annotation can be placed on a field of type T or the method
21
 * of the form <tt>void <i>methodName</i>(T value)</tt>. Its access
22
 * modified can be anything, but if it's not public, your application
23
 * needs to run in a security context that allows args4j to access
24
 * the field/method (see {@link AccessibleObject#setAccessible(boolean)}.
25
 *
26
 * <p>
27
 * The behavior of the annotation differs depending on T --- the type
28
 * of the field or the parameter of the method.
29
 *
30
 * <h2>Boolean Switch</h2>
31
 * <p>
32
 * When T is boolean , it represents
33
 * a boolean option that takes the form of "-OPT". When this option is set,
34
 * the property will be set to true.
35
 *
36
 * <h2>String Switch</h2>
37
 * <p>
38
 * When T is {@link String}, it represents
39
 * an option that takes one operand. The value of the operand is set
40
 * to the property.
41
 *
42
 * <h2>Enum Switch</h2>
43
 * <p>
44
 * When T is derived from {@link Enum}, it represents an option that takes
45
 * an operand, which must be one of the enum constant. The comparion between
46
 * the operand and the enum constant name is done in a case insensitive fashion.
47
 * <p>
48
 * For example, the following definition will represent command line options
49
 * like "-coin penny" or "-coin DIME" but things like "-coin" or "-coin abc" are
50
 * errors.
51
 *
52
 * <pre>
53
 * enum Coin { PENNY,NICKEL,DIME,QUARTER }
54
 *
55
 * class Option {
56
 *   &#64;Option(name="-coin")
57
 *   public Coin coin;
58
 * }
59
 * </pre>
60
 *
61
 * <h2>File Switch</h2>
62
 * <p>
63
 * When T is a {@link File}, it represents an option that takes a file/directory
64
 * name as an operand.
65
 *
66
 * @author Kohsuke Kawaguchi
67
 */
68
@Retention(RUNTIME)
69
@Target({FIELD,METHOD,PARAMETER})
70
public @interface Option {
71
    /**
72
     * Name of the option, such as "-foo" or "-bar".
73
     */
74
    String name();
75
    
76
    /**
77
     * Widget to use in an UI, such as Query, File, Folder, String, Text, Boolean, Integer, Float et Date.
78
     */
79
    String widget() default "String";
80
    
81
    /**
82
     * default value as String
83
     */
84
	String def() default "";
85
    
86
    /**
87
     * Aliases for the options, such as "--long-option-name".
88
     */
89
    String[] aliases() default { };
90

  
91
    /**
92
     * Help string used to display the usage screen.
93
     *
94
     * <p>
95
     * This parameter works in two ways. For a simple use,
96
     * you can just encode the human-readable help string directly,
97
     * and that will be used as the message. This is easier,
98
     * but it doesn't support localization.
99
     *
100
     * <p>
101
     * For more advanced use, this property is set to a key of a
102
     * {@link ResourceBundle}. The actual message is obtained
103
     * by querying a {@link ResourceBundle} instance supplied to
104
     * {@link CmdLineParser} by this key. This allows the usage
105
     * screen to be properly localized.
106
     *
107
     * <p>
108
     * If this value is empty, the option will not be displayed
109
     * in the usage screen.
110
     */
111
    String usage() default "";
112

  
113
    /**
114
     * When the option takes an operand, the usage screen will show something like this:
115
     * <pre>
116
     * -x FOO  : blah blah blah
117
     * </pre>
118
     * You can replace the 'FOO' token by using this parameter.
119
     *
120
     * <p>
121
     * If left unspecifiied, this value is infered from the type of the option.
122
     *
123
     * <p>
124
     * Just like {@link #usage()}, normally, this value is printed as is.
125
     * But if a {@link ResourceBundle} is given to the {@link CmdLineParser},
126
     * it will be used to obtain the locale-specific value.
127
     */
128
    String metaVar() default "";
129

  
130
    /**
131
     * Specify that the option is mandatory.
132
     *
133
     * <p>
134
     * At the end of {@link CmdLineParser#parseArgument(String...)},
135
     * a {@link CmdLineException} will be thrown if a required option
136
     * is not present.
137
     *
138
     * <p>
139
     * Note that in most of the command line interface design principles,
140
     * options should be really optional. So use caution when using this
141
     * flag.
142
     */
143
    boolean required() default false;
144

  
145
    /**
146
     * Specify the {@link OptionHandler} that processes the command line arguments.
147
     *
148
     * <p>
149
     * The default value {@link OptionHandler} indicates that
150
     * the {@link OptionHandler} will be infered from
151
     * the type of the field/method where a {@link Option} annotation
152
     * is placed.
153
     *
154
     * <p>
155
     * If this annotation element is used, it overrides the inference
156
     * and determines the handler to be used. This is convenient for
157
     * defining a non-standard option parsing semantics.
158
     *
159
     * <h3>Example</h3>
160
     * <pre>
161
     * // this is a normal "-r" option
162
     * &#64;Option(name="-r")
163
     * boolean value;
164
     *
165
     * // this causes arg4j to use MyHandler, not the default
166
     * // handler provided for boolean
167
     * &#64;Option(name="-b",handler=MyHandler.class)
168
     * boolean value;
169
     * </pre>
170
     */
171
    Class<? extends OptionHandler> handler() default OptionHandler.class;
172
    
173
    /**
174
     * Whether the option is multi-valued.
175
     * For mappings to List<...>, this defaults to true, otherwise false 
176
     */
177
    boolean multiValued() default false;
178
}
0 179

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/ConfigElement.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
/**
4
 * The ConfigElement is an <tt>&lt;option></tt> or <tt>&lt;argument></tt> tag
5
 * in the xml configuration file.
6
 * @author Jan Materne
7
 */
8
public class ConfigElement {
9
	public String field;
10
	public String method;
11
	public String name;
12
	public String usage = ""; //$NON-NLS-1$
13
	public String handler;
14
	public String metavar = ""; //$NON-NLS-1$
15
	public String[] aliases = {};
16
	public boolean multiValued = false;
17
	public boolean required = false;
18
	/**
19
	 * Ensures that only a field XOR a method is set.
20
	 * @return
21
	 */
22
	public boolean isInvalid() {
23
		return field == null && method == null || field != null && method != null;
24
	}
25
}
0 26

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/Messages.properties (revision 890)
1
ILLEGAL_OPERAND = \
2
    "{1}" is not a valid value for "{0}"
3
ILLEGAL_BOOLEAN = \
4
	"{0}" is not a legal boolean value
5
ILLEGAL_CHAR = \
6
    "{0}" must be a single character
7
ILLEGAL_METHOD_SIGNATURE = \
8
    Method {0} takes more than one parameter
9
ILLEGAL_FIELD_SIGNATURE = \
10
    Field of type {0} isn''t supported by args4j
11
ILLEGAL_LIST = \
12
    type of {0} is not a List
0 13

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/ShortOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineParser;
4
import org.kohsuke.args4j.OptionDef;
5

  
6
/**
7
 * {@link Short}
8
 * {@link OptionHandler}
9
 * {@link OneArgumentOptionHandler}
10
 * @author Jan Materne
11
 * @since 2.0.9
12
 */
13
public class ShortOptionHandler extends OneArgumentOptionHandler<Short> {
14

  
15
	public ShortOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Short> setter) {
16
		super(parser, option, setter);
17
	}
18

  
19
	@Override
20
	protected Short parse(String argument) throws NumberFormatException {
21
		return Short.parseShort(argument);
22
	}
23

  
24
}
0 25

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/LongOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineParser;
4
import org.kohsuke.args4j.OptionDef;
5

  
6
/**
7
 * {@link Long}
8
 * {@link OptionHandler}
9
 * {@link OneArgumentOptionHandler}
10
 * @author Jan Materne
11
 * @since 2.0.9
12
 */
13
public class LongOptionHandler extends OneArgumentOptionHandler<Long> {
14

  
15
	public LongOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Long> setter) {
16
		super(parser, option, setter);
17
	}
18

  
19
	@Override
20
	protected Long parse(String argument) throws NumberFormatException {
21
		return Long.parseLong(argument);
22
	}
23

  
24
}
0 25

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/Messages_ru.properties (revision 890)
1

  
2
ILLEGAL_METHOD_SIGNATURE = \u041C\u0435\u0442\u043E\u0434 {0} \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043E\u043B\u0435\u0435 \u043E\u0434\u043D\u043E\u0433\u043E \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u0430
3

  
4
ILLEGAL_FIELD_SIGNATURE = \u041F\u043E\u043B\u0435 \u0442\u0438\u043F\u0430 {0} \u043D\u0435 \u043F\u043E\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044F args4j
5

  
6

  
7
# For Translation
8
#-------------------
9
# ILLEGAL_OPERAND =     "{1}" is not a valid value for "{0}"
10
# ILLEGAL_BOOLEAN = 	"{0}" is not a legal boolean value
11
# ILLEGAL_CHAR =     "{0}" must be a single character
12
# ILLEGAL_LIST =     type of {0} is not a List
0 13

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/Setters.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.MapSetter;
4
import org.kohsuke.args4j.CmdLineParser;
5

  
6
import java.lang.reflect.Field;
7
import java.lang.reflect.AccessibleObject;
8
import java.lang.reflect.Method;
9
import java.util.List;
10
import java.util.Map;
11

  
12
/**
13
 * Factory of {@link Setter}s.
14
 *
15
 * @author Kohsuke Kawaguchi
16
 */
17
public class Setters {
18
    public static Setter create(CmdLineParser parser, AccessibleObject fieldOrMethod, Object bean) {
19
        if (fieldOrMethod instanceof Method) {
20
            return new MethodSetter(parser,bean,(Method) fieldOrMethod);
21
        } else {
22
            return create((Field)fieldOrMethod,bean);
23
        }
24
    }
25

  
26
    public static Setter create(Field f, Object bean) {
27
        if(List.class.isAssignableFrom(f.getType()))
28
            return new MultiValueFieldSetter(bean,f);
29
        else if(Map.class.isAssignableFrom(f.getType()))
30
            return new MapSetter(bean,f);
31
        else
32
            return new FieldSetter(bean,f);
33
    }
34
}
0 35

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/OptionImpl.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.Option;
4

  
5
/**
6
 * Implementation of @Option so we can instantiate it.
7
 * @author Jan Materne
8
 */
9
public class OptionImpl extends AnnotationImpl implements Option {
10
	public OptionImpl(ConfigElement ce) throws ClassNotFoundException {
11
		super(Option.class,ce);
12
		name = ce.name;
13
	}
14
	public String name;
15
	public String name() {
16
		return name;
17
	}
18
	@Override
19
	public String widget() {
20
		// TODO Auto-generated method stub
21
		return ""; //$NON-NLS-1$
22
	}
23
	@Override
24
	public String def() {
25
		// TODO Auto-generated method stub
26
		return ""; //$NON-NLS-1$
27
	}
28
}
0 29

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/ByteOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineParser;
4
import org.kohsuke.args4j.OptionDef;
5

  
6
/**
7
 * {@link Byte}
8
 * {@link OptionHandler}
9
 * {@link OneArgumentOptionHandler}
10
 * @author Jan Materne
11
 * @since 2.0.9
12
 */
13
public class ByteOptionHandler extends OneArgumentOptionHandler<Byte> {
14

  
15
	public ByteOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Byte> setter) {
16
		super(parser, option, setter);
17
	}
18

  
19
	@Override
20
	protected Byte parse(String argument) throws NumberFormatException {
21
		return Byte.parseByte(argument);
22
	}
23

  
24
}
0 25

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/EnumOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineException;
4
import org.kohsuke.args4j.CmdLineParser;
5
import org.kohsuke.args4j.OptionDef;
6

  
7
/**
8
 * {@link Enum} {@link OptionHandler}.
9
 *
10
 * @author Kohsuke Kawaguchi
11
 */
12
public class EnumOptionHandler<T extends Enum<T>> extends OptionHandler<T> {
13

  
14
    private final Class<T> enumType;
15

  
16
    public EnumOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super T> setter, Class<T> enumType) {
17
        super(parser, option, setter);
18
        this.enumType = enumType;
19
    }
20

  
21
    @Override
22
    public int parseArguments(Parameters params) throws CmdLineException {
23
        String s = params.getParameter(0).replaceAll("-", "_");
24
        T value = null;
25
        for( T o : enumType.getEnumConstants() )
26
            if(o.name().equalsIgnoreCase(s)) {
27
                value = o;
28
                break;
29
            }
30

  
31
        if(value==null) {
32
            if (option.isArgument()) {
33
                throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND.format(option.toString(),s));
34
            } else {
35
                throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND.format(params.getParameter(-1),s));
36
            }
37
        }
38
        setter.addValue(value);
39
        return 1;
40
    }
41

  
42
    /* 
43
     * Returns all values of an enum type split by pipe.
44
     * <tt>[ one | two | three]</tt>
45
     * @see org.kohsuke.args4j.spi.OptionHandler#getDefaultMetaVariable()
46
     */
47
    @Override
48
    public String getDefaultMetaVariable() {
49
    	StringBuffer rv = new StringBuffer();
50
    	rv.append("[");
51
    	for (T t : enumType.getEnumConstants()) {
52
			rv.append(t).append(" | ");
53
		}
54
    	rv.delete(rv.length()-3, rv.length());
55
    	rv.append("]");
56
    	return rv.toString();
57
    }
58
}
0 59

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/FloatOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineParser;
4
import org.kohsuke.args4j.OptionDef;
5

  
6
/**
7
 * {@link Float} 
8
 * {@link OptionHandler}
9
 * {@link OneArgumentOptionHandler}
10
 * 
11
 * @author Jan Materne
12
 * @since 2.0.9
13
 */
14
public class FloatOptionHandler extends OneArgumentOptionHandler<Float> {
15

  
16
	public FloatOptionHandler(CmdLineParser parser, OptionDef option,
17
			Setter<? super Float> setter) {
18
		super(parser, option, setter);
19
	}
20

  
21
	@Override
22
	protected Float parse(String argument) throws NumberFormatException {
23
		return Float.parseFloat(argument);
24
	}
25

  
26
}
0 27

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/StringArrayOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineException;
4
import org.kohsuke.args4j.CmdLineParser;
5
import org.kohsuke.args4j.OptionDef;
6

  
7
import java.util.ArrayList;
8

  
9
/**
10
 * <p>
11
 * An {@link OptionHandler} for handling {@link String[]} types. Can handle arrays of strings.
12
 * </p>
13
 * <p>
14
 * <h1>How it works:</h1>
15
 * Example for parameter -s, which is String[] type:<br>
16
 * java -jar aaa.jar -s banan hruska jablko<br>
17
 * java -jar aaa.jar -s banan "hruska jablko"<br>
18
 * java -jar aaa.jar -s "banan hruska jablko"<br>
19
 * java -jar aaa.jar -s banan hruska jablko -l 4 -r<br>
20
 * java -jar aaa.jar -t 222 -s banan hruska jablko -r<br><br>
21
 * It will handle all of these posibilites. This OptionHandler scans for parameter which begins
22
 * with "-". If it found it, it will stop.
23
 * </p>
24
 *
25
 * @author PlainText,LuVar
26
 *
27
 */
28
public class StringArrayOptionHandler extends OptionHandler<String[]> {
29

  
30
	public StringArrayOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super String[]> setter) {
31
		super(parser, option, setter);
32
	}
33

  
34
	/**
35
	 * <p>
36
	 * Returns "STRING[]".
37
	 * </p>
38
	 *
39
	 * @return	return "STRING[]";
40
	 */
41
	@Override
42
	public String getDefaultMetaVariable() {
43
		return "STRING[]"; //$NON-NLS-1$
44
	}
45

  
46
	/**
47
	 * <p>
48
	 * Tryies to parse String[] argument from {@link Parameters}.
49
	 * </p>
50
	 */
51
	@Override
52
	public int parseArguments(Parameters params) throws CmdLineException {
53
		int counter = 0;
54
		ArrayList<String> values = new ArrayList<String>();
55
		while(true) {
56
			String param;
57
			try {
58
				param = params.getParameter(counter);
59
			} catch (CmdLineException ex) {
60
				break;
61
			}
62
			if(param.startsWith("-")) { //$NON-NLS-1$
63
				break;
64
			}
65

  
66
			for (String str : param.split(" ")) { //$NON-NLS-1$
67
				values.add(str);
68
			}
69
			counter++;
70
		}//while true
71

  
72
        // to work around a javac bug in Java1.5, we need to first assign this to
73
        // the raw type.
74
        Setter s = this.setter;
75
        s.addValue(values.toArray(new String[values.size()]));
76
		return counter;
77
	}
78

  
79
}
0 80

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/StringOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.OptionDef;
4
import org.kohsuke.args4j.CmdLineException;
5
import org.kohsuke.args4j.CmdLineParser;
6

  
7
/**
8
 * String {@link OptionHandler}.
9
 *
10
 * @author Kohsuke Kawaguchi
11
 */
12
public class StringOptionHandler extends OptionHandler<String> {
13
    public StringOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super String> setter) {
14
        super(parser, option, setter);
15
    }
16

  
17
    @Override
18
    public int parseArguments(Parameters params) throws CmdLineException {
19
        setter.addValue(params.getParameter(0));
20
        return 1;
21
    }
22

  
23
    @Override
24
    public String getDefaultMetaVariable() {
25
        return "VAL"; //$NON-NLS-1$
26
    }
27
}
0 28

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/ObjectOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineParser;
4
import org.kohsuke.args4j.OptionDef;
5

  
6
/**
7
 * {@link Object}
8
 * {@link OptionHandler}
9
 * {@link OneArgumentOptionHandler}
10
 * @author Jan Materne
11
 * @since 2.0.9
12
 */
13
public class ObjectOptionHandler extends OneArgumentOptionHandler<Object> {
14

  
15
	public ObjectOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Object> setter) {
16
		super(parser, option, setter);
17
	}
18

  
19
	@Override
20
	protected Object parse(String argument) throws NumberFormatException {
21
		return argument;
22
	}
23

  
24
}
0 25

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/Setter.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineException;
4

  
5
/**
6
 * Abstraction of the value setter.
7
 *
8
 * @author Kohsuke Kawaguchi
9
 */
10
public interface Setter<T> {
11
    /**
12
     * Adds/sets a value to the property of the option bean.
13
     *
14
     * <p>
15
     * A {@link Setter} object has an implicit knowledge about the property it's setting,
16
     * and the instance of the option bean.
17
     */
18
    void addValue(T value) throws CmdLineException;
19

  
20
    /**
21
     * Gets the type of the underlying method/field.
22
     */
23
    Class<T> getType();
24
    
25
    /**
26
     * Whether this setter is instrinsically multi-valued.
27
     */
28
    boolean isMultiValued();
29
}
0 30

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/MethodSetter.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.spi.Setter;
4
import org.kohsuke.args4j.*;
5

  
6
import java.lang.reflect.InvocationTargetException;
7
import java.lang.reflect.Method;
8

  
9
/**
10
 * {@link Setter} that sets to a {@link Method}.
11
 *
12
 * @author Kohsuke Kawaguchi
13
 */
14
public final class MethodSetter implements Setter {
15
    private final CmdLineParser parser;
16
    private final Object bean;
17
    private final Method m;
18

  
19
    public MethodSetter(CmdLineParser parser, Object bean, Method m) {
20
        this.parser = parser;
21
        this.bean = bean;
22
        this.m = m;
23
        if(m.getParameterTypes().length!=1)
24
            throw new IllegalAnnotationError(Messages.ILLEGAL_METHOD_SIGNATURE.format(m));
25
    }
26

  
27
    public Class getType() {
28
        return m.getParameterTypes()[0];
29
    }
30

  
31
    public boolean isMultiValued() {
32
    	return false;
33
    }
34

  
35
    public void addValue(Object value) throws CmdLineException {
36
        try {
37
            try {
38
                m.invoke(bean,value);
39
            } catch (IllegalAccessException _) {
40
                // try again
41
                m.setAccessible(true);
42
                try {
43
                    m.invoke(bean,value);
44
                } catch (IllegalAccessException e) {
45
                    throw new IllegalAccessError(e.getMessage());
46
                }
47
            }
48
        } catch (InvocationTargetException e) {
49
            Throwable t = e.getTargetException();
50
            if(t instanceof RuntimeException)
51
                throw (RuntimeException)t;
52
            if(t instanceof Error)
53
                throw (Error)t;
54
            if(t instanceof CmdLineException)
55
                throw (CmdLineException)t;
56

  
57
            // otherwise wrap
58
            if(t!=null)
59
                throw new CmdLineException(parser, t);
60
            else
61
                throw new CmdLineException(parser, e);
62
        }
63
    }
64
}
0 65

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/ExplicitBooleanOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import static java.lang.Boolean.FALSE;
4
import static java.lang.Boolean.TRUE;
5

  
6
import java.util.HashMap;
7
import java.util.Map;
8

  
9
import org.kohsuke.args4j.CmdLineException;
10
import org.kohsuke.args4j.CmdLineParser;
11
import org.kohsuke.args4j.OptionDef;
12

  
13
/**
14
 * Boolean {@link OptionHandler} that (unlike the standard {@link BooleanOptionHandler}
15
 * allows values to be set to false explicitly (using e.g. '-myOpt false') rather
16
 * than only returning false when the option is <em>omitted</em>.
17
 */
18
public class ExplicitBooleanOptionHandler extends OptionHandler<Boolean> {
19
    // same values as BooleanOptionHandler
20
    private static final Map<String, Boolean> ACCEPTABLE_VALUES;
21
    
22
    static {
23
        // I like the trick in BooleanOptionHandler but find this explicit map more readable
24
        ACCEPTABLE_VALUES = new HashMap<String, Boolean>();
25
        ACCEPTABLE_VALUES.put("true", TRUE); //$NON-NLS-1$
26
        ACCEPTABLE_VALUES.put("on", TRUE); //$NON-NLS-1$
27
        ACCEPTABLE_VALUES.put("yes", TRUE); //$NON-NLS-1$
28
        ACCEPTABLE_VALUES.put("1", TRUE); //$NON-NLS-1$
29
        ACCEPTABLE_VALUES.put("false", FALSE); //$NON-NLS-1$
30
        ACCEPTABLE_VALUES.put("off", FALSE); //$NON-NLS-1$
31
        ACCEPTABLE_VALUES.put("no", FALSE); //$NON-NLS-1$
32
        ACCEPTABLE_VALUES.put("0", FALSE); //$NON-NLS-1$
33
    }
34
        
35
    public ExplicitBooleanOptionHandler(CmdLineParser parser,
36
            OptionDef option, Setter<? super Boolean> setter) {
37
        super(parser, option, setter);
38
    }
39

  
40
    @Override
41
    public int parseArguments(Parameters params) throws CmdLineException {
42
        // end of arg list or next arg is another option
43
        if ((params.size() == 0) || params.getParameter(0).startsWith("-")) { //$NON-NLS-1$
44
            setter.addValue(TRUE);
45
            return 0;
46
        } else {
47
            setter.addValue(getBoolean(params.getParameter(0)));
48
            return 1;
49
        }
50
    }
51

  
52
    private Boolean getBoolean(String parameter) throws CmdLineException {
53
        String valueStr = parameter.toLowerCase();
54
        if (!ACCEPTABLE_VALUES.containsKey(valueStr)) {
55
            throw new CmdLineException(owner, Messages.ILLEGAL_BOOLEAN.format(valueStr));
56
        }
57
        return ACCEPTABLE_VALUES.get(valueStr);
58
    }
59

  
60
    @Override
61
    public String getDefaultMetaVariable() {
62
        return "[VAL]"; //$NON-NLS-1$
63
    }
64
}
0 65

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/FieldSetter.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.spi.Setter;
4

  
5
import java.lang.reflect.Field;
6

  
7
/**
8
 * {@link Setter} that sets to a {@link Field}.
9
 *
10
 * @author Kohsuke Kawaguchi
11
 */
12
final class FieldSetter implements Setter {
13
    private final Field f;
14
    private final Object bean;
15

  
16
    public FieldSetter(Object bean, Field f) {
17
        this.bean = bean;
18
        this.f = f;
19
    }
20

  
21
    public Class getType() {
22
        return f.getType();
23
    }
24
    
25
    public boolean isMultiValued() {
26
    	return false;
27
    }
28

  
29
    public void addValue(Object value) {
30
        try {
31
            f.set(bean,value);
32
        } catch (IllegalAccessException _) {
33
            // try again
34
            f.setAccessible(true);
35
            try {
36
                f.set(bean,value);
37
            } catch (IllegalAccessException e) {
38
                throw new IllegalAccessError(e.getMessage());
39
            }
40
        }
41
    }
42
}
0 43

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/StopOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineException;
4
import org.kohsuke.args4j.CmdLineParser;
5
import org.kohsuke.args4j.OptionDef;
6

  
7
/**
8
 * {@link OptionHandler} for the option terminator "--".
9
 *
10
 * <p>
11
 * This {@link OptionHandler} can be used to implement the special token
12
 * "--" that indicates that the rest of tokens are not options, but arguments.
13
 *
14
 * <p>
15
 * For example, if you have the following class:
16
 *
17
 * <pre>
18
 * class Foo {
19
 *   &#64;Argument
20
 *   &#64;Option(name="--",handler={@link StopOptionHandler}.class)
21
 *   List&lt;String> args;
22
 *
23
 *   &#64;Option(name="-n")
24
 *   int n;
25
 * }
26
 * </pre>
27
 *
28
 * <p>
29
 * The command line {@code -n 5 abc def} would parse into n=5, args={"abc",def"},
30
 * but {@code -- -n 5 abc def} would parse into n=0, args={"-n","5","abc","def"}.
31
 *
32
 * @author Kohsuke Kawaguchi
33
 */
34
public class StopOptionHandler extends OptionHandler<String> {
35
    public StopOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super String> setter) {
36
        super(parser, option, setter);
37
    }
38

  
39
    @Override
40
    public int parseArguments(Parameters params) throws CmdLineException {
41
    	owner.stopOptionParsing();
42
    	return 0;
43
    }
44

  
45
    @Override
46
    public String getDefaultMetaVariable() {
47
        return "ARGUMENTS"; //$NON-NLS-1$
48
    }
49
}
0 50

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/package.html (revision 890)
1
<html><body>
2
<p>
3
Classes for extending the behavior of args4j.
4
</p>
5
</body></html>
0 6

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/IntOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineParser;
4
import org.kohsuke.args4j.OptionDef;
5

  
6
/**
7
 * {@link Integer} 
8
 * {@link OptionHandler}.
9
 * {@link OneArgumentOptionHandler}
10
 * 
11
 * @author Kohsuke Kawaguchi
12
 */
13
public class IntOptionHandler extends OneArgumentOptionHandler<Integer> {
14
	public IntOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super Integer> setter) {
15
		super(parser, option, setter);
16
	}
17

  
18
	@Override
19
	protected Integer parse(String argument) throws NumberFormatException {
20
		return Integer.parseInt(argument);
21
	}
22
}
0 23

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/MultiValueFieldSetter.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.spi.Setter;
4
import org.kohsuke.args4j.*;
5

  
6
import java.lang.reflect.Field;
7
import java.lang.reflect.ParameterizedType;
8
import java.lang.reflect.Type;
9
import java.util.ArrayList;
10
import java.util.List;
11

  
12
/**
13
 * {@link Setter} that sets multiple values to a collection {@link Field}.
14
 *
15
 * @author Kohsuke Kawaguchi
16
 */
17
final class MultiValueFieldSetter  implements Setter {
18
    private final Object bean;
19
    private final Field f;
20

  
21
    public MultiValueFieldSetter(Object bean, Field f) {
22
        this.bean = bean;
23
        this.f = f;
24

  
25
        if(!List.class.isAssignableFrom(f.getType()))
26
            throw new IllegalAnnotationError(Messages.ILLEGAL_FIELD_SIGNATURE.format(f.getType()));
27
    }
28

  
29
    public boolean isMultiValued() {
30
    	return true;
31
    }
32

  
33
    public Class getType() {
34
        // TODO: compute this correctly
35
        Type t = f.getGenericType();
36
        if(t instanceof ParameterizedType) {
37
            ParameterizedType pt = (ParameterizedType)t;
38
            t = pt.getActualTypeArguments()[0];
39
            if(t instanceof Class)
40
                return (Class)t;
41
        }
42
        return Object.class;
43
    }
44

  
45
    public void addValue(Object value) {
46
        try {
47
            doAddValue(bean, value);
48
        } catch (IllegalAccessException _) {
49
            // try again
50
            f.setAccessible(true);
51
            try {
52
                doAddValue(bean,value);
53
            } catch (IllegalAccessException e) {
54
                throw new IllegalAccessError(e.getMessage());
55
            }
56
        }
57
    }
58

  
59
    private void doAddValue(Object bean, Object value) throws IllegalAccessException {
60
        Object o = f.get(bean);
61
        if (o==null) {
62
            o = new ArrayList<Object>();
63
            f.set(bean,o);
64
        }
65
        if (!(o instanceof List))
66
            throw new IllegalAnnotationError(Messages.ILLEGAL_LIST.format(f));
67

  
68
        ((List)o).add(value);
69
    }
70
}
0 71

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/URIOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineException;
4
import org.kohsuke.args4j.CmdLineParser;
5
import org.kohsuke.args4j.OptionDef;
6

  
7
import java.net.MalformedURLException;
8
import java.net.URL;
9
import java.net.URI;
10
import java.net.URISyntaxException;
11

  
12
/**
13
 * {@link URI} {@link OptionHandler}.
14
 *
15
 * @author Kohsuke Kawaguchi
16
 */
17
public class URIOptionHandler extends OptionHandler<URI> {
18
    public URIOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super URI> setter) {
19
        super(parser, option, setter);
20
    }
21

  
22
    @Override
23
    public int parseArguments(Parameters params) throws CmdLineException {
24
        String param = params.getParameter(0);
25
        try {
26
            setter.addValue(new URI(param));
27
            return 1;
28
        } catch (URISyntaxException e) {
29
            throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND.format(params.getParameter(-1),param));
30
        }
31
    }
32

  
33
    @Override
34
    public String getDefaultMetaVariable() {
35
        return "URI"; //$NON-NLS-1$
36
    }
37
}
0 38

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/RestOfArgumentsHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.Argument;
4
import org.kohsuke.args4j.CmdLineParser;
5
import org.kohsuke.args4j.OptionDef;
6
import org.kohsuke.args4j.CmdLineException;
7

  
8
/**
9
 * Eagerly grabs all the arguments.
10
 *
11
 * <p>
12
 * Used with {@link Argument}, this implements a semantics where
13
 * non-option token causes the option parsing to terminate.
14
 * An example of this is ssh(1), where "ssh -p 222 abc" will treat "-p" as an option
15
 * to ssh but "ssh abc -p 222" is considered to have no option for ssh.
16
 *
17
 * @author Kohsuke Kawaguchi
18
 */
19
public class RestOfArgumentsHandler extends OptionHandler<String> {
20
    public RestOfArgumentsHandler(CmdLineParser cmdLineParser, OptionDef optionDef, Setter<String> setter) {
21
        super(cmdLineParser, optionDef, setter);
22
    }
23

  
24
    public int parseArguments(Parameters parameters) throws CmdLineException {
25
        for (int i=0; i<parameters.size(); i++)
26
            setter.addValue(parameters.getParameter(i));
27
        return parameters.size();
28
    }
29

  
30
    public String getDefaultMetaVariable() {
31
        return "ARGS"; //$NON-NLS-1$
32
    }
33
}
0 34

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/URLOptionHandler.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import org.kohsuke.args4j.CmdLineException;
4
import org.kohsuke.args4j.CmdLineParser;
5
import org.kohsuke.args4j.OptionDef;
6

  
7
import java.net.MalformedURLException;
8
import java.net.URL;
9

  
10
/**
11
 * {@link URL} {@link OptionHandler}.
12
 *
13
 * @author Kohsuke Kawaguchi
14
 */
15
public class URLOptionHandler extends OptionHandler<URL> {
16
    public URLOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super URL> setter) {
17
        super(parser, option, setter);
18
    }
19

  
20
    @Override
21
    public int parseArguments(Parameters params) throws CmdLineException {
22
        String param = params.getParameter(0);
23
        try {
24
            setter.addValue(new URL(param));
25
            return 1;
26
        } catch (MalformedURLException e) {
27
            throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND.format(
28
                    params.getParameter(-1),param));
29
        }
30
    }
31

  
32
    @Override
33
    public String getDefaultMetaVariable() {
34
        return "URL"; //$NON-NLS-1$
35
    }
36
}
0 37

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/Messages_de.properties (revision 890)
1
# Unicode for Umlauts
2
#   Ae, ae     \u00c4, \u00e4
3
#   Oe, oe     \u00d6, \u00f6
4
#   Ue, ue     \u00dc, \u00fc
5
#   ss         \u00df
6

  
7

  
8

  
9
ILLEGAL_METHOD_SIGNATURE = \
10
    Methode {0} ben\u00f6tigt mehr als einen Parameter
11
ILLEGAL_FIELD_SIGNATURE = \
12
    Der Datentyp {0} wird von args4j nicht unterst\u00fctzt
13
ILLEGAL_OPERAND = \
14
    "{1}" ist kein g\u00fcltiger Wert f\u00fcr "{0}"
15
ILLEGAL_BOOLEAN = \
16
	"{0}" ist kein g\u00fcltiger Boolscher Wert
17
ILLEGAL_CHAR = \
18
    "{0}" muss ein einzelnes Zeichen sein
19
ILLEGAL_LIST = \
20
    Der Typ von {0} ist keine List
0 21

  
tmp/org.txm.libs.args4j/src/org/kohsuke/args4j/spi/AnnotationImpl.java (revision 890)
1
package org.kohsuke.args4j.spi;
2

  
3
import java.lang.annotation.Annotation;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff