diff options
author | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-07 07:00:27 +0000 |
---|---|---|
committer | antelder <antelder@13f79535-47bb-0310-9956-ffa450edef68> | 2010-07-07 07:00:27 +0000 |
commit | 0b0c517366baa9528c3849f349ce9e9ad9ddc49f (patch) | |
tree | fd590831dc39f22ee45100671f706aa55a775704 | |
parent | e284af21ab7ad05e5bfe091aaf9a00104de8e7ef (diff) |
Update to use a Tuscany specific completor that can complete each command argument
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@961259 13f79535-47bb-0310-9956-ffa450edef68
2 files changed, 234 insertions, 0 deletions
diff --git a/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/CompositeURICompletor.java b/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/CompositeURICompletor.java new file mode 100644 index 0000000000..025b03fc92 --- /dev/null +++ b/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/CompositeURICompletor.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.shell; + +import java.util.ArrayList; +import java.util.List; + +import jline.SimpleCompletor; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.node2.Node; + +/** + * A Completor that uses the composite URIs within a Contribution + * Will only work if the argument before the composite URI is a contribution URI + */ +public class CompositeURICompletor extends SimpleCompletor { + + private Node node; + + public CompositeURICompletor(Node node) { + super(""); + this.node = node; + } + + @Override + public int complete(final String buffer, final int cursor, final List clist) { + Contribution c = node.getInstalledContribution(getContributionURI()); + if (c == null) { + return -1; + } + + List<String> cus = new ArrayList<String>(); + for (Artifact a : c.getArtifacts()) { + if (a.getModel() instanceof Composite) { + cus.add(((Composite)a.getModel()).getURI()); + } + } + setCandidateStrings(cus.toArray(new String[cus.size()])); + return super.complete(buffer, cursor, clist); + } + + protected String getContributionURI() { + /* A little hacky to use a static but i can't see how else to get the contribution URI */ + return TShellCompletor.lastArg; + } +} diff --git a/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/TShellCompletor.java b/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/TShellCompletor.java new file mode 100644 index 0000000000..be70b1e375 --- /dev/null +++ b/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/TShellCompletor.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.shell; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import jline.ArgumentCompletor; +import jline.Completor; +import jline.ConsoleReader; +import jline.FileNameCompletor; +import jline.NullCompletor; +import jline.SimpleCompletor; + +import org.apache.tuscany.sca.node2.Node; + +/** + * A Completor thats specific to the Tuscany Shell that knows about + * each command and has an argument specific Completor for each command argument. + */ +public class TShellCompletor extends ArgumentCompletor { + + Map<String, Completor[]> completors; + final Completor commandCompletor = new SimpleCompletor(Shell.COMMANDS); + final ArgumentDelimiter delim = new WhitespaceArgumentDelimiter(); + final Node node; + + static String lastArg; + + public TShellCompletor(Node node) { + super((Completor)null); + this.node = node; + completors = new HashMap<String, Completor[]>(); + completors.put("help", new Completor[]{commandCompletor, commandCompletor, new NullCompletor()}); +// completors.put("install", new Completor[]{commandCompletor, new InstallCompletor(), new NullCompletor()}); + completors.put("install", new Completor[]{commandCompletor, new FileNameCompletor(), new FileNameCompletor(), new NullCompletor()}); + completors.put("installed", new Completor[]{commandCompletor, new ICURICompletor(node), new NullCompletor()}); + completors.put("remove", new Completor[]{commandCompletor, new ICURICompletor(node), new NullCompletor()}); + completors.put("addDeploymentComposite", new Completor[]{commandCompletor, new ICURICompletor(node), new FileNameCompletor(), new NullCompletor()}); + completors.put("addToDomainLevelComposite", new Completor[]{commandCompletor, new ICURICompletor(node), new CompositeURICompletor(node), new NullCompletor()}); + completors.put("removeFromDomainLevelComposite", new Completor[]{commandCompletor, new ICURICompletor(node), new CompositeURICompletor(node), new NullCompletor()}); + completors.put("listDeployedCompostes", new Completor[]{commandCompletor, new ICURICompletor(node), new NullCompletor()}); + completors.put("listInstalledContributions", new Completor[]{commandCompletor, new NullCompletor()}); + completors.put("printDomainLevelComposite", new Completor[]{commandCompletor, new NullCompletor()}); + completors.put("start", new Completor[]{commandCompletor, new ICURICompletor(node), new CompositeURICompletor(node), new NullCompletor()}); + completors.put("status", new Completor[]{commandCompletor, new ICURICompletor(node), new CompositeURICompletor(node), new NullCompletor()}); + completors.put("stop", new Completor[]{commandCompletor, new ICURICompletor(node), new CompositeURICompletor(node), new NullCompletor()}); + } + + @Override + /** + * Copied from JLine ArgumentCompletor class. The only change is to + * get the completors by using the getCompletors method and setting the lastArg static. + */ + public int complete(final String buffer, final int cursor, + final List candidates) { + ArgumentList list = delim.delimit(buffer, cursor); + int argpos = list.getArgumentPosition(); + int argIndex = list.getCursorArgumentIndex(); + + if (argIndex < 0) { + return -1; + } + + if (argIndex > 0) { + /* set the last argument in a static for the CompositeURICompletor */ + lastArg = list.getArguments()[argIndex-1]; + } + + final Completor comp; + + Completor[] completors = getCompletors(buffer); + + // if we are beyond the end of the completors, just use the last one + if (argIndex >= completors.length) { + comp = completors[completors.length - 1]; + } else { + comp = completors[argIndex]; + } + + // ensure that all the previous completors are successful before + // allowing this completor to pass (only if strict is true). + for (int i = 0; getStrict() && (i < argIndex); i++) { + Completor sub = + completors[(i >= completors.length) ? (completors.length - 1) : i]; + String[] args = list.getArguments(); + String arg = ((args == null) || (i >= args.length)) ? "" : args[i]; + + List subCandidates = new LinkedList(); + + if (sub.complete(arg, arg.length(), subCandidates) == -1) { + return -1; + } + + if (subCandidates.size() == 0) { + return -1; + } + } + + int ret = comp.complete(list.getCursorArgument(), argpos, candidates); + + if (ret == -1) { + return -1; + } + + int pos = ret + (list.getBufferPosition() - argpos); + + /** + * Special case: when completing in the middle of a line, and the + * area under the cursor is a delimiter, then trim any delimiters + * from the candidates, since we do not need to have an extra + * delimiter. + * + * E.g., if we have a completion for "foo", and we + * enter "f bar" into the buffer, and move to after the "f" + * and hit TAB, we want "foo bar" instead of "foo bar". + */ + if ((cursor != buffer.length()) && delim.isDelimiter(buffer, cursor)) { + for (int i = 0; i < candidates.size(); i++) { + String val = candidates.get(i).toString(); + + while ((val.length() > 0) + && delim.isDelimiter(val, val.length() - 1)) { + val = val.substring(0, val.length() - 1); + } + + candidates.set(i, val); + } + } + + ConsoleReader.debug("Completing " + buffer + "(pos=" + cursor + ") " + + "with: " + candidates + ": offset=" + pos); + + return pos; + } + + protected Completor[] getCompletors(String buffer) { + StringBuilder sb = new StringBuilder(); + for (int i=0; i<buffer.length(); i++) { + if (Character.isWhitespace(buffer.charAt(i))) { + break; + } + sb.append(buffer.charAt(i)); + } + String command = sb.toString(); + Completor[] comps = completors.get(command); + return comps == null ? new Completor[]{commandCompletor} : comps; + } +} |