# HG changeset patch # User Omair Majid # Date 1361994747 18000 # Node ID d13774aae51813da72bfb2b559deca565aebbf41 # Parent 412c3e8caf4bb2ad288b573941557230ea49cb4b Parse options element in plugin.xml Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005908.html diff -r 412c3e8caf4b -r d13774aae518 launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java --- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java Wed Feb 27 12:27:17 2013 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParser.java Wed Feb 27 14:52:27 2013 -0500 @@ -51,6 +51,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; import org.apache.commons.cli.Options; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -88,6 +90,15 @@ * <name>hello</name> * <description>print hello</description> * <usage>hello</usage> + * <options> + * <options> + * <long>long</long> + * <short>l</short> + * <hasArg>true</hasArg> + * <required>true</required> + * <description>some required and long option</description> + * </option> + * </options> * <bundles> * <bundle>hello-world-plugin-0.1-SNAPSHOT.jar</bundle> * </bundles> @@ -219,8 +230,8 @@ usage = node.getTextContent().trim(); } else if (node.getNodeName().equals("description")) { description = node.getTextContent().trim(); - } else if (node.getNodeName().equals("arguments")) { - options = parseArguments(node); + } else if (node.getNodeName().equals("options")) { + options = parseOptions(node); } else if (node.getNodeName().equals("bundles")) { bundles.addAll(parseBundles(pluginName, name, node)); } else if (node.getNodeName().equals("dependencies")) { @@ -280,9 +291,67 @@ return dependencies; } - private Options parseArguments(Node argumentsNode) { - // TODO need to identify a way to express arguments - return new Options(); + private Options parseOptions(Node optionsNode) { + Options opts = new Options(); + NodeList nodes = optionsNode.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + if (node.getNodeName().equals("group")) { + OptionGroup group = parseOptionGroup(node); + opts.addOptionGroup(group); + } else if (node.getNodeName().equals("option")) { + Option option = parseOption(node); + opts.addOption(option); + } + } + + return opts; + } + + private OptionGroup parseOptionGroup(Node optionGroupNode) { + OptionGroup group = new OptionGroup(); + + NodeList nodes = optionGroupNode.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + if (node.getNodeName().equals("option")) { + Option option = parseOption(node); + group.addOption(option); + } else if (node.getNodeName().equals("required")) { + group.setRequired(Boolean.valueOf(node.getTextContent().trim())); + } + } + + return group; + } + + private Option parseOption(Node optionNode) { + String longName = null; + String shortName = null; + String description = null; + boolean required = false; + boolean hasArg = false; + + NodeList nodes = optionNode.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + if (node.getNodeName().equals("long")) { + longName = node.getTextContent().trim(); + } else if (node.getNodeName().equals("short")) { + shortName = node.getTextContent().trim(); + } else if (node.getNodeName().equals("description")) { + description = node.getTextContent().trim(); + } else if (node.getNodeName().equals("hasArg")) { + hasArg = Boolean.valueOf(node.getTextContent().trim()); + } else if (node.getNodeName().equals("required")) { + required = Boolean.valueOf(node.getTextContent().trim()); + } + } + + Option opt = new Option(shortName, longName, hasArg, description); + opt.setArgName(longName != null? longName : shortName); + opt.setRequired(required); + return opt; } private static class ConfigurationParserErrorHandler implements ErrorHandler { diff -r 412c3e8caf4b -r d13774aae518 launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java --- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java Wed Feb 27 12:27:17 2013 -0500 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PluginConfigurationParserTest.java Wed Feb 27 14:52:27 2013 -0500 @@ -37,6 +37,9 @@ package com.redhat.thermostat.launcher.internal; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -45,6 +48,8 @@ import java.util.Arrays; import java.util.List; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; import org.apache.commons.cli.Options; import org.junit.Test; @@ -180,4 +185,79 @@ assertEquals(Arrays.asList("foo", "bar baz", "buzz"), first.getPluginBundles()); assertEquals(Arrays.asList("thermostat-foo"), first.getDepenedencyBundles()); } + + @Test + public void testOptionParsing() throws UnsupportedEncodingException { + String config = "\n" + + "\n" + + " \n" + + " \n" + + " test\n" + + " just a test\n" + + " test [ -a | -b ] -l <foo>\n" + + " \n" + + " \n" + + " true\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + PluginConfiguration result = new PluginConfigurationParser() + .parse("test", new ByteArrayInputStream(config.getBytes("UTF-8"))); + + assertEquals(0, result.getExtendedCommands().size()); + + List newCommands = result.getNewCommands(); + assertEquals(1, newCommands.size()); + + NewCommand command = newCommands.get(0); + assertEquals("test", command.getCommandName()); + assertEquals("just a test", command.getDescription()); + assertEquals("test [ -a | -b ] -l ", command.getUsage()); + Options opts = command.getOptions(); + assertNull(opts.getOption("foobarbaz")); + + Option requiredOption = opts.getOption("l"); + assertNotNull(requiredOption); + + Option exclusiveOptionA = opts.getOption("a"); + assertNotNull(exclusiveOptionA); + assertEquals("exclusive-a", exclusiveOptionA.getLongOpt()); + assertFalse(exclusiveOptionA.hasArg()); + assertFalse(exclusiveOptionA.isRequired()); + assertEquals("exclusive option a", exclusiveOptionA.getDescription()); + + Option exclusiveOptionB = opts.getOption("b"); + assertNotNull(exclusiveOptionB); + assertEquals("exclusive-b", exclusiveOptionB.getLongOpt()); + assertFalse(exclusiveOptionB.hasArg()); + assertFalse(exclusiveOptionB.isRequired()); + assertEquals("exclusive option b", exclusiveOptionB.getDescription()); + + OptionGroup group = opts.getOptionGroup(exclusiveOptionA); + assertTrue(group.isRequired()); + } }