add set command and switch completely to . as an object seperator
[org.ibex.xt-crawshaw.git] / src / java / org / ibex / xt / shell / Request.java
index f0d3697..2f85247 100644 (file)
@@ -100,6 +100,45 @@ public abstract class Request implements Serializable {
         }
     }
 
+    public static class SetKey extends Key {
+        protected JS value;
+        public SetKey() {}
+        public SetKey(String p, String m, JS val) { super(p, m); this.value = val; }
+
+        public Response process(JSScope root) throws JSExn {
+            JS js = keyed(path(root));
+            if (js == null) throw new JSExn("no such path");
+
+            js.put(matcher, JS.eval(JS.cloneWithNewParentScope(
+                                    value, new JSResolve(root, js))));
+            return new Res(); // TODO: return put() value when it has one
+        }
+
+        public class Res extends Response {
+        }
+
+        public class JSResolve extends JSScope {
+            private JS path;
+            public JSResolve(JSScope parent, JS path) { super(parent); this.path = path; }
+            public Object get(Object key) throws JSExn {
+                if (key != null && key instanceof String) {
+                    String k = (String)key;
+                    if (k.startsWith(".")) k = k.substring(1);
+                    else return path.get(k);
+                }
+                return super.get(key);
+            }
+            public void put(Object key, Object val) throws JSExn {
+                if (key != null && key instanceof String) {
+                    String k = (String)key;
+                    if (k.startsWith(".")) k = k.substring(1);
+                    else { path.put(key, val); return; }
+                }
+                super.put(key, val);
+            }
+        }
+    }
+
     /** Runs a series of requests. */
     public static class Composite extends Request {
         private boolean breakOnError = false;