summaryrefslogtreecommitdiffstats
path: root/args4j/args4j/src/org/kohsuke/args4j/XmlParser.java
blob: abf07f1e90c8d2e8c9fc657faf0b92876e11ab1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.kohsuke.args4j;

import java.lang.reflect.AccessibleObject;
import java.net.URL;

import org.kohsuke.args4j.spi.ArgumentImpl;
import org.kohsuke.args4j.spi.ConfigElement;
import org.kohsuke.args4j.spi.OptionImpl;
import org.kohsuke.args4j.spi.Setters;
import org.xml.sax.InputSource;

/**
 * Parses an XML-file specifying the 'annotations'.
 * The XML must have the structure:
 * <pre>
 * &lt;args&gt;
 *     &lt;option field="" method="" name="" usage="" metavar="" handler=""/&gt;
 *     &lt;argument field="" method="" usage="" metavar="" handler=""/&gt;
 * &lt;/args&gt;
 * </pre>
 * Exactly one of the attributes 'field' or 'method' must be set.
 * The 'handler' value specifies a full qualified class name.
 *
 * <h3>Example</h3>
 * <pre>
 * &lt;args&gt;
 *     &lt;option field="recursive" name="-r" usage="recursively run something"/&gt;
 *     &lt;option field="out" name="-o" usage="output to this file" metavar="OUTPUT"/&gt;
 *     &lt;option method="setStr(String)" name="-str"/&gt;
 *     &lt;option field="data" name="-custom" handler="org.kohsuke.args4j.spi.BooleanOptionHandler" usage="boolean value for checking the custom handler"/&gt;
 *     &lt;argument field="arguments"/&gt;
 * &lt;args&gt;
 * </pre>
 *
 * @author Jan Materne
 */
public class XmlParser {
    public void parse(URL xml, CmdLineParser parser, Object bean) {
        parse(new InputSource(xml.toExternalForm()),parser,bean);
    }

	public void parse(InputSource xml, CmdLineParser parser, Object bean) {
		try {
			Config config = Config.parse(xml);
			for(ConfigElement ce : config.options) {
				Option option = new OptionImpl(ce);
                parser.addOption(Setters.create(parser, findMethodOrField(bean, ce.field, ce.method),bean), option);
			}
			for (ConfigElement ce : config.arguments) {
				Argument argument = new ArgumentImpl(ce);
				parser.addArgument(Setters.create(parser, findMethodOrField(bean, ce.field, ce.method),bean), argument);
			}
		} catch (Exception e) {
			throw new RuntimeException(Messages.METADATA_ERROR.format(), e);
		}
	}


	/**
	 * Finds a {@link java.lang.reflect.Method} or {@link java.lang.reflect.Method} in the bean
	 * instance with the requested name.
	 * @param bean    bean instance
	 * @param field   name of the field (field XOR method must be specified)
	 * @param method  name of the method (field XOR method must be specified)
	 * @return the reflection reference
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws NoSuchMethodException
	 * @throws ClassNotFoundException
	 */
	private AccessibleObject findMethodOrField(Object bean, String field, String method) throws SecurityException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
		AccessibleObject rv;
		if (field != null) {
			rv = bean.getClass().getDeclaredField(field);
		} else {
			String methodName = method.substring(0, method.indexOf("("));
			String[] params = method.substring(method.indexOf("(")+1, method.indexOf(")")).split(",");
			Class[] paramTypes = new Class[params.length];
			for(int i=0; i<params.length; i++) {
				String className = params[i];
				if (className.indexOf('.') < 0) {
					className = "java.lang." + className;
				}
				paramTypes[i] = Class.forName(className);
			}
			rv = bean.getClass().getMethod(methodName, paramTypes);
		}
		return rv;
	}

}