org.jvnet.jaxb2_commons.plugin.AbstractParameterizablePlugin Maven / Gradle / Ivy
package org.jvnet.jaxb2_commons.plugin;
import java.io.IOException;
import org.apache.commons.beanutils.BeanUtils;
import com.sun.tools.xjc.BadCommandLineException;
import com.sun.tools.xjc.Options;
/**
* Abstract base class for parameterizable JAXB plugins.
*
* This plugin looks for the arguments of the form
* -myPlugin-name=value
* (myPlugin
is the plugin option name) and
* then invokes setName(value)
on itself.
* For instance, the argument -Xfoo-bar=test
triggers
* setBar("test")
invocation.
* Values are injected using Commons BeanUtils as bean properties, so
* types will be converted correspondingly
*
* @author valikov
*/
public abstract class AbstractParameterizablePlugin extends AbstractPlugin {
/**
* Parses the arguments and injects values into the beans via properties.
*/
public int parseArgument(Options opt, String[] args, int start)
throws BadCommandLineException, IOException {
int consumed = 0;
final String optionPrefix = "-" + getOptionName() + "-";
final int optionPrefixLength = optionPrefix.length();
final String arg = args[start];
final int equalsPosition = arg.indexOf('=');
if (arg.startsWith(optionPrefix) && equalsPosition > optionPrefixLength) {
final String propertyName = arg.substring(optionPrefixLength,
equalsPosition);
final String value = arg.substring(equalsPosition + 1);
consumed++;
try {
BeanUtils.setProperty(this, propertyName, value);
} catch (Exception ex) {
ex.printStackTrace();
throw new BadCommandLineException("Error setting property ["
+ propertyName + "], value [" + value + "].");
}
}
return consumed;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy