+ /** Supported commands. */
+ private Command[] commands;
+
+ /** Root context. */
+ private final JS root;
+
+ /** Current JS context for the shell. */
+ private JS scope;
+
+ /** Current path to <tt>scope</tt> in <tt>root</tt>. */
+ private Object[] path;
+
+ /** Returns the object represented by the given path,
+ * ignoring the current shell path context.*/
+ public Object getFromPath(Object[] path) throws NoSuchPathException, JSExn {
+ if (path.length == 0) return root;
+
+ if (root instanceof JSRemote) {
+ // JSRemote will automatically process its keys for '.' seperators
+ StringBuffer sb = new StringBuffer(path[0].toString());
+ for (int i=1; i < path.length; i++) {
+ sb.append('.'); sb.append(path[i].toString());
+ }
+ return root.get(sb.toString());
+ } else {
+ JS cur = root;
+ for (int i=0; i < path.length - 1; i++) {
+ Object o = cur.get(path[i]);
+ if (o == null || !(o instanceof JS)) throw new Shell.NoSuchPathException();
+ cur = (JS)o;
+ }
+ return cur.get(path[path.length - 1]);
+ }
+ }
+
+ /** Set the current path of the shell, modifiying the result of getScope(). */
+ public void setPath(Object[] s) throws NoSuchPathException {
+ JS cur = root;
+ if (s == null) s = new Object[0];
+ for (int i=0; i < s.length; i++) {
+ Object o;
+ try { o = cur.get(s[i]); } catch (JSExn e) { throw new NoSuchPathException(); }
+ if (o == null || !(o instanceof JS)) throw new NoSuchPathException();
+ cur = (JS)o;
+ }
+ scope = cur;
+ path = s;
+ }
+
+ /** Returns the current path of the shell. */
+ public Object[] getPath() { return path; }
+
+ /** Returns String represntation of path, using '/' as a seperator. */
+ public String getPathAsString() {
+ if (path.length == 0) return "/";
+ StringBuffer sb = new StringBuffer();
+ for (int i=0; i < path.length; i++) {
+ sb.append('/'); sb.append(path[i].toString());
+ }
+ return sb.toString();
+ }
+
+ /** Returns the context matching the current path. */
+ public JS getScope() { return scope; }
+
+ /** Returns the context matching the current path. */
+ public JS getRootScope() { return root; }
+
+ /** Returns all supported commands. */
+ public Command[] getCommands() { return commands; }
+
+ /** Returns the command matching the given name. */
+ public Command getCommand(String name) {
+ for (int i=0; i < commands.length; i++)
+ if (commands[i].name().equals(name)) return commands[i];
+ return null;
+ }