Mercurial > hg > thermostat-tools-eclipse
changeset 46:c3c3388877f7
Support bundles and arguments in plugin model
author | Omair Majid <omajid@redhat.com> |
---|---|
date | Mon, 30 Dec 2013 09:30:58 -0500 |
parents | a75d3028e5e6 |
children | c870da2752e9 |
files | .classpath META-INF/MANIFEST.MF src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java |
diffstat | 5 files changed, 254 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- a/.classpath Fri Dec 27 15:20:23 2013 -0500 +++ b/.classpath Mon Dec 30 09:30:58 2013 -0500 @@ -3,5 +3,6 @@ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="src" path="src"/> + <classpathentry kind="src" path="test"/> <classpathentry kind="output" path="bin"/> </classpath>
--- a/META-INF/MANIFEST.MF Fri Dec 27 15:20:23 2013 -0500 +++ b/META-INF/MANIFEST.MF Mon Dec 30 09:30:58 2013 -0500 @@ -14,6 +14,7 @@ org.eclipse.jdt.launching, org.eclipse.jdt.core;bundle-version="3.9.0", org.eclipse.ui.forms;bundle-version="3.6.0", - org.eclipse.wst.sse.ui;bundle-version="1.3.200" + org.eclipse.wst.sse.ui;bundle-version="1.3.200", + org.junit;bundle-version="4.10.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Bundle-ActivationPolicy: lazy
--- a/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java Fri Dec 27 15:20:23 2013 -0500 +++ b/src/com/redhat/thermostat/plugin/eclipse/editor/CommandEditPage.java Mon Dec 30 09:30:58 2013 -0500 @@ -19,7 +19,9 @@ import org.eclipse.ui.forms.widgets.Section; import com.redhat.thermostat.plugin.eclipse.model.PluginModel; +import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Bundle; import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Command; +import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Option; import com.redhat.thermostat.plugin.eclipse.model.PluginModel.PluginModelChangeListener; class CommandEditPage implements IDetailsPage { @@ -206,7 +208,10 @@ Command updated = new Command(commandName.getText(), commandDescription.getText(), commandUsage.getText(), - environments); + new ArrayList<String>(), + new ArrayList<Option>(), + environments, + new ArrayList<Bundle>()); model.updateCommand(updated); } }
--- a/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java Fri Dec 27 15:20:23 2013 -0500 +++ b/src/com/redhat/thermostat/plugin/eclipse/model/PluginModel.java Mon Dec 30 09:30:58 2013 -0500 @@ -5,7 +5,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.concurrent.CopyOnWriteArrayList; @@ -143,6 +142,7 @@ expressionString = "/plugin/commands/command[name/text() = '" + name + "']"; expr = xpath.compile(expressionString); node = (Node) expr.evaluate(doc, XPathConstants.NODE); + Node commandNode = node; expressionString = "/plugin/commands/command[name/text() = '" + name + "']/description"; expr = xpath.compile(expressionString); @@ -154,22 +154,73 @@ node = (Node) expr.evaluate(doc, XPathConstants.NODE); String usage = node == null ? "" : node.getTextContent(); - expressionString = "/plugin/commands/command[name/text() = '" + name + "']/environments/environment"; - expr = xpath.compile(expressionString); - NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); - List<String> environments = new ArrayList<>(); - for (int i = 0; i < nodes.getLength(); i++) { - node = nodes.item(i); - String env = node.getTextContent(); - environments.add(env); - } - System.out.println(environments); - return new Command(name, description, usage, environments); + List<String> arguments = parseArguments(commandNode); + List<Option> options = parseOptions(commandNode); + List<String> environments = parseEnvironments(commandNode); + List<Bundle> bundles = parseBundles(commandNode); + + return new Command(name, description, usage, arguments, options, environments, bundles); } catch (XPathExpressionException e) { throw new AssertionError("bad xpath expression", e); } } + private List<String> parseArguments(Node commandNode) throws XPathExpressionException { + String expressionString = "arguments/argument"; + XPathExpression expr = xPathfactory.newXPath().compile(expressionString); + NodeList nodes = (NodeList) expr.evaluate(commandNode, XPathConstants.NODESET); + List<String> arguments = new ArrayList<>(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + String arg = node.getTextContent(); + arguments.add(arg); + } + return arguments; + } + + private List<Option> parseOptions(Node commandNode) { + // TODO implement me + return new ArrayList<>(); + } + + private List<String> parseEnvironments(Node commandNode) throws XPathExpressionException { + String expressionString = "environments/environment"; + XPathExpression expr = xPathfactory.newXPath().compile(expressionString); + NodeList nodes = (NodeList) expr.evaluate(commandNode, XPathConstants.NODESET); + List<String> environments = new ArrayList<>(); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + String env = node.getTextContent(); + environments.add(env); + } + return environments; + } + + private List<Bundle> parseBundles(Node commandNode) throws XPathExpressionException { + String expressionString = "bundles/bundle"; + XPathExpression expr = xPathfactory.newXPath().compile(expressionString); + NodeList nodes = (NodeList) expr.evaluate(commandNode, XPathConstants.NODESET); + List<Bundle> bundles = new ArrayList<>(); + for (int i = 0; i < nodes.getLength(); i++) { + NodeList children = nodes.item(i).getChildNodes(); + + String symbolicName = null; + String version = null; + + for (int j = 0; j < children.getLength(); j++) { + + Node node = children.item(j); + if (node.getNodeName().equals("symbolic-name")) { + symbolicName = node.getTextContent(); + } else if (node.getNodeName().equals("version")) { + version = node.getTextContent(); + } + } + + bundles.add(new Bundle(symbolicName, version)); + } + return bundles; + } public void addCommand(String name) { // FIXME sanitize name; it should be alphanumeric only! @@ -234,17 +285,33 @@ Node descriptionNode = null; Node usageNode = null; + Node argumentsNode = null; + Node optionsNode = null; Node environmentsNode = null; + Node bundlesNode = null; NodeList childNodes = commandNode.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { node = childNodes.item(i); - if (node.getNodeName().equals("description")) { + switch (node.getNodeName()) { + case "description": descriptionNode = node; - } else if (node.getNodeName().equals("usage")) { + break; + case "usage": usageNode = node; - } else if (node.getNodeName().equals("environments")) { + break; + case "arguments": + argumentsNode = node; + break; + case "options": + optionsNode = node; + break; + case "environments": environmentsNode = node; + break; + case "bundles": + bundlesNode = node; + break; } } @@ -262,30 +329,92 @@ usageNode.setTextContent(command.usage); - if (environmentsNode == null) { - environmentsNode = doc.createElement("environments"); - commandNode.appendChild(environmentsNode); - } + updateArguments(command.arguments, commandNode, argumentsNode); + updateOptions(command.options, commandNode, optionsNode); + updateEnvironments(command.environments, commandNode, environmentsNode); + updateBundles(command.bundles, commandNode, bundlesNode); - // remove all existing children - childNodes = environmentsNode.getChildNodes(); - while(childNodes.getLength() > 0) { - node = childNodes.item(0); - environmentsNode.removeChild(node); - } - - // add new children - for (String env : command.environments) { - Element environmentNode = doc.createElement("environment"); - environmentNode.setTextContent(env); - environmentsNode.appendChild(environmentNode); - } - + fireModelChanged(); } catch (XPathExpressionException e) { throw new AssertionError("bad xpath expression", e); } } + private void updateArguments(List<String> arguments, Node commandNode, Node argumentsNode) { + if (argumentsNode == null) { + argumentsNode = doc.createElement("arguments"); + commandNode.appendChild(argumentsNode); + } + + // remove all existing children + NodeList childNodes = argumentsNode.getChildNodes(); + while(childNodes.getLength() > 0) { + Node node = childNodes.item(0); + argumentsNode.removeChild(node); + } + + // add new children + for (String env : arguments) { + Element argumentNode = doc.createElement("argument"); + argumentNode.setTextContent(env); + argumentsNode.appendChild(argumentNode); + } + } + + private void updateOptions(List<Option> oiptions, Node commandNode, Node optionsNode) { + // TODO implement me + } + + private void updateEnvironments(List<String> environments, Node commandNode, Node environmentsNode) { + if (environmentsNode == null) { + environmentsNode = doc.createElement("environments"); + commandNode.appendChild(environmentsNode); + } + + // remove all existing children + NodeList childNodes = environmentsNode.getChildNodes(); + while(childNodes.getLength() > 0) { + Node node = childNodes.item(0); + environmentsNode.removeChild(node); + } + + // add new children + for (String env : environments) { + Element environmentNode = doc.createElement("environment"); + environmentNode.setTextContent(env); + environmentsNode.appendChild(environmentNode); + } + } + + private void updateBundles(List<Bundle> bundles, Node commandNode, Node bundlesNode) { + if (bundlesNode == null) { + bundlesNode = doc.createElement("bundles"); + commandNode.appendChild(bundlesNode); + } + + // remove all existing children + NodeList childNodes = bundlesNode.getChildNodes(); + while(childNodes.getLength() > 0) { + Node node = childNodes.item(0); + bundlesNode.removeChild(node); + } + + // add new children + for (Bundle bundle : bundles) { + Element bundleNode = doc.createElement("bundle"); + + Element nameNode = doc.createElement("symbolic-name"); + nameNode.setTextContent(bundle.name); + Element versionNode = doc.createElement("version"); + versionNode.setTextContent(bundle.version); + + bundleNode.appendChild(nameNode); + bundleNode.appendChild(versionNode); + + bundlesNode.appendChild(bundleNode); + } + } + public void removeCommand(String name) { // FIXME sanitize name; it should be alphanumeric only! @@ -327,33 +456,31 @@ public final String name; public final String description; public final String usage; - public final List<Argument> arguments; + public final List<String> arguments; public final List<Option> options; public final List<String> environments; public final List<Bundle> bundles; - public Command(String name, String description, String usage, List<String> environments) { + public Command(String name, String description, String usage, + List<String> arguments, List<Option> options, + List<String> environments, List<Bundle> bundles) { this.name = name; this.description = description; this.usage = usage; - this.arguments = Collections.emptyList(); - this.options = Collections.emptyList(); + this.arguments = arguments; + this.options = options; this.environments = environments; - this.bundles = Collections.emptyList(); - } - } - - public static class Argument { - public final String name; - - public Argument(String name) { - this.name = name; + this.bundles = bundles; } } public static class Option { - // TODO complete this + public final String name; + + public Option(String name) { + this.name = name; + } } public static class Extension { @@ -384,19 +511,19 @@ } public static class Bundle { - private String name; - private String version; + private final String name; + private final String version; - public Bundle(String name, String bundle) { + public Bundle(String name, String version) { this.name = name; - this.version = bundle; + this.version = version; } - public String getBundleName(){ + public String getName(){ return this.name; } - public String getBundleVersion(){ + public String getVersion(){ return this.version; } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/com/redhat/thermostat/plugin/eclipse/model/PluginModelTest.java Mon Dec 30 09:30:58 2013 -0500 @@ -0,0 +1,65 @@ +package com.redhat.thermostat.plugin.eclipse.model; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import org.junit.Test; + +import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Bundle; +import com.redhat.thermostat.plugin.eclipse.model.PluginModel.Command; + +public class PluginModelTest { + + @Test + public void testParsingCommand() { + String document = "<?xml version='1.0'?>\n" + + "<plugin>\n" + + " <commands>\n" + + " <command>\n" + + " <name>foo</name>\n" + + " <usage>foo bar</usage>\n" + + " <description>foos the bar if possible</description>\n" + + " <arguments>\n" + + " <argument>bar</argument>\n" + + " </arguments>\n" + + " <options>\n" + + " <option>\n" + + " </option>\n" + + " </options>\n" + + " <environments>\n" + + " <environment>cli</environment>\n" + + " </environments>\n" + + " <bundles>\n" + + " <bundle><symbolic-name>foo</symbolic-name><version>1</version></bundle>\n" + + " </bundles>\n" + + " </command>\n" + + " </commands>\n" + + "</plugin>\n"; + + InputStream stream = new ByteArrayInputStream(document.getBytes(StandardCharsets.UTF_8)); + PluginModel model = new PluginModel(null, "<stdin>", stream); + + Command foo = model.getCommand("foo"); + + assertNotNull(foo); + + assertEquals("foo", foo.name); + assertEquals("foo bar", foo.usage); + assertEquals("foos the bar if possible", foo.description); + + assertArrayEquals(new String[] {"cli"}, foo.environments.toArray(new String[0])); + + List<Bundle> bundles = foo.bundles; + assertEquals(1, bundles.size()); + + Bundle bundle = bundles.get(0); + assertEquals("foo", bundle.getName()); + assertEquals("1", bundle.getVersion()); + } +}